【服务治理】熔断和限流


前言

服务的熔断/降级/限流

一、相关概念

详见本地hystrix文档

二、使用步骤

  1. POM.XML 引入依赖
com.sf.sgs.hystrix sf-sgs-hystrix 1.0.8 com.sf.sgs.hystrix sf-sgs-hystrix
  1. 在动态配置中心新增配置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:类名_方法名

#以上配置支持动态调整

  1. 修改spring-config-service.xml系统配置
    使包能够使用动态配置中心配置

classpath:hystrix.properties

  1. 实现默认降级实现方法

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;
}
}

  1. 修改配置文件spring-dubbo-service.xml,
    配置默认的降级方法实现

com.sf.cos.command.service.CommandExceptionFilter

  1. 重写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);
    }
}

}

  1. 配置RPC过滤器

HystrixFilter=com.sf.sgs.hystrix.filter.HystrixFilter

  1. 编写测试代码

@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;
}

  1. 进行简单测试
    postMan连续请求接口 http://localhost:8080/cos/config/hystrix/hystrixTest

返回值如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值