当前位置:网站首页>Sentinel规则持久化到Nacos
Sentinel规则持久化到Nacos
2022-07-02 06:13:00 【dotaer-df】
上文介绍到Sentinel的规则如果需要持久化,就需要采用推或者拉模式
- 拉模式:客户端主动向某个规则管理中心定期轮询拉取规则,这个规则中心可以是 RDBMS、文件,甚至是 VCS 等。这样做的方式是简单,缺点是无法及时获取变更;
- 推模式:规则中心统一推送,客户端通过注册监听器的方式时刻监听变化,比如使用 Nacos、Zookeeper 等配置中心。这种方式有更好的实时性和一致性保证。
本文采用Nacos也就是推模式持久化规则,这种方式的交互就切断了Sentinel客户端和 Sentinel Dashboard的联系。它的交互方式是这样:当Sentinel Dashboard有规则变更后,会推送到Nacos,再又Nacos推送给客户端,如下图所示:
这种方式需要修改两处地方:
1、修改 Sentinel Dashboard源码(其实Dashboard已经接入了nacos,我们只需要替换一下默认的实现)
2、Sentinel客户端接入Nacos
修改 Sentinel Dashboard源码
1、下载Sentinel Dashboard项目
https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard
2、将test目录下关于Nacos拷贝到com.alibaba.csp.sentinel.dashboard.rule
其中FlowRuleNacosProvider会从Nacos中拉取规则,FlowRuleNacosPublisher会向Nacos推送规则,这是Dashboard为我们提供的实现
3、然后替换FlowControllerV2类中DynamicRuleProvider为Nacos的实现
4、然后前端页面也要改成调用v2的接口,默认是调用/v1/flow
sidebar.html(resources/app/scripts/directives/sidebar)页面
控规则路由从dashboard.flowV1
改成dashboard.flow
app.js(resources/dist/js/app.js)将app/scripts/controllers/flow_v1.js
替换为app/scripts/controllers/flow_v2.js
Sentinel Dashboard修改完成!!
Sentinel客户端接入Nacos
2、Sentinel 针对 Nacos 作了适配,底层可以采用 Nacos 作为规则配置数据源。使用时只需添加以下依赖:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.0</version>
</dependency>
完整依赖:
<!-- 依赖web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- sentinel springboot-starter -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2021.1</version>
</dependency>
<!-- sentinel 核心依赖-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.0</version>
</dependency>
<!--sentinel持久化 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.0</version>
</dependency>
Yml配置
server:
port: 8081
spring:
application:
name: sentinel
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
sentinel:
transport:
dashboard: 127.0.0.1:8080 #指定sentinel dashboard web 地址
datasource:
ds1: #名称自定义,唯一
nacos:
server-addr: 127.0.0.1:8848
dataId: ${spring.application.name}-flow-rules
groupId: SENTINEL_GROUP
data-type: json
rule-type: flow
其中dataId和groupId要和Sentinel Dashboard中FlowRuleNacosPublisher所push的一致,默认值如下,所以我们也设置成默认值即可
然后依次启动Sentinel Dashboard和Sentinel客户端即可,从Dashboard中修改的规则会立马推送到Nacos,从Nacos添加的规则Dashboard也能感知到,且就算客户端或者Dashboard重启,数据也能从Nacos中读取出来,做到了持久化
边栏推荐
猜你喜欢
Deep learning classification network -- vggnet
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
Eco express micro engine system has supported one click deployment to cloud hosting
加密压缩文件解密技巧
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
Google Play Academy 组队 PK 赛,正式开赛!
VRRP之监视上行链路
Classic literature reading -- deformable Detr
线性dp(拆分篇)
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
随机推荐
深入了解JUC并发(一)什么是JUC
BGP中的状态机
队列(线性结构)
浏览器原理思维导图
BGP报文详细解释
栈(线性结构)
标签属性disabled selected checked等布尔类型赋值不生效?
步骤详解 | 助您轻松提交 Google Play 数据安全表单
The real definition of open source software
LeetCode 90. Subset II
How to use mitmproxy
No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
介绍两款代码自动生成器,帮助提升工作效率
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
从设计交付到开发,轻松畅快高效率!
Shenji Bailian 3.53-kruskal
Pbootcms collection and warehousing tutorial quick collection release
Introduce uview into uni app
锐捷EBGP 配置案例
TensorRT的功能