Spring Cloud sentinel使用总结

1 配置限流

spring:
  application:
    name: service-feign
  cloud:
      sentinel:
        transport:
          port: 8719
          dashboard: 192.168.0.105:8080
          clientIp: 192.168.0.105
        datasource:
          ds1:
            file:
              file: classpath:flowRule.json
              data-type: json
              rule-type: flow
[{

    "resource":"sayHiFromClientOne",//必填项,资源名称可以是方法
    "count":1,//必填项,限流阈值 单位是每秒
    "grade": 1,//0:线程数(客户端并发控制)1:QPS(默认)
    "limitApp": "default",
    "strategy" : 0,//
判断的根据是资源自身,还是根据其它关联资源 (refResource),还是根据链路入口 根据资源本身
"controlBehavior": 0 //0:直接拒绝(默认)2:匀速通过
}]

2 配置熔断

  熔断的配置可以配在实际的服务类也可以配在负载均衡客户端,比如ribbon和feign

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

//    @HystrixCommand(fallbackMethod = "hiError")
    @SentinelResource(value= "hiService", blockHandler  ="hiErrorLimit" ,fallback = "hiErrorFallback")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

    public String hiErrorLimit(String name) {
        return "hi,"+name+",limit!";
    }

    public String hiErrorFallback(String name) {
        return "hi,"+name+",fallback!";
    }

}

 

posted on 2021-02-22 19:47  MaXianZhe  阅读(184)  评论(0编辑  收藏  举报

导航