nacos(九):sentinel——规则持久化
接上回,sentinel基本使用我们已经掌握。但是在设置限流规则时,会发现规则都是临时的,一段时间没访问资源或者重启sentinel,规则就会消失。所以,我们需要有一个将规则持久化保存的地方,让规则一直生效。
关于sentinel的规则持久化,我们一般会关注两个方面:
1) 在sentinel设置规则的时候,自动将规则同步到外部的存储设备;
2)sentinel启动的时候,如何自动拉取外部存储设备中的规则;
关于第1点,在官方版本中(截止当前为止1.8.8)都未进行支持。如果需要实现自动推送,则需要修改源码,不在今天的讨论范围中;
第2点才是我们今天讨论的重点。
步骤一:在nacos中添加规则
1)在nacos中新建一个命名空间,该命名空间专门用于存放sentinel的规则,如下图:

注意:这一步是必须的。因为经过多次尝试,放在public命名空间下,无法将规则同步到sentinel。
2)在新建的命名空间下,添加DataID为sentinel-flow-rules的配置项
该配置项中,即为流控规则,内容如下:
[ { "resource": "/hello", "limitApp": "default", "grade": 1, "count": 2, "clusterMode": false, "controlBehavior": 0, "strategy": 0 }, { "resource": "/api", "limitApp": "default", "grade": 1, "count": 2, "clusterMode": false, "controlBehavior": 0, "strategy": 0 } ]
添加完成后,效果如下图所示:

步骤二:在项目中进行配置
当前仍是在多模块测试项目(详情点击查看)的基本上进行扩展。
1) 在pom.xml中添加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.example</groupId> <artifactId>multimod_nacos</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>producer</artifactId> <packaging>jar</packaging> <dependencies> <!--springboot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 服务发现(生产者) --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--sentinel --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <configuration> <mainClass>com.example.producer.ProducerApplication</mainClass> <skip>false</skip> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
2) 配置application.yml
server: port: 8085 spring: cloud: nacos: discovery: server-addr: 127.0.0.1:8848 username: nacos password: nacos config: server-addr: 127.0.0.1:8848 username: nacos password: nacos namespace: public loadbalancer: nacos: enabled: true sentinel: filter: enabled: true transport: port: 8719 dashboard: 127.0.0.1:8080 datasource: flow: nacos: server-addr: 127.0.0.1:8848 username: nacos password: nacos namespace: 86c02680-8d68-478a-b263-524726121545 #不能放在public命名空间中 dataId: sentinel-flow-rules groupId: DEFAULT_GROUP data-type: json rule-type: flow eager: true #主动全量加载,如果不设置,默认懒加载 application: name: product1
其中:
namespace与dataId要与nacos中命名空间ID和配置项的dataId一致;
rule-type:规则的类型,所有类型的流控规则见文后附录;
eager:该参数如果设置为true,则项目启动后会立即全量加载流控规则。
3) 配置完成,启动项目
启动项目后,进入sentinel官理面版,在流控规则下即可看到生效的两条规则,如下图:

备注:在nacos中修改规则后,规则会立即同步到sentinel。所以开发过程中,尽量做好约定,如果里要修改规则,统一在nacos中进行修改。
附录:各种类型规则说明
1) 流控规则
[ { // 资源名 "resource": "/test", // 针对来源,若为 default 则不区分调用来源 "limitApp": "default", // 限流阈值类型(1:QPS;0:并发线程数) "grade": 1, // 阈值 "count": 1, // 是否是集群模式 "clusterMode": false, // 流控效果(0:快速失败;1:Warm Up(预热模式);2:排队等待) "controlBehavior": 0, // 流控模式(0:直接;1:关联;2:链路) "strategy": 0, // 预热时间(秒,预热模式需要此参数) "warmUpPeriodSec": 10, // 超时时间(排队等待模式需要此参数) "maxQueueingTimeMs": 500, // 关联资源、入口资源(关联、链路模式) "refResource": "/ref" } ]
2) 降级规则
[ { // 资源名 "resource": "/test1", "limitApp": "default", // 熔断策略(0:慢调用比例,1:异常比率,2:异常计数) "grade": 0, // 最大RT、比例阈值、异常数 "count": 200, // 慢调用比例阈值,仅慢调用比例模式有效(1.8.0 引入) "slowRatioThreshold": 0.2, // 最小请求数 "minRequestAmount": 5, // 当单位统计时长(类中默认1000) "statIntervalMs": 1000, // 熔断时长 "timeWindow": 10 } ]
3) 热点规则
[ { // 资源名 "resource": "/test1", // 限流模式(QPS 模式,不可更改) "grade": 1, // 参数索引 "paramIdx": 0, // 单机阈值 "count": 13, // 统计窗口时长 "durationInSec": 6, // 是否集群 默认false "clusterMode": 默认false, "burstCount": 0, // 集群模式配置 "clusterConfig": { "fallbackToLocalWhenFail": true, "flowId": 2, "sampleCount": 10, "thresholdType": 0, "windowIntervalMs": 1000 }, // 流控效果(支持快速失败和匀速排队模式) "controlBehavior": 0, "limitApp": "default", "maxQueueingTimeMs": 0, // 高级选项 "paramFlowItemList": [ { // 参数类型 "classType": "int", // 限流阈值 "count": 222, // 参数值 "object": "2" } ] }
4) 系统规则
[ { // RT "avgRt": 1, // CPU 使用率 "highestCpuUsage": -1, // LOAD "highestSystemLoad": -1, // 线程数 "maxThread": -1, // 入口 QPS "qps": -1 } ]
5) 授权规则
[ { // 资源名 "resource": "sentinel_spring_web_context", // 流控应用 "limitApp": "/test", // 授权类型(0代表白名单;1代表黑名单。) "strategy": 0 } ]
则对应以上各种类型规则的rule-type如下:
spring: cloud: sentinel: datasource: # 名称随意 flow: nacos: server-addr: localhost:8848 dataId: cloud-ali-flow-rules groupId: DEFAULT_GROUP # 规则类型,取值见: # org.springframework.cloud.alibaba.sentinel.datasource.RuleType rule-type: flow #流控规则 degrade: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-degrade-rules groupId: DEFAULT_GROUP rule-type: degrade #降级规则 system: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-system-rules groupId: DEFAULT_GROUP rule-type: system #系统规则 authority: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-authority-rules groupId: DEFAULT_GROUP rule-type: authority #授权规则 param-flow: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-param-flow-rules groupId: DEFAULT_GROUP rule-type: param-flow #热点规则
到这里sentinel的使用则告一段落,接下来的话题我们一起讨论dobbo的运用以及接入nacos的方法:)
浙公网安备 33010602011771号