Section 12: Sentinel Getting Started Installation + Current Limiting Rules

Get into the habit of writing together! This is the 14th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event .

1. What is Sentinel

Avalanche problem:

When service B, which service A depends on, fails, requests in service A keep waiting, resulting in a high number of connections in service A, which may eventually cause service A to have problems, and then services that depend on service A also have problems, causing the entire micro There is a problem with the service cluster.

Solution:

  • Add a timeout to not wait all the time
  • Bulkhead mode: Limit the number of threads used by each service to prevent a service from exhausting all resources. However, it will result in a waste of resources
  • Fusing downgrade: The abnormal proportion of business execution is counted by the circuit breaker . If the threshold is exceeded, the business will be blown and all requests to access the business will be intercepted.
  • Flow control: limit the QPS of business access to avoid service failure due to sudden increase in traffic.

Sentinel mainly implements bulkhead mode, fuse downgrade and current limiting.

2. Technical comparison

** ** Sentinel Hystrix
Quarantine Policy Semaphore isolation Thread pool isolation/semaphore isolation
circuit breaker downgrade strategy Based on slow call ratio or abnormal ratio based on failure rate
Real-time indicator implementation sliding window Sliding window (based on RxJava)
Rule configuration Supports multiple data sources Supports multiple data sources
Extensibility multiple extension points form of plugin
Annotation-based support support support
Limiting Based on QPS, support current limit based on call relationship limited support
traffic shaping Support slow start, uniform queuing mode not support
System adaptive protection support not support
console Out of the box, you can configure rules, view second-level monitoring, machine discovery, etc. imperfect
Adaptation of common frameworks Servlet、Spring Cloud、Dubbo、gRPC 等 Servlet、Spring Cloud Netflix

Thread pool isolation: each service has a thread pool

Semaphore isolation: a thread pool that counts the number of threads used by each business.

3. Get to know Sentinel

Sentinel is a microservice traffic control component open sourced by Alibaba. Official website address: sentinelguard.io/zh-cn/index…

Sentinel has the following characteristics:

  • 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
  • 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
  • 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
  • 完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。

4、安装Sentinel

sentinel官方提供了UI控制台,方便我们对系统做限流设置。大家可以在GitHub下载。github.com/alibaba/Sen…

4.1 获取 Sentinel 控制台

您可以从 release 页面 下载最新版本的控制台 jar 包。

您也可以从最新版本的源码自行构建 Sentinel 控制台:

  • 下载 控制台 工程
  • 使用以下命令将代码打包成一个 fat jar: mvn clean package

4.2 启动

注意:启动 Sentinel 控制台需要 JDK 版本为 1.8 及以上版本。

使用如下命令启动控制台:

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
复制代码

其中 -Dserver.port=8080 用于指定 Sentinel 控制台端口为 8080

从 Sentinel 1.6.0 起,Sentinel 控制台引入基本的登录功能,默认用户名和密码都是 sentinel。可以参考 鉴权模块文档 配置用户名和密码。

注:若您的应用为 Spring Boot 或 Spring Cloud 应用,您可以通过 Spring 配置文件来指定配置,详情请参考 Spring Cloud Alibaba Sentinel 文档

配置项 默认值 说明
server.port 8080 服务端口
sentinel.dashboard.auth.username sentinel 默认用户名
sentinel.dashboard.auth.password sentinel 默认密码

5 客户端接入控制台

控制台启动后,客户端需要按照以下步骤接入到控制台。

客户端需要引入 Transport 模块来与 Sentinel 控制台进行通信。您可以通过 pom.xml 引入 JAR 包:

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

添加配置:

spring:
    cloud:
        sentinel:
          transport:
              dashboard: 192.168.0.105:9999
复制代码

访问微服务的任意端点,触发sentinel监控

6、限流规则

6.1、簇点链路

就是项目内的调用链路,链路中被监控的每个接口就是一个资源。默认情况下sentinel会监控SpringMVC的每一个端点(Endpoint),因此SpringMVC的每一个端点(Endpoint)就是调用链路中的一个资源。

流控、熔断等都是针对簇点链路中的资源来设置的,因此我们可以点击对应资源后面的按钮来设置规则:

Screenshot 2022-04-17 3.17.05 pm

击资源/order/{orderId}后面的流控按钮,就可以弹出表单。表单中可以添加流控规则,如下图所示:

Screenshot 2022-04-17 3.18.25 pm

来源:default对于所有的请求生效

其含义是限制 /order/{orderId}这个资源的单机QPS为xx,即每秒只允许n次请求,超出的请求会被拦截并报错。

6.2、流控模式

在添加限流规则时,点击高级选项,可以选择三种流控模式:

  • 直接:统计当前资源的请求,触发阈值时对当前资源直接限流,也是默认的模式
  • 关联:统计与当前资源相关的另一个资源,触发阈值时,对当前资源限流
  • 链路:统计从指定链路访问到本资源的请求,触发阈值时,对指定链路限流

6.3、流控模式-关联

关联模式:统计与当前资源相关的另一个资源,触发阈值时,对当前资源限流

使用场景:比如用户支付时需要修改订单状态,同时用户要查询订单。查询和修改操作会争抢数据库锁,产生竞争。业务需求是优先支付和更新订单的业务,因此当修改订单业务触发阈值时,需要对查询订单业务限流。

1、ordercontroller新增2个资源

    @GetMapping("query")
    public String queryOrder(){
        return "查询订单成功";
    }

    @GetMapping("update")
    public String updateOrder(){
        return "update ok";
    }
复制代码

2、query请求设置,当关联资源update超过5的时候,给query限流

Screenshot 2022-04-17 3.49.06 pm

3、使用Jemeter工具对order/update每秒发起10次调用,然后访问order/query。可以发现请求失败。

6.4、流控模式-链路

只针对从指定链路访问到本资源的请求做统计,判断是否超过阈值。

需求:有查询订单和创建订单业务,两者都需要查询商品。针对从查询订单进入到查询商品的请求统计,并设置限流。

步骤:

  1. 在OrderService中添加一个queryGoods方法,不用实现业务
  2. 在OrderController中,改造/order/query端点,调用OrderService中的queryGoods方法
  3. 在OrderController中添加一个/order/save的端点,调用OrderService的queryGoods方法
  4. 给queryGoods设置限流规则,从/order/query进入queryGoods的方法限制QPS必须小于2
    @GetMapping("query")
    public String queryOrder(){
        orderService.queryGoods();
        log.info("查询订单");
        return "查询订单成功";
    }
    @GetMapping("save")
    public String saveOrder(){
        orderService.queryGoods();
        log.info("新增订单");
        return "save ok";
    }
    
    //service
    public void queryGoods(){
        System.out.println("查询商品");
    }
复制代码

Sentinel默认只标记Controller中的方法为资源,如果要标记其它方法,需要利用@SentinelResource注解,示例:

    @SentinelResource("goods")
    public void queryGoods(){
        System.out.println("查询商品");
    }
复制代码

Sentinel默认会将Controller方法做context整合,同一个controller作为同一个链路,导致链路模式的流控失效,需要修改application.yml,添加配置:

spring:
    cloud:
        sentinel:
            transport:
              dashboard: 192.168.0.105:9999
            web-context-unify: false #关闭context整合
复制代码

给goods添加流控,限制query来源的qps为2

Screenshot 2022-04-17 4.12.28 pm

6.4、流控效果

流控效果是指请求达到流控阈值时应该采取的措施,包括三种:

  • 快速失败:达到阈值后,新的请求会被立即拒绝并抛出FlowException异常。是默认的处理方式。
  • warm up:预热模式,对超出阈值的请求同样是拒绝并抛出异常。但这种模式阈值会动态变化,从一个较小值逐渐增加到最大阈值。
  • 排队等待:让所有的请求按照先后次序排队执行,两个请求的间隔不能小于指定时长

warm up

warm up也叫预热模式,是应对服务冷启动的一种方案。请求阈值初始值是 threshold / coldFactor,持续指定时长后,逐渐提高到threshold值。而coldFactor的默认值是3.

例如,我设置QPS的threshold为10,预热时间为5秒,那么初始阈值就是 10 / 3 ,也就是3,然后在5秒后逐渐增长到10.

排队等待

当请求超过QPS阈值时,快速失败和warm up 会拒绝新的请求并抛出异常。而排队等待则是让所有请求进入一个队列中,然后按照阈值允许的时间间隔依次执行。后来的请求必须等待前面执行完成,如果请求预期的等待时间超出最大时长,则会被拒绝。

例如:QPS = 5,意味着每200ms处理一个队列中的请求;timeout = 2000,意味着预期等待超过2000ms的请求会被拒绝并抛出异常

6.5、热点参数限流

之前的限流是统计访问某个资源的所有请求,判断是否超过QPS阈值。而热点参数限流是分别统计参数值相同的请求,判断是否超过QPS阈值。

热点参数限流对默认的SpringMVC资源无效

1、设置热点资源

    @SentinelResource("hot")
    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId,
            @RequestHeader(value = "X-Request-other", required = false) String other) {
        log.info("header is:{}", other);
        // 根据id查询订单并返回
        return orderService.queryOrderById(orderId);
    }
复制代码

2、重启服务后添加配置

Screenshot 2022-04-17 4.28.12 pm

Screenshot 2022-04-17 4.30.06 pm

Guess you like

Origin juejin.im/post/7087489335666147358