• Sentinel 限流降级,Sentinel持久化


    Sentinel 限流降级,Sentinel持久化

    ================================

    ©Copyright 蕃薯耀 2021-04-01

    https://www.cnblogs.com/fanshuyao/

    一、Sentinel安装使用和配置,sentinel-dashboard安装使用和配置
    见:

    https://www.cnblogs.com/fanshuyao/p/14606396.html

    二、Sentinel 限流降级配置

    1、pom.xml引入依赖

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>

    2、application.properties配置文件增加sentinel配置:

    spring.cloud.sentinel.transport.port=8719
    spring.cloud.sentinel.transport.dashboard=localhost:8070
    spring.cloud.sentinel.log.dir=C:/logs/sentinel-web


    3、Sentinel简单限流(url匹配限流)

    复制代码
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import com.lqy.springCloud.Result;
    
    @RestController
    @RequestMapping("/")
    public class SentinelController {
    
        @Value("${server.port}")
        private String serverPort;
        
        @RequestMapping(value="/sentinel", produces = MediaType.APPLICATION_JSON_VALUE)
        public Result sentinel() {
            return Result.ok(serverPort);
        }
    }
    复制代码

    浏览器打开:

    http://localhost:8070/

    在Sentinel dashboard配置/sentinel的限流规则,每秒只能访问一次,超出访问,就会限流,返回默认的提示:

    Blocked by Sentinel (flow limiting)

    打开浏览器频繁访问(1秒内必须超过一次请求):

    http://127.0.0.1:8901/sentinel

    4、@SentinelResource注解使用

    复制代码
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.alibaba.csp.sentinel.annotation.SentinelResource;
    import com.alibaba.csp.sentinel.slots.block.BlockException;
    import com.lqy.springCloud.Result;
    
    @RestController
    @RequestMapping("/")
    public class SentinelController {
    
        @Value("${server.port}")
        private String serverPort;
        
        //blockHandler 函数会在原方法被限流/降级/系统保护的时候调用,而 fallback 函数会针对所有类型的异常。
        @RequestMapping(value="/limit", produces = MediaType.APPLICATION_JSON_VALUE)
        @SentinelResource(value="limit", blockHandler = "limitBlockHandler")
        public Result limit() {
            return Result.ok(serverPort);
        }
        
        public Result limitBlockHandler(BlockException be) {
            return Result.fail(serverPort);
        }
    }
    复制代码

    @SentinelResource中,value配置的是资源名,不带有/,blockHandler配置的是限流回调的方法

    @SentinelResource不生效:
    出现一个@SentinelResource不生效的情况,
    原因是:在Sentinel dashboard同时配置了/limit和limit两个限流规划,导致limit不生效,blockHandler不被回调。

    blockHandler 函数访问范围需要是 public返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 BlockException
    blockHandler 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 blockHandlerClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。

    5、Sentinel fallback异常处理

    复制代码
    import org.apache.commons.lang.StringUtils;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.alibaba.csp.sentinel.annotation.SentinelResource;
    import com.alibaba.csp.sentinel.slots.block.BlockException;
    import com.lqy.springCloud.Result;
    
    @RestController
    @RequestMapping("/")
    public class SentinelController {
    
        //blockHandler 函数会在原方法被限流/降级/系统保护的时候调用,而 fallback 函数会针对所有类型的异常。
        @RequestMapping(value="/fallback", produces = MediaType.APPLICATION_JSON_VALUE)
        @SentinelResource(value="fallback", blockHandler = "limitBlockHandler", fallback = "failFallback")
        public Result fail(String a) {
            if(StringUtils.isBlank(a)) {
                throw new RuntimeException("参数错误");
            }
            return Result.ok(null, a);
        }
        
        
        //参数要一致,不然fallback匹配不到,后面可以多一个Throwable参数
        public Result failFallback(String a, Throwable t) {
            return Result.failMsg("出现异常:" + serverPort);
        }
    
    }
    复制代码

    正常结果:

    复制代码
    // http://127.0.0.1:8901/fallback?a=11
    
    {
      "result": true,
      "timestamp": "2021-03-30 15:22:10",
      "msg": "操作成功。",
      "datas": "11"
    }
    复制代码

    Fallback结果:

    复制代码
    // http://127.0.0.1:8901/fallback
    
    {
      "result": false,
      "timestamp": "2021-03-30 15:21:25",
      "msg": "出现异常:8901",
      "datas": null
    }
    复制代码

    fallback 函数名称,可选项,用于在抛出异常的时候提供 fallback 处理逻辑。fallback 函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。

    fallback 函数签名和位置要求:
    返回值类型必须与原函数返回值类型一致;
    方法参数列表需要和原函数一致,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。
    fallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。

    注:1.6.0 之前的版本 fallback 函数只针对降级异常(DegradeException)进行处理,不能针对业务异常进行处理。

    若 blockHandler 和 fallback 都进行了配置,则被限流降级而抛出 BlockException 时只会进入 blockHandler 处理逻辑。

    6、同时配置blockHandler和fallback

    若 blockHandler 和 fallback 都进行了配置,则被限流降级而抛出 BlockException 时只会进入 blockHandler 处理逻辑。
    当配置了blockHandler,但blockHandler没有匹配上的时候(如参数不一致),会使用fallback异常处理返回

    复制代码
    import org.apache.commons.lang.StringUtils;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.alibaba.csp.sentinel.annotation.SentinelResource;
    import com.alibaba.csp.sentinel.slots.block.BlockException;
    import com.lqy.springCloud.Result;
    
    @RestController
    @RequestMapping("/")
    public class SentinelController {
        
        //blockHandler 函数会在原方法被限流/降级/系统保护的时候调用,而 fallback 函数会针对所有类型的异常。
        @RequestMapping(value="/fallback", produces = MediaType.APPLICATION_JSON_VALUE)
        @SentinelResource(value="fallback", blockHandler = "failBlockHandler", fallback = "failFallback")
        public Result fail(String a) {
            if(StringUtils.isBlank(a)) {
                throw new RuntimeException("参数错误");
            }
            return Result.ok(null, a);
        }
        
        
        public Result failBlockHandler(String a, BlockException be) {
            return Result.fail("failBlockHandler:" + serverPort);
        }
        
        
        //参数要一致,不然fallback匹配不到,后面可以多一个Throwable参数
        //当配置了blockHandler,但blockHandler没有匹配上的时候,会使用fallback异常处理返回
        public Result failFallback(String a, Throwable t) {
            return Result.failMsg("出现异常:" + serverPort);
        }
    
    }
    复制代码
    复制代码
    // http://127.0.0.1:8901/fallback?a=11
    
    {
    "result": false,
    "timestamp": "2021-03-30 15:27:05",
    "msg": "操作失败!",
    "datas": "failBlockHandler:8901"
    }
    复制代码

    7、Sentinel自定义blockHandlerClass和自定义fallbackClass

    复制代码
    import com.alibaba.csp.sentinel.slots.block.BlockException;
    import com.lqy.springCloud.Result;
    
    /**
     * 自定义BlockHandler
     * 方法必需为 static 方法,否则无法解析
     *
     */
    public class MyBlockHandler {
    
        /**
         * 参数一定要和方法对应,否则无法匹配到
         * @param a 
         * @param be BlockException
         * @return
         */
        public static Result blockHandler(String a, BlockException be) {
            return Result.fail("访问频繁,请稍候再试");
        }
        
        
        /**
         * 参数一定要和方法对应,否则无法匹配到
         * @param a 
         * @param t Throwable
         * @return
         */
        public static Result exceptionHandler(String a, Throwable t) {
            return Result.fail("服务暂时不可用,请稍候再试");
        }
    }
    复制代码

    自定义blockHandlerClass和fallbackClass

    复制代码
    import org.apache.commons.lang.StringUtils;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.alibaba.csp.sentinel.annotation.SentinelResource;
    import com.alibaba.csp.sentinel.slots.block.BlockException;
    import com.lqy.springCloud.Result;
    
    
    @RestController
    @RequestMapping("/")
    public class SentinelController {
    
        @Value("${server.port}")
        private String serverPort;
    
        //自定义blockHandlerClass和fallbackClass
        //注意对应的函数必需为 static 函数,否则无法解析。
        @RequestMapping(value="/myBlockHandler", produces = MediaType.APPLICATION_JSON_VALUE)
        @SentinelResource(value="myBlockHandler", blockHandlerClass = MyBlockHandler.class, blockHandler = "blockHandler", 
            fallbackClass = MyBlockHandler.class, fallback = "exceptionHandler")
        public Result myBlockHandler(String a) {
            if(StringUtils.isBlank(a)) {
                throw new RuntimeException("参数错误");
            }
            return Result.ok(null, a);
        } 
    }
    复制代码

    测试结果:

    复制代码
    // http://127.0.0.1:8901/myBlockHandler?a=11
    
    {
      "result": false,
      "timestamp": "2021-03-30 15:56:03",
      "msg": "操作失败!",
      "datas": "访问频繁,请稍候再试"
    }
    
    
    // http://127.0.0.1:8901/myBlockHandler
    
    {
      "result": false,
      "timestamp": "2021-03-30 15:57:28",
      "msg": "操作失败!",
      "datas": "服务暂时不可用,请稍候再试"
    }
    复制代码

    8、Sentinel exceptionsToIgnore:
    (since 1.6.0),用于指定哪些异常被排除掉,不会计入异常统计中,也不会进入 fallback 逻辑中,而是会原样抛出。

    9、Sentinel全局限流降级结果返回

    实现BlockExceptionHandler接口,实现handle方法。
    旧版本的sentinel是实现UrlBlockHandler接口,新版本(1.8版本)是没有这个接口的。
    默认的结果处理类是:

    com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler
    复制代码
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Component;
    
    import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
    import com.alibaba.csp.sentinel.slots.block.BlockException;
    import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
    import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
    import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
    import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
    import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
    import com.lqy.springCloud.Result;
    
    import cn.hutool.json.JSONUtil;
    
    @Component
    public class SentinelExceptionHandler implements BlockExceptionHandler {
    
            @Override
            public void handle(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws Exception {
            
            String msg = null;
            
                if (ex instanceof FlowException) {
                    msg = "访问频繁,请稍候再试";
                    
                } else if (ex instanceof DegradeException) {
                    msg = "系统降级";
                    
                } else if (ex instanceof ParamFlowException) {
                    msg = "热点参数限流";
                    
                } else if (ex instanceof SystemBlockException) {
                    msg = "系统规则限流或降级";
                    
                } else if (ex instanceof AuthorityException) {
                    msg = "授权规则不通过";
                    
                } else {
                    msg = "未知限流降级";
                }
                // http状态码
                response.setStatus(500);
                response.setCharacterEncoding("utf-8");
                response.setHeader("Content-Type", "application/json;charset=utf-8");
                response.setContentType("application/json;charset=utf-8");
                
                response.getWriter().write(JSONUtil.toJsonStr(Result.failMsg(msg)));
            }
    
    }
    复制代码

    三、Sentinel持久化配置
    1、pom.xml引入依赖

    <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-datasource-nacos</artifactId>
    </dependency>

    2、sentinel结合Nacos持久化配置
    sentinel持久化支持的类型有,详情见:

    com.alibaba.cloud.sentinel.datasource.config.DataSourcePropertiesConfiguration

    private FileDataSourceProperties file;
    private NacosDataSourceProperties nacos;
    private ZookeeperDataSourceProperties zk;
    private ApolloDataSourceProperties apollo;
    private RedisDataSourceProperties redis;
    private ConsulDataSourceProperties consul;

    sentinel结合Nacos持久化配置:

    复制代码
    #sentinel结合Nacos持久化配置
    #配置的属性见:com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties
    spring.cloud.sentinel.datasource.ds1.nacos.server-addr=http://127.0.0.1:8848
    spring.cloud.sentinel.datasource.ds1.nacos.data-id=sentinel-config
    #//group-id默认是:DEFAULT_GROUP
    spring.cloud.sentinel.datasource.ds1.nacos.group-id=SENTINEL_GROUP
    #RuleType类型见:com.alibaba.cloud.sentinel.datasource.RuleType
    #data-type这个属性没有提示,和Nacos配置的类型保持一致,配置在:com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties
    spring.cloud.sentinel.datasource.ds1.nacos.data-type=json
    #authority(授权规则)、degrade(降级规则)、flow(流控规则)、param(热点规则)、system(系统规则)五种规则持久化到Nacos中。 另外还增加网关的两个(api分组,限流)
    #rule-type这个属性没有提示,为空时,会报空指针错误
    spring.cloud.sentinel.datasource.ds1.nacos.rule-type=flow
    复制代码

    每种数据源都有两个共同的配置项: data-type、 converter-class 以及 rule-type,配置在:

    com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties

    data-type 配置项表示 Converter 类型,Spring Cloud Alibaba Sentinel 默认提供两种内置的值,分别是 json 和 xml (不填默认是json)。

    如果不想使用内置的 json 或 xml 这两种 Converter,可以填写 custom 表示自定义 Converter,然后再配置 converter-class 配置项,该配置项需要写类的全路径名(比如 spring.cloud.sentinel.datasource.ds1.file.converter-class=com.alibaba.cloud.examples.JsonFlowRuleListConverter)。
    rule-type 配置表示该数据源中的规则属于哪种类型的规则(flow,degrade,authority,system, param-flow, gw-flow, gw-api-group)。

    注意:
    当某个数据源规则信息加载失败的情况下,不会影响应用的启动,会在日志中打印出错误信息。
    默认情况下,xml 格式是不支持的。需要添加 jackson-dataformat-xml 依赖后才会自动生效。
    如果规则加载没有生效,有可能是 jdk 版本导致的,请关注 759 issue 的处理。

    sentinel缺少spring.cloud.sentinel.datasource.ds1.nacos.rule-type报错:DataSource ds1 build error: null

    复制代码
    2021-03-31 11:20:59.718 ERROR 12168 --- [ restartedMain] c.a.c.s.c.SentinelDataSourceHandler : [Sentinel Starter] DataSource ds1 build error: null
    java.lang.NullPointerException: null
    at com.alibaba.cloud.sentinel.custom.SentinelDataSourceHandler.lambda$registerBean$3(SentinelDataSourceHandler.java:185) ~[spring-cloud-starter-alibaba-sentinel-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at java.util.HashMap.forEach(HashMap.java:1289) ~[na:1.8.0_241]
    at com.alibaba.cloud.sentinel.custom.SentinelDataSourceHandler.registerBean(SentinelDataSourceHandler.java:128) ~[spring-cloud-starter-alibaba-sentinel-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at com.alibaba.cloud.sentinel.custom.SentinelDataSourceHandler.lambda$afterSingletonsInstantiated$0(SentinelDataSourceHandler.java:93) ~[spring-cloud-starter-alibaba-sentinel-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at java.util.TreeMap.forEach(TreeMap.java:1005) ~[na:1.8.0_241]
    at com.alibaba.cloud.sentinel.custom.SentinelDataSourceHandler.afterSingletonsInstantiated(SentinelDataSourceHandler.java:80) ~[spring-cloud-starter-alibaba-sentinel-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    复制代码

    原因就是com.alibaba.cloud.sentinel.custom.SentinelDataSourceHandler.registerBean(AbstractDataSourceProperties, String)中的dataSourceProperties.getRuleType()为空,所以配置文件中的spring.cloud.sentinel.datasource.ds1.nacos.rule-type不能为空

    // converter type now support xml or json.
    // The bean name of these converters wrapped by
    // 'sentinel-{converterType}-{ruleType}-converter'
    builder.addPropertyReference("converter", "sentinel-" + propertyValue.toString() + "-" + dataSourceProperties.getRuleType().getName() + "-converter");

    提示是没有rule-type属性的,可能是因为rule-type是继承过来的或者是开发工具的问题。

    rule-type在Sts提示没有该属性,但启动是没问题的。

    3、Nacos增加配置文件
    data-id名称为:sentinel-config
    group名称为:SENTINEL_GROUP
    配置格式:选择JSON
    配置内容如下:

    复制代码
    [
        {
            "resource": "myBlockHandler",
            "limitApp": "default",
            "grade": "1",
            "count": "1",
            "strategy": "0",
            "controlBehavior": "0",
            "clusterMode": false
        }
    ]
    复制代码

    【流控规则】配置项说明:
    resource:资源名
    limitApp:针对来源,若为 default 则不区分调用来源
    grade:阈值类型,0表示线程数,1表示QPS
    count:单机阈值
    strategy:流控模式,0表示直接,1表示关联,2表示链路
    controlBehavior:流控效果,0表示快速失败,1表示Warm Up,2表示排队等待
    clusterMode:是否集群

    配置多个规则:

    复制代码
    [
        {
            "resource": "myBlockHandler",
            "limitApp": "default",
            "grade": "1",
            "count": "1",
            "strategy": "0",
            "controlBehavior": "0",
            "clusterMode": false
        },
       {
            "resource": "flowLimit",
            "limitApp": "default",
            "grade": "1",
            "count": "1",
            "strategy": "0",
            "controlBehavior": "0",
            "clusterMode": false
        }
    ]
    复制代码

    Nacos配置文件发布后,会马上在Sentinel更新。

    4、测试Sentinel持久化
    sentinel是懒加载机制,只有当服务发起了请求,控制台才能看到并进行操作。
    浏览器打开链接:

    http://127.0.0.1:8901/myBlockHandler?a=11

    请求后,查看Sentinel是否已经加载配置。


    四、其它限流规则

    1、Sentinel熔断降级规则说明
    熔断降级规则(DegradeRule)包含下面几个重要的属性:

    复制代码
    Field            说明                                                                    默认值
    resource        资源名,即规则的作用对象    
    grade            熔断策略,支持慢调用比例/异常比例/异常数策略                                      慢调用比例
    count            慢调用比例模式下为慢调用临界 RT(超出该值计为慢调用);异常比例/异常数模式下为对应的阈值    
    timeWindow        熔断时长,单位为 s    
    minRequestAmount    熔断触发的最小请求数,请求数小于该值时即使异常比率超出阈值也不会熔断(1.7.0 引入)          5
    statIntervalMs        统计时长(单位为 ms),如 60*1000 代表分钟级(1.8.0 引入)                          1000 ms
    slowRatioThreshold    慢调用比例阈值,仅慢调用比例模式有效(1.8.0 引入)    
    复制代码

    2、Sentinel【热点规则】热点参数规则
    热点参数规则(ParamFlowRule)类似于流量控制规则(FlowRule):

    复制代码
    属性            说明                                                                              默认值
    resource        资源名,必填    
    count            限流阈值,必填    
    grade            限流模式                                                                          QPS 模式
    durationInSec        统计窗口时间长度(单位为秒),1.6.0 版本开始支持                                            1s
    controlBehavior    流控效果(支持快速失败和匀速排队模式),1.6.0 版本开始支持                                      快速失败
    maxQueueingTimeMs    最大排队等待时长(仅在匀速排队模式生效),1.6.0 版本开始支持                                    0ms
    paramIdx        热点参数的索引,必填,对应 SphU.entry(xxx, args) 中的参数索引位置    
    paramFlowItemList    参数例外项,可以针对指定的参数值单独设置限流阈值,不受前面 count 阈值的限制。仅支持基本类型和字符串类型    
    clusterMode        是否是集群参数流控规则                                                                false
    clusterConfig        集群流控相关配置
    复制代码

    3、Sentinel系统规则
    系统保护规则是从应用级别的入口流量进行控制,从单台机器的 load、CPU 使用率、平均 RT、入口 QPS 和并发线程数等几个维度监控应用指标,让系统尽可能跑在最大吞吐量的同时保证系统整体的稳定性。
    系统保护规则是应用整体维度的,而不是资源维度的,并且仅对入口流量生效。入口流量指的是进入应用的流量(EntryType.IN),比如 Web 服务或 Dubbo 服务端接收的请求,都属于入口流量。

    系统规则支持以下的模式:
    Load 自适应(仅对 Linux/Unix-like 机器生效):系统的 load1 作为启发指标,进行自适应系统保护。当系统 load1 超过设定的启发值,且系统当前的并发线程数超过估算的系统容量时才会触发系统保护(BBR 阶段)。系统容量由系统的 maxQps * minRt 估算得出。设定参考值一般是 CPU cores * 2.5。
    CPU usage(1.5.0+ 版本):当系统 CPU 使用率超过阈值即触发系统保护(取值范围 0.0-1.0),比较灵敏。
    平均 RT:当单台机器上所有入口流量的平均 RT 达到阈值即触发系统保护,单位是毫秒。
    并发线程数:当单台机器上所有入口流量的并发线程数达到阈值即触发系统保护。
    入口 QPS:当单台机器上所有入口流量的 QPS 达到阈值即触发系统保护。


    系统规则json文件配置(最简单配置),配置QPS为1:

    [
        {
            "qps":1.0
        }
    ]

    系统规则json文件配置,配置QPS为1(这段json配置是从新增那里获取的,能正常持久化):

    复制代码
    [
        {
            "id":12,
            "app":"SPRING-CLOUD-SENTINEL",
            "ip":"192.168.170.1",
            "port":8720,
            "highestSystemLoad":-1.0,
            "avgRt":-1,
            "maxThread":-1,
            "qps":1.0,
            "highestCpuUsage":-1.0,
            "gmtCreate":null,
            "gmtModified":null
        }
    ]
    复制代码

    highestSystemLoad:对应页面的Load
    avgRt:对应页面的RT
    maxThread:对应页面的线程数
    qps:对应入口QPS
    highestCpuUsage:对应CUP使用率

    五、、Feign 支持
    Sentinel 适配了 Feign 组件。如果想使用,除了引入 spring-cloud-starter-alibaba-sentinel 的依赖外还需要 2 个步骤:

    配置文件打开 Sentinel 对 Feign 的支持:feign.sentinel.enabled=true

    加入 spring-cloud-starter-openfeign 依赖使 Sentinel starter 中的自动化配置类生效:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    六、RestTemplate 支持
    Spring Cloud Alibaba Sentinel 支持对 RestTemplate 的服务调用使用 Sentinel 进行保护,在构造 RestTemplate bean的时候需要加上 @SentinelRestTemplate 注解。

    @Bean
    @SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @SentinelRestTemplate 注解的属性支持限流(blockHandler, blockHandlerClass)和降级(fallback, fallbackClass)的处理。
    其中 blockHandler 或 fallback 属性对应的方法必须是对应 blockHandlerClass 或 fallbackClass 属性中的静态方法。

    注意:应用启动的时候会检查 @SentinelRestTemplate 注解对应的限流或降级方法是否存在,如不存在会抛出异常

    @SentinelRestTemplate 注解的限流(blockHandler, blockHandlerClass)和降级(fallback, fallbackClass)属性不强制填写。
    当使用 RestTemplate 调用被 Sentinel 熔断后,会返回 RestTemplate request block by sentinel 信息,或者也可以编写对应的方法自行处理返回信息。这里提供了 SentinelClientHttpResponse 用于构造返回信息。
    Sentinel RestTemplate 限流的资源规则提供两种粒度:
    httpmethod:schema://host:port/path:协议、主机、端口和路径
    httpmethod:schema://host:port:协议、主机和端口

    七、动态数据源支持
    SentinelProperties 内部提供了 TreeMap 类型的 datasource 属性用于配置数据源信息。

    比如配置 4 个数据源:

    复制代码
    spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.json
    spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
    
    #spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
    #spring.cloud.sentinel.datasource.ds1.file.data-type=custom
    #spring.cloud.sentinel.datasource.ds1.file.converter-class=com.alibaba.cloud.examples.JsonFlowRuleListConverter
    #spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
    
    spring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848
    spring.cloud.sentinel.datasource.ds2.nacos.data-id=sentinel
    spring.cloud.sentinel.datasource.ds2.nacos.group-id=DEFAULT_GROUP
    spring.cloud.sentinel.datasource.ds2.nacos.data-type=json
    spring.cloud.sentinel.datasource.ds2.nacos.rule-type=degrade
    
    spring.cloud.sentinel.datasource.ds3.zk.path = /Sentinel-Demo/SYSTEM-CODE-DEMO-FLOW
    spring.cloud.sentinel.datasource.ds3.zk.server-addr = localhost:2181
    spring.cloud.sentinel.datasource.ds3.zk.rule-type=authority
    
    spring.cloud.sentinel.datasource.ds4.apollo.namespace-name = application
    spring.cloud.sentinel.datasource.ds4.apollo.flow-rules-key = sentinel
    spring.cloud.sentinel.datasource.ds4.apollo.default-flow-rule-value = test
    spring.cloud.sentinel.datasource.ds4.apollo.rule-type=param-flow
    复制代码

    d1, ds2, ds3, ds4 是 ReadableDataSource 的名字,可随意编写。后面的 file ,zk ,nacos , apollo 就是对应具体的数据源,它们后面的配置就是这些具体数据源各自的配置。注意数据源的依赖要单独引入(比如 sentinel-datasource-nacos)。

    每种数据源都有两个共同的配置项: data-type、 converter-class 以及 rule-type。
    data-type 配置项表示 Converter 类型,Spring Cloud Alibaba Sentinel 默认提供两种内置的值,分别是 json 和 xml (不填默认是json)。
    如果不想使用内置的 json 或 xml 这两种 Converter,可以填写 custom 表示自定义 Converter,然后再配置 converter-class 配置项,该配置项需要写类的全路径名(比如 spring.cloud.sentinel.datasource.ds1.file.converter-class=com.alibaba.cloud.examples.JsonFlowRuleListConverter)。
    rule-type 配置表示该数据源中的规则属于哪种类型的规则(flow,degrade,authority,system, param-flow, gw-flow, gw-api-group)。

    八、Spring Cloud Gateway 支持

    若想跟 Sentinel Starter 配合使用,需要加上 spring-cloud-alibaba-sentinel-gateway 依赖,同时需要添加 spring-cloud-starter-gateway 依赖来让 spring-cloud-alibaba-sentinel-gateway 模块里的 Spring Cloud Gateway 自动化配置类生效:

    复制代码
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    复制代码

    同时请将 spring.cloud.sentinel.filter.enabled 配置项置为 false(若在网关流控控制台上看到了 URL 资源,就是此配置项没有置为 false)。

    九、Endpoint 支持
    在使用 Endpoint 特性之前需要在 Maven 中添加 spring-boot-starter-actuator 依赖,并在配置中允许 Endpoints 的访问。


    Spring Boot 1.x 中添加配置 management.security.enabled=false。暴露的 endpoint 路径为 /sentinel
    Spring Boot 2.x 中添加配置 management.endpoints.web.exposure.include=*。暴露的 endpoint 路径为 /actuator/sentinel


    Sentinel Endpoint 里暴露的信息非常有用。包括当前应用的所有规则信息、日志目录、当前实例的 IP,Sentinel Dashboard 地址,Block Page,应用与 Sentinel Dashboard 的心跳频率等等信息。

    十、Sentinel 、Hystrix、Resilience4j 功能对比

     

    (时间宝贵,分享不易,捐赠回馈,^_^)

    ================================

    ©Copyright 蕃薯耀 2021-04-01

    https://www.cnblogs.com/fanshuyao/

  • 相关阅读:
    elasticsearch(0.90.10)安装配置+超多插件!!
    【架构】基于Nutch+Hadoop+Hbase+ElasticSearch的网络爬虫及搜索引擎
    Elasticsearch(1.1.1)基础教程pdf
    hadoop1.2.1+hbase0.94.11+nutch2.2.1+elasticsearch0.90.5安装配置攻略
    安装和使用 Elasticsearch(1.1.1)+marvel插件、Kibana插件
    nutch2.1+mysql+elasticsearch整合linux单机部署
    Nutch 快速入门(Nutch 2.2.1+Hbase+Solr)
    Linux中VMware虚拟机增加磁盘空间的扩容操作
    java8使用stream的collect进行list转map注意事项
    Java 8 指南
  • 原文地址:https://www.cnblogs.com/ExMan/p/15876500.html
Copyright © 2020-2023  润新知