Spring Cloud Alibaba Sentinel

/ springcloud / 2 条评论 / 1717浏览

Spring Cloud Alibaba Sentinel

官网:https://sentinelguard.io/zh-cn/

流控规则

降级规则

热点规则

系统规则

SentinelResource配置

    @RequestMapping("/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
            blockHandlerClass = CustomerBlockHandler.class,
            blockHandler = "handlerException2")
    public CommonResult customerBlockHandler()
    {
        return CommonResult.success(new Payment(1l,"2021","serial002"));
    }
    
    
public class CustomerBlockHandler {

    public static CommonResult handlerException(BlockException exception)
    {
        return CommonResult.fail("自定义全局限流处理类,global handlerException----1");
    }
    public static CommonResult handlerException2(BlockException exception)
    {
        return CommonResult.fail("自定义全局限流处理类,global handlerException----2");
    }
}
    @RequestMapping("/consumer/fallback/{id}")
    @SentinelResource(value = "fallback",blockHandler = "blockHandler")
    public CommonResult<Payment> fallback(@PathVariable Long id)
    {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id, CommonResult.class,id);
        if (id == 4) {
            throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");
        }else if (result.getData() == null) {
            throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常");
        }
        return result;
    }
    
    //本例是blockHandler
    public CommonResult blockHandler(@PathVariable  Long id, BlockException blockException) {
        Payment payment = new Payment(id,"null","");
        return new CommonResult<>(445,"blockHandler-sentinel限流,无此流水: blockException  "+blockException.getMessage(),payment);
    }
    @RequestMapping("/consumer/fallback/{id}")
    @SentinelResource(value = "fallback",fallback = "handlerFallback",exceptionsToIgnore = {NullPointerException.class})
    public CommonResult<Payment> fallback(@PathVariable Long id)
    {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id, CommonResult.class,id);
        if (id == 4) {
            throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");
        }else if (result.getData() == null) {
            throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常");
        }
        return result;
    }

    //本例是fallback
    public CommonResult handlerFallback(@PathVariable  Long id,Throwable e) {
        Payment payment = new Payment(id,"null","");
        return new CommonResult<>(444,"兜底异常handlerFallback,exception内容  "+e.getMessage(),payment);
    }

Sentinel持久化

spring:
  cloud:
    sentinel:
      datasource:
        ds1:
          nacos:
            server-addr: localhost:8848
            dataid: cloudalibaba-sentinel-service   #服务名称
            groupid: DEFAULT_GROUP
            data-type: json
            rule-type: flow
Data ID:前面的服务名称cloudalibaba-sentinel-service
配置格式:json

[
    {
         "resource": "/testA",
         "limitApp": "default",
         "grade":   1,
         "count":   1,
         "strategy": 0,
         "controlBehavior": 0,
         "clusterMode": false    
    }
]
resource:资源名称
limitApp:来源应用
grade:阈值类型 0线程数 1QPS
count:单机阈值
strategy:流控模式 0直接 1关联 2链路
controlBehavior:流控效果 0快速失败 1Warm Up 2排队等待
clusterMode:是否集群