前言
服务的熔断/降级/限流
一、相关概念
详见本地hystrix文档
二、使用步骤
- POM.XML 引入依赖
- 在动态配置中心新增配置hystrix.properties
#是否开启使用全局配置 off 关闭
hystrix.switch=off
#默认配置
#使用命令调用隔离方式,默认:采用线程隔离
hystrix.command.default.execution.isolation.strategy=SEMAPHORE
#配置默认信号量最大值,超过信号量拒绝
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=100
#熔断器默认工作时间,默认:5秒.熔断器中断请求5秒后会进入半打开状态,放部分流量过去重试
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000
#默认:50%。当出错率超过该值后熔断器启动.
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50
#指定类方法配置
hystrix.command.IPickupTaskService_getDeskClerkTask.execution.isolation.semaphore.maxConcurrentRequests =200
#配置自定义信号量
hystrix.command.BuildBagService_addBagPackageBySort.execution.isolation.semaphore.maxConcurrentRequests=125
#其中BuildBagService_addBagPackageBySort:类名_方法名
#以上配置支持动态调整
- 修改spring-config-service.xml系统配置
使包能够使用动态配置中心配置
classpath:hystrix.properties
- 实现默认降级实现方法
package com.sf.cos.command.service.hystrix;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcResult;
import com.sf.sgs.hystrix.handler.HttpFallbackHandler;
public class HttpFallbackHandlerImpl implements HttpFallbackHandler {
@Override
public Result getResult(Invoker<?> invoker, Invocation invocation) {
RpcResult rpcResult = new RpcResult();
com.sf.framework.domain.Result result = new
com.sf.framework.domain.Result<>();
result.setErrorCode(“500”);
result.setErrorMessage(“系统压力过大,您的请求已经被降级,请稍后再试!”);
rpcResult.setValue(result);
return rpcResult;
}
}
- 修改配置文件spring-dubbo-service.xml,
配置默认的降级方法实现
com.sf.cos.command.service.CommandExceptionFilter
- 重写HystrixFilter
用于覆盖依赖中的代码,便于观察日志。上线时可以删除
package com.sf.sgs.hystrix.filter;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.baidu.disconf.client.common.model.DisconfCenterFile;
import com.baidu.disconf.client.common.model.DisconfCenterFile.FileItemValue;
import com.baidu.disconf.client.store.inner.DisconfCenterStore;
import com.sf.sgs.hystrix.command.CommonHystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Activate(group = {“provider”})
public class HystrixFilter implements Filter {
private static final Logger logger = LoggerFactory.getLogger(HystrixFilter.class);
private static final String OFF = "off";
public HystrixFilter() {
}
public Result invoke(Invoker<?> invoker, Invocation invocation) {
DisconfCenterFile disconfCenterFile = (DisconfCenterFile)DisconfCenterStore.getInstance().getConfFileMap().get("hystrix.properties");
FileItemValue fileItemValue = (FileItemValue)disconfCenterFile.getKeyMaps().get("hystrix.switch");
logger.error("熔断开关值:"+fileItemValue.getValue());
if (fileItemValue != null && !"off".equals(fileItemValue.getValue())) {
logger.error("执行熔断代理...............代理执行方法是:"+invocation.getMethodName());
CommonHystrixCommand commonHystrixCommand = new CommonHystrixCommand(invoker, invocation);
return commonHystrixCommand.execute();
} else {
logger.error("未执行执行熔断代理...............");
return invoker.invoke(invocation);
}
}
}
- 配置RPC过滤器
HystrixFilter=com.sf.sgs.hystrix.filter.HystrixFilter
- 编写测试代码
@GET
@Path(“/hystrix/hystrixTest”)
@ApiOperation(value = “hystrix测试”)
Result hystrixTest();
@Override
public Result hystrixTest() {
//模拟调用代码
try{
Thread.sleep(5000L);
}catch (Exception e){
e.printStackTrace();
}
Result result = new Result<> (true);
result.setObj(“hystrixTest已经返回结果…”);
return result;
}
- 进行简单测试
postMan连续请求接口 http://localhost:8080/cos/config/hystrix/hystrixTest
返回值如下: