在实现限流和熔断时,每次重启应用都会导致Dashboard中的规则丢失,这显然是无法满足正常使用的。这时因为Sentinel默认将规则存储在内存中,因此客户端一旦重启,Sentinel的数据规则就会丢失。
Sentinel提供了4种持久化机制:
- 本地文件
- Apollo
- Nacos
- Zookeeper
那么这一节就利用Nacos
来将规则进行持久化处理,保证每次重启应用后都能保留设置的规则。
本文仅以接口限流为例,熔断的操作方式与限流无异,因此不做赘述。
使用Nacos存储限流规则
前提:启动Nacos
和Sentinel Dashboard
第一步:创建模块alibaba-sentinel-datasource-nacos
第二步:修改pom.xml
,引入Sentinel和Nacos存储拓展:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-learn</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>alibaba-sentinel-datasource-nacos</artifactId>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
第三步:修改application.properties
,添加配置信息:
spring.application.name=alibaba-sentinel-datasource-nacos
server.port=8003
# sentinel dashboard
spring.cloud.sentinel.transport.dashboard=localhost:9260
# sentinel datasource: nacos
spring.cloud.sentinel.datasource.ds.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.ds.nacos.username=nacos
spring.cloud.sentinel.datasource.ds.nacos.password=nacos
spring.cloud.sentinel.datasource.ds.nacos.data-id=${spring.application.name}-sentinel
spring.cloud.sentinel.datasource.ds.nacos.group-id=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds.nacos.data-type=json
spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow
其中:
spring.cloud.sentinel.transport.dashboard
:Sentinel Dashboard的访问地址spring.cloud.sentinel.datasource.ds.nacos.server-addr
: Nacos的访问地址spring.cloud.sentinel.datasource.ds.nacos.username
:Nacos的访问用户名spring.cloud.sentinel.datasource.ds.nacos.password
:Nacos的访问密码spring.cloud.sentinel.datasource.ds.nacos.group-id
:Nacos存储规则的Group
spring.cloud.sentinel.datasource.ds.nacos.data-id
:Nacos存储规则的Data ID
spring.cloud.sentinel.datasource.ds.nacos.data-type
:Nacos存储规格的配置格式spring.cloud.sentinel.datasource.ds.nacos.rule-type
: Nacos存储的规则类型
第四步:创建应用入口类,定义一个REST接口:
package com.demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@SpringBootApplication
public class AlibabaSentinelDatasourceNacosApplication {
public static void main(String[] args) {
SpringApplication.run(AlibabaSentinelDatasourceNacosApplication.class, args);
}
@RestController
static class EchoController {
@GetMapping("echo")
public String echo() {
return "echo controller";
}
}
}
第五步:进入Nacos控制台,创建限流规则,如下图:
其中:
Data ID
:alibaba-sentinel-datasource-nacos-sentinel
,与配置文件的spring.cloud.sentinel.datasource.ds.nacos.data-id
一致配置格式
:JSON
配置内容
:[
{
"resource": "/echo",
"limitApp": "default",
"grade": 1,
"count": 5,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
其中:
resource
:资源名,即限流规则的作用对象limitApp
:流控针对的调用来源,若为 default 则不区分调用来源grade
:限流阈值类型(QPS 或并发线程数);0
代表根据并发数量来限流,1
代表根据QPS来进行流量控制count
:限流阈值strategy
:调用关系限流策略controlBehavior
:流量控制效果(直接拒绝、Warm Up、匀速排队)clusterMode
:是否为集群模式
第六步:启动应用,可以看到控制台中输出了Nacos的记载信息:
2021-04-20 16:25:22.120 INFO 75095 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.cloud.sentinel-com.alibaba.cloud.sentinel.SentinelProperties' of type [com.alibaba.cloud.sentinel.SentinelProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-04-20 16:25:22.122 INFO 75095 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration' of type [com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
通过curl访问一次/echo
接口,打开Sentinel Dashboard,可以看到流控规则中出现了一条记录,如下图:
这时前往Nacos配置中心,修改限流阈值为3,点击发布,再刷新Sentinel Dashboard,可以看到流控规则的阈值自动刷新了。
修改完成后,使用Postman Collection Runner请求接口,检查限流有没有生效: