概述
经观察源码发现,仅需要2部
1、增加对应pom
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- Sentinel 针对 Nacos 作了适配,底层可以采用 Nacos 作为规则配置数据源 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
2、application.yml配置sentinel数据源
spring:
cloud:
sentinel:
transport:
port: 8719
#dashboard port,dashboard独立下载jar启动 --server.port=8333
dashboard: ${SENTINEL_DASHBOARD_HOST:localhost}:${SENTINEL_DASHBOARD_PORT:8333}
#启动就加载上报 ,务启动直接建立心跳连接
eager: true
log:
dir: ${cloud.logger.path}
#sentinel配置持久化:https://blog.csdn.net/z69183787/article/details/109010980
datasource:
flow-data:
nacos:
server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848}
dataId: ${spring.application.name}-flow-rules
namespace: c7d1b9ac-ea6c-4494-89c0-f9d7f7410fc0
groupId: SENTINEL_GROUP
data-type: json
rule-type: flow
degrade-data:
nacos:
server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848}
dataId: ${spring.application.name}-degrade-rules
namespace: c7d1b9ac-ea6c-4494-89c0-f9d7f7410fc0
groupId: SENTINEL_GROUP
data-type: json
rule-type: degrade
system-data:
nacos:
server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848}
dataId: ${spring.application.name}-system-rules
namespace: c7d1b9ac-ea6c-4494-89c0-f9d7f7410fc0
groupId: SENTINEL_GROUP
data-type: json
rule-type: system
authority-data:
nacos:
server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848}
dataId: ${spring.application.name}-authority-rules
namespace: c7d1b9ac-ea6c-4494-89c0-f9d7f7410fc0
groupId: SENTINEL_GROUP
data-type: json
rule-type: authority
param-flow-data:
nacos:
server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848}
dataId: ${spring.application.name}-param-flow-rules
namespace: c7d1b9ac-ea6c-4494-89c0-f9d7f7410fc0
groupId: SENTINEL_GROUP
data-type: json
rule-type: param-flow
gw-flow-data:
nacos:
server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848}
dataId: ${spring.application.name}-gw-flow-rules
namespace: c7d1b9ac-ea6c-4494-89c0-f9d7f7410fc0
groupId: SENTINEL_GROUP
data-type: json
rule-type: gw-flow
gw-api-group-data:
nacos:
server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848}
dataId: ${spring.application.name}-gw-api-group-rules
namespace: c7d1b9ac-ea6c-4494-89c0-f9d7f7410fc0
groupId: SENTINEL_GROUP
data-type: json
rule-type: gw-api-group
3、原理上,
0、SentinelAutoConfiguration 自动配置类,装载SentinelProperties配置文件,为后续操作进行设置
1、SentinelAutoConfiguration 会初始化 N 个 和 sentinel规则相关的 json converter ,命名规则:@Bean("sentinel-json-flow-converter")
2、SentinelProperties中的 NacosDataSourceProperties 映射 bootstrap中的 sentinel.datasource.flow-data.nacos 对应
3、SentinelAutoConfiguration 同时初始化 SentinelDataSourceHandler,进入该类的 afterSingletonsInstantiated 方法,初始化 NacosDataSource数据源(通过factoryBeanName属性,为NacosDataSourceFactoryBean)
public NacosDataSourceProperties() {
super(NacosDataSourceFactoryBean.class.getName());
}
4、NacosDataSource 注册 nacos属性变更后的监听器,同步进行内存中的 sentinel 规则刷新
5、各个规则的 listener 根据 1 中定义的 converter对value进行反序列化后,进行应用内部的sentinel规则刷新
首先贴出官网的说明:
动态数据源支持:https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel
在生产环境中使用:https://github.com/alibaba/Sentinel/wiki/%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%E4%BD%BF%E7%94%A8-Sentinel
动态规则扩展:https://github.com/alibaba/Sentinel/wiki/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%99%E6%89%A9%E5%B1%95
最后
以上就是碧蓝早晨为你收集整理的sentinel nacos 持久化规则配置+客户端推送的全部内容,希望文章能够帮你解决sentinel nacos 持久化规则配置+客户端推送所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复