sentinel nacos 持久化规则配置+客户端推送

经观察源码发现,仅需要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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值