Skip to content

Commit

Permalink
Not load hystrix when hystrix does not exist on the classpath. (sofas…
Browse files Browse the repository at this point in the history
  • Loading branch information
ScienJus authored and ujjboy committed Dec 28, 2018
1 parent 4215c1e commit 8f07447
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,25 @@ public class HystrixFilter extends Filter {
public boolean needToLoad(FilterInvoker invoker) {
AbstractInterfaceConfig config = invoker.getConfig();
// 只支持 consumer 侧
if (!(config instanceof ConsumerConfig)) {
if (LOGGER.isWarnEnabled()) {
if (!isConsumerSide(config)) {
if (LOGGER.isWarnEnabled(config.getAppName())) {
LOGGER.warnWithApp(config.getAppName(), "HystrixFilter is not allowed on provider, interfaceId: {}",
config.getInterfaceId());
}
return false;
}
String consumerHystrixEnabled = config.getParameter(HystrixConstants.SOFA_HYSTRIX_ENABLED);
if (StringUtils.isNotBlank(consumerHystrixEnabled)) {
return Boolean.valueOf(consumerHystrixEnabled);
if (!isHystrixEnabled(config)) {
return false;
}
return RpcConfigs.getOrDefaultValue(HystrixConstants.SOFA_HYSTRIX_ENABLED, false);
if (!isHystrixOnClasspath()) {
if (LOGGER.isWarnEnabled(config.getAppName())) {
LOGGER
.warnWithApp(config.getAppName(),
"HystrixFilter is disabled because 'com.netflix.hystrix:hystrix-core' does not exist on the classpath");
}
return false;
}
return true;
}

@Override
Expand All @@ -70,16 +77,27 @@ public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws So
} else {
return invoker.invoke(request);
}
logOnCircuitBreakerOpened(command, invoker, request);
return command.execute();
return command.invoke();
}

private void logOnCircuitBreakerOpened(SofaHystrixInvokable command, FilterInvoker invoker, SofaRequest request) {
if (command.isCircuitBreakerOpen()) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warnWithApp(invoker.getConfig().getAppName(), "Circuit Breaker is opened, method: {}#{}",
invoker.getConfig().getInterfaceId(), request.getMethodName());
}
private boolean isConsumerSide(AbstractInterfaceConfig config) {
return config instanceof ConsumerConfig;
}

private boolean isHystrixEnabled(AbstractInterfaceConfig config) {
String consumerHystrixEnabled = config.getParameter(HystrixConstants.SOFA_HYSTRIX_ENABLED);
if (StringUtils.isNotBlank(consumerHystrixEnabled)) {
return Boolean.valueOf(consumerHystrixEnabled);
}
return RpcConfigs.getOrDefaultValue(HystrixConstants.SOFA_HYSTRIX_ENABLED, false);
}

private boolean isHystrixOnClasspath() {
try {
Class.forName("com.netflix.hystrix.HystrixCommand");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
import com.alipay.sofa.rpc.filter.FilterInvoker;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.netflix.hystrix.HystrixCommand;

import java.lang.reflect.InvocationTargetException;
Expand All @@ -32,12 +34,14 @@
*
* @author <a href=mailto:[email protected]>ScienJus</a>
*/
public class SofaHystrixCommand extends HystrixCommand<SofaResponse> implements SofaHystrixInvokable<SofaResponse> {
public class SofaHystrixCommand extends HystrixCommand<SofaResponse> implements SofaHystrixInvokable {

private RpcInternalContext rpcInternalContext;
private RpcInvokeContext rpcInvokeContext;
protected FilterInvoker invoker;
protected SofaRequest request;
private final static Logger LOGGER = LoggerFactory.getLogger(SofaHystrixCommand.class);

private RpcInternalContext rpcInternalContext;
private RpcInvokeContext rpcInvokeContext;
protected FilterInvoker invoker;
protected SofaRequest request;

public SofaHystrixCommand(FilterInvoker invoker, SofaRequest request) {
super(SofaHystrixConfig.loadSetterFactory((ConsumerConfig) invoker.getConfig()).createSetter(invoker, request));
Expand All @@ -47,6 +51,15 @@ public SofaHystrixCommand(FilterInvoker invoker, SofaRequest request) {
this.request = request;
}

@Override
public SofaResponse invoke() {
if (isCircuitBreakerOpen() && LOGGER.isWarnEnabled(invoker.getConfig().getAppName())) {
LOGGER.warnWithApp(invoker.getConfig().getAppName(), "Circuit Breaker is opened, method: {}#{}",
invoker.getConfig().getInterfaceId(), request.getMethodName());
}
return execute();
}

@Override
protected SofaResponse run() throws Exception {
RpcInternalContext.setContext(rpcInternalContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
package com.alipay.sofa.rpc.hystrix;

import com.alipay.sofa.rpc.core.response.SofaResponse;
import com.netflix.hystrix.HystrixInvokableInfo;

/**
* Basic interface for {@link SofaHystrixCommand} and {@link SofaHystrixObservableCommand}
* @param <R>
*
* @author <a href=mailto:[email protected]>ScienJus</a>
*/
public interface SofaHystrixInvokable<R> extends HystrixInvokableInfo<R> {
public interface SofaHystrixInvokable {

SofaResponse execute();
SofaResponse invoke();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
import com.alipay.sofa.rpc.filter.FilterInvoker;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.message.ResponseFuture;
import com.netflix.hystrix.HystrixObservableCommand;
import rx.Observable;
Expand All @@ -38,13 +40,15 @@
*/
public class SofaHystrixObservableCommand extends HystrixObservableCommand implements SofaHystrixInvokable {

private FilterInvoker invoker;
private final static Logger LOGGER = LoggerFactory.getLogger(SofaHystrixCommand.class);

private SofaRequest request;
private FilterInvoker invoker;

private SofaResponse sofaResponse;
private SofaRequest request;

private ResponseFuture responseFuture;
private SofaResponse sofaResponse;

private ResponseFuture responseFuture;

public SofaHystrixObservableCommand(FilterInvoker invoker, SofaRequest request) {
super(SofaHystrixConfig.loadSetterFactory((ConsumerConfig) invoker.getConfig()).createObservableSetter(invoker,
Expand Down Expand Up @@ -97,7 +101,11 @@ public Object call(Object fallback) {
}

@Override
public SofaResponse execute() {
public SofaResponse invoke() {
if (isCircuitBreakerOpen() && LOGGER.isWarnEnabled(invoker.getConfig().getAppName())) {
LOGGER.warnWithApp(invoker.getConfig().getAppName(), "Circuit Breaker is opened, method: {}#{}",
invoker.getConfig().getInterfaceId(), request.getMethodName());
}
Future delegate = this.toObservable().toBlocking().toFuture();
RpcInternalContext.getContext().setFuture(new HystrixResponseFuture(delegate, this.responseFuture));
if (this.sofaResponse == null && this.responseFuture == null) {
Expand Down

0 comments on commit 8f07447

Please sign in to comment.