sentinel 集群限流

           

官网:https://sentinelguard.io/zh-cn/docs/cluster-flow-control.html

           

                 

                                     

集群限流

        

集群限流:一个服务在多个机器上部署,对服务服务的总体调用次数进行限制, 结合单机限流,可以更好地发挥限流效果

                 

Resilience4J集群限流_服务端

token client:集群流控客户端,向token server请求获取token

token server:集群限流服务端,根据集群限流规则判断是否向token client发放token


*************

token client

        

客户端依赖

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-cluster-client-default</artifactId>
    <version>1.8.3</version>
</dependency>

               

api 设置客户端(也可直接在sentinel dashbpard配置)

# 通过api将当前模式置为客户端模式
http://<ip>:<port>/setClusterMode?mode=<xxx>
mode=0:当前服务设置为客户端
mode=1:当前服务设置为服务端
mode=-1:关闭集群模式
 
# 配置客户端(ClusterClientConfig),除使用api配置外,也可使用sentinel dashboard配置
http://<ip>:<port>/cluster/client/modifyConfig?data=<config>
说明:config为json格式的clusterClientConfig

            

ClusterClientConfig

public class ClusterClientConfig {
    private Integer requestTimeout;

    public ClusterClientConfig() {
    }

    public Integer getRequestTimeout() {
        return this.requestTimeout;
    }

    public ClusterClientConfig setRequestTimeout(Integer requestTimeout) {
        this.requestTimeout = requestTimeout;
        return this;
    }

    public String toString() {
        return "ClusterClientConfig{requestTimeout=" + this.requestTimeout + '}';
    }
}

           

token client连接token server失败处理

# 连接远程服务端(token server)失败、或者通信失败,触发以下规则:
集群热点限流默认直接通过
普通集群限流会退化到local模式的限流
 
# token client连接server失败重试
连接意外断开时,token client会不断进行重试,每次重试的间隔时间以n*2000ms的形式递增

            

*************

token server

     

服务端依赖

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-cluster-server-default</artifactId>
    <version>1.8.3</version>
</dependency>

               

独立模式:token server独立部署,与应用实例分离,可直接创建ClusterTokenServer实例并启动

                 

Resilience4J集群限流_限流_02

             

嵌入模式:token server与应用实例同时工作,token client、token server可随时切换

                 

Resilience4J集群限流_限流_03

                    

api 设置节点类型

http://<ip>:<port>/setClusterMode?mode=<xxx>

mode可选值:
0:客户端
1:服务端
-1:关闭

           

服务端配置属性

namespace set:集群限流服务端服务的作用域(命名空间)
               token client连接到token server会上报自己的命名空间(默认为project.name),
               token server 会根据上报的命名空间名称统计连接数
transport config:通信相关配置,如server、port等
flow config:限流配置,如动窗口统计时长、格子数目、最大允许总QPS等

                   

Resilience4J集群限流_服务端_04

          

            

                                     

相关依赖

   

<!-- 服务注册与发现 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
 
        <!-- dubbo服务调用 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
 
        <!-- sentinel dubbo限流 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-apache-dubbo-adapter</artifactId>
        </dependency>
 
        <!-- 数据上传到sentinel控制台 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
        </dependency>

        <!-- 集群客户端、服务端 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-cluster-client-default</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-cluster-server-default</artifactId>
            <version>1.8.0</version>
        </dependency>

          

              

                                     

使用示例

    

***********

服务端

        

服务端部署3台服务,使用集群限流:token client(8081、8082)、token server:8083

                               

Resilience4J集群限流_Resilience4J集群限流_05

          

application.yml

spring:
  application:
    name: nacos-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

dubbo:
  #registry:
  # address: spring-cloud://localhost:8848
  protocol:
    name: dubbo
    port: -1

server:
  port: 8081

           

HelloService

public interface HelloService {
 
    String hello();
}

          

HelloServiceImpl

@DubboService
public class HelloServiceImpl implements HelloService {
 
    @Override
    public String hello() {
        return "hello";
    }
}

           

DemoApplication

@EnableDubbo  //开启dubbo
@SpringBootApplication
public class DemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
 
}

               

***********

消费端

    

                               

Resilience4J集群限流_sentinel_06

            

application.yml

spring:
  application:
    name: nacos-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

dubbo:
  protocol:
    name: dubbo
    port: -1

server:
  port: 9000

          

HelloService

public interface HelloService {
 
    String hello();
}

        

HelloController

@RestController
public class HelloController {
 
    @DubboReference
    private HelloService helloService;
 
    @RequestMapping("/hello")
    public String hello(){
        System.out.println(helloService.hello());
 
        return "success";
    }
}

             

DemoApplication

@EnableDubbo
@SpringBootApplication
public class DemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
 
}

           

***********

启动配置

   

sentinel控制台

java -Dserver.port=8000 -Dcsp.sentinel.dashboard.server=localhost:8000 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
 
参数说明:
-Dserver.port=8080:指定控制台启动端口
-Dcsp.sentinel.dashboard.server:指定控制台地址和端口
-Dproject.name=sentinel-dashboard:指定控制台项目名称

服务端:添加启动参数

-Dcsp.sentinel.dashboard.server=localhost:8000 -Dcsp.sentinel.api.port=6001 -Dcsp.sentinel.log.use.pid=true -Dproject.name=nacos-provider

          

服务端 2:添加启动参数

-Dcsp.sentinel.dashboard.server=localhost:8000 -Dcsp.sentinel.api.port=6002 -Dcsp.sentinel.log.use.pid=true -Dproject.name=nacos-provider

       

服务端 3:添加启动参数

-Dcsp.sentinel.dashboard.server=localhost:8000 -Dcsp.sentinel.api.port=6003 -Dcsp.sentinel.log.use.pid=true -Dproject.name=nacos-provider

        

消费端:添加启动参数

-Dcsp.sentinel.dashboard.server=localhost:8000 -Dcsp.sentinel.api.port=9200 -Dcsp.sentinel.log.use.pid=true -Dproject.name=nacos-consumer

             

***********

sentinel 配置

   

localhost:8000,查看sentinel控制台:

                   

Resilience4J集群限流_sentinel_07

             

配置token server、添加token client

                   

Resilience4J集群限流_客户端_08

                   

Resilience4J集群限流_服务端_09

                    

Resilience4J集群限流_服务端_10

                    

Resilience4J集群限流_限流_11

            

添加流控规则:每个集群中的节点都需要添加流控规则

                   

Resilience4J集群限流_Resilience4J集群限流_12

                   

Resilience4J集群限流_客户端_13

                   

Resilience4J集群限流_客户端_14

             

jmeter测试

                    

Resilience4J集群限流_Resilience4J集群限流_15

                   

Resilience4J集群限流_服务端_16

                   

Resilience4J集群限流_Resilience4J集群限流_17

                   

Resilience4J集群限流_Resilience4J集群限流_18