当前位置:网站首页>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中读取出来,做到了持久化
边栏推荐
- Linear DP (split)
- 500. Keyboard line
- No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
- LeetCode 47. Full arrangement II
- 492. Construction rectangle
- Shenji Bailian 3.53-kruskal
- Spark overview
- Introduce uview into uni app
- 最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
- LeetCode 39. Combined sum
猜你喜欢
如何使用MITMPROXy
递归(迷宫问题、8皇后问题)
Detailed explanation of BGP message
Hydration failed because the initial UI does not match what was rendered on the server.问题原因之一
从设计交付到开发,轻松畅快高效率!
500. Keyboard line
Ros2 --- lifecycle node summary
How to use mitmproxy
Network related knowledge (Hardware Engineer)
Common means of modeling: combination
随机推荐
深入了解JUC并发(二)并发理论
TensorRT中的循环
LeetCode 83. 删除排序链表中的重复元素
Zhuanzhuanben - LAN construction - Notes
LeetCode 27. 移除元素
Cglib代理-代码增强测试
如何使用MITMPROXy
BGP 路由优选规则和通告原则
ZABBIX server trap command injection vulnerability (cve-2017-2824)
Classic literature reading -- deformable Detr
LeetCode 83. Delete duplicate elements in the sorting linked list
Generic classes and parameterized classes of SystemVerilog
LeetCode 39. 组合总和
Community theory | kotlin flow's principle and design philosophy
BGP中的状态机
从设计交付到开发,轻松畅快高效率!
Problems encountered in uni app development (continuous update)
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
Deep learning classification network -- vggnet
Frequently asked questions about jetpack compose and material you