一、在生产环境中使用Sentinel
生产环境的Sentinel Dashboard需要具备下面几个特性:
1、规则管理及推送
一般来说,规则的推送有下面三种模式
推送模式 | 说明 | 优点 | 缺点 |
API 将规则推送至客户端并直接更新到内存中,扩展写数据源(WritableDataSource) | 简单,无任何依赖 | 不保证一致性;规则保存在内存中,重启即消失。严重不建议用于生产环境 | |
扩展写数据源(WritableDataSource), 客户端主动向某个规则管理中心定期轮询拉取规则,这个规则中心可以是 RDBMS、文件 等 | 简单,无任何依赖;规则持久化 | 不保证一致性;实时性不保证,拉取过于频繁也可能会有性能问题。 | |
扩展读数据源(ReadableDataSource),规则中心统一推送,客户端通过注册监听器的方式时刻监听变化,比如使用 Nacos、Zookeeper 等配置中心。这种方式有更好的实时性和一致性保证。生产环境下一般采用 push 模式的数据源。 | 规则持久化;一致性;快速 | 引入第三方依赖 |
2、动态规则扩展
Sentinel的理念是开发者只需要关注资源的定义,当资源定义成功后可以动态增加各种流控降级规则。
Sentinel提供两种方式修改规则:
- 通过API直接修改(loadRules)——手动修改
- 通过 DataSource 适配不同数据源修改——动态规则
手动通过API修改比较直观,可以通过以下几个API修改不同的规则:
FlowRuleManager.loadRules(List<FlowRule> rules); // 修改流控规则
DegradeRuleManager.loadRules(List<DegradeRule> rules); // 修改降级规则
手动修改规则(硬编码方式)一般仅用于测试和演示,生产上一般通过动态规则源的方式来动态管理规则。示例代码如下所示:
//TODO 发布限流规则方式一:手动通过API修改
final String rule = "[\n"
+ " {\n"
+ " \"resource\": \"login\",\n"
+ " \"controlBehavior\": 0,\n"
+ " \"count\": 5.0,\n"
+ " \"grade\": 1,\n"
+ " \"limitApp\": \"default\",\n"
+ " \"strategy\": 0\n"
+ " }\n"
+ "]";
ConfigService configService = NacosFactory.createConfigService(serverAddr);
boolean isPublishOk = configService.publishConfig(dataId, group, rule);
System.out.println(isPublishOk);
上述 loadRules() 方法只接受内存态的规则对象,但更多时候规则存储在文件、数据库或者配置中心当中。DataSource 接口给我们提供了对接任意配置源的能力。相比直接通过API 修改规则,实现 DataSource 接口是更加可靠的做法。
我们推荐通过控制台设置规则后将规则推送到统一的规则中心,客户端实现 ReadableDataSource 接口端监听规则中心实时获取变更,流程如下:
二、DataSource扩展方式介绍
1、DataSource 扩展常见的实现方式有:
拉模式:客户端主动向某个规则管理中心定期轮询拉取规则,这个规则中心可以是RDBMS、文件,甚至是VCS等。这样做的方式是简单,缺点是无法及时获取变更;
推模式:规则中心统一推送,客户端通过注册监听器的方式时刻监听变化,比如使用 Nacos、Zookeeper 等配置中心。这种方式有更好的实时性和一致性保证。
Sentinel目前支持以下数据源扩展:
1.1、拉模式拓展
实现拉模式的数据源最简单的方式是继承AutoRefreshDataSource抽象类,然后实现readSource()方法,在该方法里从指定数据源读取字符串格式的配置数据。比如 基于文件的数据源。
1.2、推模式拓展
实现推模式的数据源最简单的方式是继承AbstractDataSource抽象类,在其构造方法中添加监听器,并实现readSource()从指定数据源读取字符串格式的配置数据。比如基于Nacos的数据源NacosDataSource。
com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource#NacosDataSource(java.lang.String, java.lang.String, java.lang.String, com.alibaba.csp.sentinel.datasource.Converter<java.lang.String,T>)
控制台通常需要做一些改造来直接推送应用维度的规则到配置中心。功能示例可以参考AHAS Sentinel控制台的规则推送功能。改造指南可以参考 在生产环境中使用 Sentinel控制台。
通常需要调用以下方法将数据源注册至指定的规则管理器中:
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId, parser);
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
若不希望手动注册数据源,可以借助 Sentinel 的 InitFunc SPI 扩展接口。只需要实现自己的 InitFunc接口,在 init 方法中编写注册数据源的逻辑。
public class DataSourceInitFunc implements InitFunc {
@Override
public void init() throws Exception {
final String remoteAddress = "localhost";
final String groupId = "Sentinel:Demo";
final String dataId = "com.alibaba.csp.sentinel.demo.flow.rule";
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(
remoteAddress, groupId, dataId,
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
}
}
接着将对应的类名添加到位于资源目录(通常是resource目录)下的 META-INF/services 目录下的 com.alibaba.csp.sentinel.init.InitFunc 文件中,比如:com.test.init.DataSourceInitFunc
这样,当初次访问任意资源的时候,Sentinel 就可以自动去注册对应的数据源了。
2、推模式:使用Nacos配置规则
Nacos 是阿里中间件团队开源的服务发现和动态配置中心。Sentinel针对Nacos作了适配,底层可以采用Nacos作为规则配置数据源。使用时只需添加以下依赖:
<!-- 引入Spring Cloud Alibaba的Sentinel模块和Nacos存储扩展: -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.7.1</version>
</dependency>
然后创建 NacosDataSource 并将其注册至对应的 RuleManager 上即可。比如:
// remoteAddress 代表 Nacos 服务端的地址, groupId 和 dataId 对应 Nacos 中相应配置
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
3、推模式:使用ZooKeeper配置规则
Sentinel 针对 ZooKeeper 作了相应适配,底层可以采用 ZooKeeper 作为规则配置数据源。使用时只需添加以下依赖:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-zookeeper</artifactId>
<version>1.7.1</version>
</dependency>
然后创建 ZookeeperDataSource 并将其注册至对应的RuleManager上即可。比如:
// remoteAddress 代表 ZooKeeper 服务端的地址, path 对应 ZK 中的数据路径
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(
remoteAddress, path, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
- Sentinel使用Nacos存储规则
1、读取Nacos配置文件实现规则存储
本文我们就来一起动手尝试一下,使用Spring Cloud Alibaba的中整合的配置中心Nacos存储限流规则。
1.1、准备工作
下面我们将同时使用到Nacos和Sentinel Dashboard,所以先把Nacos和Sentinel Dashboard启动起来。
1.2、应用配置
第一步:在Spring Cloud应用的pom.xml中引入Spring Cloud Alibaba的Sentinel模块和Nacos存储扩展:
第一步:新建模块:springcloudalibaba-sentinel-nacos
第二步:添加pom依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 注意选择版本号 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 引入Spring Cloud Alibaba的Sentinel模块和Nacos存储扩展: -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.7.1</version>
</dependency>
</dependencies>
第三步:在Spring Cloud应用中添加配置信息:
spring:
application:
name: springcloudalibaba-sentinel-nacos
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8090
eager: true
datasource: # sentinel datasource nacos :http://blog.didispace.com/spring-cloud-alibaba-sentinel-2-1/
ds:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-sentinel
groupId: DEFAULT_GROUP
rule-type: flow
1.3、动态数据源支持
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
配置说明:
- spring.cloud.sentinel.transport.dashboard:sentinel dashboard的访问地址,根据上面准备工作中启动的实例配置。
- spring.cloud.sentinel.datasource.ds.nacos.server-addr:nacos的访问地址,根据上面准备工作中启动的实例配置。
- spring.cloud.sentinel.datasource.ds.nacos.groupId:nacos中存储规则的groupId。
- spring.cloud.sentinel.datasource.ds.nacos.dataId:nacos中存储规则的dataId。
- spring.cloud.sentinel.datasource.ds.nacos.rule-type:该参数是spring cloud alibaba升级到0.2.2之后增加的配置,用来定义存储的规则类型。
所有的规则类型可查看枚举类:org.springframework.cloud.alibaba.sentinel.datasource.RuleType,每种规则的定义格式可以通过各枚举值中定义的规则对象来查看,比如限流规则可查看:com.alibaba.csp.sentinel.slots.block.flow.FlowRule。这里对于dataId使用了${spring.application.name}变量,这样可以根据应用名来区分不同的规则配置。
注意:
由于版本迭代关系,Github Wiki中的文档信息不一定适用所有版本。比如:在这里适用的0.2.1版本中,并没有spring.cloud.sentinel.datasource.ds2.nacos.rule-type这个参数。所以,读者在使用的时候,可以通过查看org.springframework.cloud.alibaba.sentinel.datasource.config.DataSourcePropertiesConfiguration和org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties两个类来分析具体的配置内容,会更为准确。
第四步:Nacos中创建限流规则的配置,比如:springcloudalibaba-sentinel-nacos.json
[
{
"resource": "/sentinelNacos/hello",
"limitApp": "default",
"grade": 1,
"count": 3,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
可以看到上面配置规则是一个数组类型,数组中的每个对象是针对每一个保护资源的配置对象,每个对象中的属性解释如下:
resource:资源名,即限流规则的作用对象
- limitApp:流控针对的调用来源,若为 default 则不区分调用来源
- grade:限流阈值类型(QPS 或并发线程数);0代表根据并发数量来限流,1代表根据QPS来进行流量控制
- count:限流阈值
- strategy:调用关系限流策略
- controlBehavior:流量控制效果(直接拒绝、Warm Up、匀速排队)
- clusterMode:是否为集群模式
第五步:启动应用测试
如果一些顺利,可以看到类似下面的日志,代表已经成功从Nacos加载了一条限流规则,此时在Sentinel Dashboard中就可以看到当前我们启动的alibaba-sentinel-datasource-nacos服务。点击左侧菜单中的流控规则,可以看到已经存在一条记录了,具体如下:
通过postman或者curl访问接口:http://localhost:8003/sentinelNacos/hello
在实时监控可以看到某个时间点访问的次数通过QPS为3,拒绝QPS为1
这条记录就是上面我们在Nacos中配置的限流规则。
1.4、备注
在完成了上面的整合之后,对于接口流控规则的修改就存在两个地方了:Sentinel控制台、Nacos控制台。这个时候,需要注意当前版本的Sentinel控制台不具备同步修改Nacos配置的能力,而Nacos由于可以通过在客户端中使用Listener来实现自动更新。所以,在整合了Nacos做规则存储之后,需要知道在下面两个地方修改存在不同的效果:
- Sentinel控制台中修改规则:仅存在于服务的内存中,不会修改Nacos中的配置值,重启后恢复原来的值。
- Nacos控制台修改规则:服务的内存中规则会更新,Nacos中持久化规则也会更新,重启后依然保持。
2、Dashboard修改流控规则同步到Nacos
代码工程:springcloudalibaba-sentinel-nacos
2.1、添加pom依赖
<dependencies>
<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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba.csp/sentinel-datasource-nacos -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.7.1</version>
</dependency>
</dependencies>
2.2、添加Nacos配置中心信息
spring:
cloud:
nacos:
config: # Nacos配置中心
server-addr: localhost:8848
group: DEFAULT_GROUP # 指定group
file-extension: JSON # 文件后缀,默认为properties
prefix: springcloud-nacos-config # 配置文件前缀
namespace: sentinel # 指定namespace=sentinel 默认为public
# 共享配置方式
# shared-configs: actuator.properties,log.properties
# refresh-enabled: true
# datasource: # sentinel datasource nacos :http://blog.didispace.com/spring-cloud-alibaba-sentinel-2-1/
# ds:
# nacos: # 使用Nacos存储限流规则
# server-addr: localhost:8848
# dataId: sentinel
# groupId: DEFAULT_GROUP
# rule-type: flow
2.3、添加测试代码
com.chj.util.SentinelWriteReadUtil
/**
* sentinel限流规则持久化
*/
@Component
public class SentinelWriteReadUtil {
@PostConstruct
public void init() throws NacosException {
System.out.println("-------------init-----------");
String serverAddr = "localhost";
String dataId = "springcloudalibaba-sentinel-nacos";
String group = "DEFAULT_GROUP";
//namespace
//TODO 从Nacos配置中心读取限流规则
// 创建 NacosDataSource 并将其注册至对应的 RuleManager 上
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(
serverAddr, group, dataId,
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
System.out.println(flowRuleDataSource.getProperty());
//TODO 发布限流规则方式一:手动通过API修改
// final String rule = "[\n"
// + " {\n"
// + " \"resource\": \"login\",\n"
// + " \"controlBehavior\": 0,\n"
// + " \"count\": 5.0,\n"
// + " \"grade\": 1,\n"
// + " \"limitApp\": \"default\",\n"
// + " \"strategy\": 0\n"
// + " }\n"
// + "]";
// ConfigService configService = NacosFactory.createConfigService(serverAddr);
// boolean isPublishOk = configService.publishConfig(dataId, group, rule);
// System.out.println(isPublishOk);
//TODO 发布限流规则方式二:自动写入
//TODO Sentinel Dashboard中修改规则同步到Nacos
WritableDataSource writableDataSource = new WriteableNacos<List<FlowRule>>();
WritableDataSourceRegistry.registerFlowDataSource(writableDataSource);
}
}
com.chj.util.WriteableNacos
/**
* 规则写入方法实现
*/
public class WriteableNacos<T> implements WritableDataSource<T> {
//当sentinel update 时候
@Override
public void write(T value) throws Exception {
String s = JSON.toJSONString(value);
System.out.println("-------------write-----------"+s);
String dataId = "springcloudalibaba-sentinel-nacos";
String group = "DEFAULT_GROUP";
// 发布配置
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, "localhost");
properties.put(PropertyKeyConst.NAMESPACE, "sentinel"); // 指定namespace配置
ConfigService configService = NacosFactory.createConfigService(properties);
boolean isPublishOk = configService.publishConfig(dataId, group, s);
System.out.println("isPublishOk========"+isPublishOk);
}
@Override
public void close() throws Exception {
}
}
2.4、Nacos配置中心添加配置文件springcloudalibaba-sentinel-nacos
注意新建namespace为sentinel的环境下面新建配置文件
2.5、启动项目测试
项目启动后,我们在sentinel的dashboard中针对hotlimit接口添加流控规则如下:
保存成功以后查看结果:
控制输出如下所示:
-------------write-----------
[{"clusterConfig":{"fallbackToLocalWhenFail":true,"sampleCount":10,"strategy":0,"thresholdType":0,"windowIntervalMs":1000},"clusterMode":false,"controlBehavior":0,"count":4.0,"grade":1,"limitApp":"default","maxQueueingTimeMs":500,"resource":"hotlimit","strategy":0,"warmUpPeriodSec":10}]
isPublishOk========true
查看Nacos配置中心namespace为sentinel下面的配置文件springcloudalibaba-sentinel-nacos内容:
到此我们的动态配置持久化功能已经实现。
注意:
目前该版本中只有流控规则可以实现持久化,其他方式配置都官方都没有提供。
3、实现其他规则持久化
3.1、修改微服务springcloudalibaba-sentinel-nacos配置
bootstrap.yml添加配置如下所示:
spring:
cloud:
nacos:
config: # Nacos配置中心
server-addr: localhost:8848
sentinel:
datasource:
# 名称随意
flow:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-flow-rules
groupId: SENTINEL_GROUP
# 规则类型,取值见:
# org.springframework.cloud.alibaba.sentinel.datasource.RuleType
rule-type: flow
degrade:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-degrade-rules
groupId: SENTINEL_GROUP
rule-type: degrade
system:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-system-rules
groupId: SENTINEL_GROUP
rule-type: system
authority:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-authority-rules
groupId: SENTINEL_GROUP
rule-type: authority
param-flow:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-param-flow-rules
groupId: SENTINEL_GROUP
rule-type: param-flow
3.2、Sentinel控制台代码改造
1)控制台sentinel-dashboard改造主要是为规则实现
DynamicRuleProvider:从Nacos上读取配置
DynamicRulePublisher:将规则推送到Nacos上
修改pom.xml,将 <scope>test</scope> 这一行注释掉,即改为如下:
<!-- for Nacos rule publisher sample -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<!-- <scope>test</scope>-->
</dependency>
2)找到 sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/rule/nacos目录,将整个目录拷贝到 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos
备注:
其实这里面的flowRule规则类是官方提供的Nacos持久化demo,其他规则实现都可以参考flow规则代码实现。
同时添加其他规则(需要自己新建):
3)在controller中修改读取规则和持久化规则的代码:
4)修改流控规则代码
com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2 ,找到
@Autowired
@Qualifier("flowRuleDefaultProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleDefaultPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
修改为:
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
5)注意其他规则修改与flow不同,其他类里面的的代码写法可以参考flow规则
例如授权规则:com.alibaba.csp.sentinel.dashboard.controller.AuthorityRuleController
需要手动添加如下代码:
@Autowired
@Qualifier("authorityRuleNacosProvider")
private DynamicRuleProvider<List<AuthorityRuleEntity>> ruleProvider;
@Autowired
@Qualifier("authorityRuleNacosPublisher")
private DynamicRulePublisher<List<AuthorityRuleEntity>> rulePublisher;
发布规则方法替换原有的发布方法:
// added by hankin
private void publishRules(String app) throws Exception {
List<AuthorityRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
}
// private boolean publishRules(String app, String ip, Integer port) {
// List<AuthorityRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
// return sentinelApiClient.setAuthorityRuleOfMachine(app, ip, port, rules);
// }
com.alibaba.csp.sentinel.dashboard.controller.AuthorityRuleController#apiQueryAllRulesForMachine
查询规则方法替换:
try {
// added by hankin 通过 AuthorityRuleNacosProvider获取授权规则
List<AuthorityRuleEntity> rules = ruleProvider.getRules(app);
// List<AuthorityRuleEntity> rules = sentinelApiClient.fetchAuthorityRulesOfMachine(app, ip, port);
rules = repository.saveAll(rules);
return Result.ofSuccess(rules);
} catch (Throwable throwable) {
com.alibaba.csp.sentinel.dashboard.controller.AuthorityRuleController#apiAddAuthorityRule
然后在增删改方法里面更新对应的publishRules发布方法:
try {
entity = repository.save(entity);
// added by hankin
publishRules(entity.getApp());
} catch (Throwable throwable) {
6)菜单栏增加:流控规则V1
修改 sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html,找到:
<li ui-sref-active="active" ng-if="entry.appType==0">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则 V1</a>
</li>
把注释打开,终于把规则改造成推模式持久化啦!
3.3、编译&启动
执行: mvn clean package -DskipTests
在项目的 target 目录找到sentinel-dashboard.jar ,执行 java -jar sentinel-dashboard.jar 启动控制台。
3.4、测试
测试1:用Sentinel控制台【菜单栏的 流控规则 V1 】推送流控规则,规则会存储到Nacos;
测试2:直接在Nacos上修改流控规则,然后刷新Sentinel控制台,控制台上的显示也会被修改;
测试3:重启Sentinel控制台,并重启微服务;刷新控制台,可以发现规则依然存在。
Sentinel有若干种规则,例如降级规则、系统规则、授权规则、热点规则等,都需要使用类似的方式,修改 com.alibaba.csp.sentinel.dashboard.controller 包中对应的Controller,才能实现持久化
注意:
springcloudalibaba-sentinel-nacos-degrade-rules
springcloudalibaba-sentinel-nacos-degrade-rules-dashboard
生成的配置文件会有两个,其实这个两个文件使我们在代码NacosConfigUtil#setRuleStringToNacos里面定义好的两种文件存储方式,
代码如下所示:
// 存储,给微服务使用
String dataId = genDataId(app, postfix);
configService.publishConfig(dataId,
NacosConfigUtil.GROUP_ID,JSONUtils.toJSONString(ruleForApp)
);
// 存储,给控制台使用
configService.publishConfig(dataId + DASHBOARD_POSTFIX,
NacosConfigUtil.GROUP_ID,JSONUtils.toJSONString(rules)
);
本文参考:作者周立创作
http://www.itmuch.com/spring-cloud-alibaba/sentinel-rules-persistence-push-mode-using-nacos/