46.Sentinel规则持久化

当微服务order-service服务重启,之前配置规则就丢失了。

这是因为Sentinel默认会将这些规则保存在内存里。那么在生产环境下这样肯定是不行的。

sentinel的控制台规则管理有三种模式:

1.原始模式:Sentinel的默认模式,将规则保存在内存,重启微服务会丢失。

2.pull模式,支持持久化。

控制台将配置规则推送的sentinel客户端,而客户端会将配置规则保存在本地文件或者数据库中。以后会定时去本地文件或数据库中查询,更新本地规则。

**缺陷:**微服务一般都是要做集群的,一个微服务将配置刚更新到数据库,其他微服务没有及时的去读取已经更新过后的配置,因为是定时读取。所以存在时效性,从而导致数据的不一致问题。所以不推荐。

3.push模式,支持持久化。

sentinel控制台将配置规则推送到远程配置中心,例如Nacos。Sentinel客户端监听Nacos,获取配置变更的推送消息,完成本地配置更新。

实现push模式,需要去修改Sentinel开源框架的源代码,同时微服务也要改成去监听nacos。

1.引入Sentinel监听nacos的依赖

java 复制代码
<!--sentinel监听nacos的依赖,实现规则持久化,push模式-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>

2.添加配置

复制代码
spring:
  profiles:
    active: test
  application:
    name: order-service
  cloud:
    nacos:
      server-addr: localhost:8848 # nacos 服务端地址0
      config:
        file-extension: yaml # 文件后缀名
      discovery:
        enabled: true
        namespace: public
        group: DEFAULT_GROUP
        watch:
          enabled: true # 启用服务监听
        watch-delay: 3000 # 3秒拉取一次最新服务列表
    sentinel:
      transport:
        dashboard: localhost:8080 # sentinel控制台地址
      web-context-unify: false # 关闭context整合(Sentinel默认会将Controller方法做context上下文整合,导致链路模式的流控失败)
      datasource:
        flow: # 配置限流
          nacos:
            server-addr: localhost:8848
            dataId: orderservice-flow-rules
            groupId: SENTINEL_GROUP
            rule-type: flow # 还可以是 degrade authority param-flow
        degrade: # 配置降级
          nacos:
            server-addr: localhost:8848
            dataId: orderservice-degrade-rules
            groupId: SENTINEL_GROUP
            rule-type: degrade # 还可以是 flow authority param-flow

3.修改源码打包

Sentinel默认不支持nacos持久化,需要修改源码

①解压源码包

首先去下载Sentinel对应版本的源码包zip文件

网址:https://github.com/alibaba/Sentinel/releases

②用idea打开解压后后的项目

③修改sentinel-dashboard源码的pom文件,nacos的依赖默认scope是test,只能在测试时候使用,这里要去除。

④将sentinel-datasource-nacos依赖的scope去掉。

4.在Sentinel控制台会多出带-NACOS的菜单,进行各种规则配置。

待续....未完....

相关推荐
Owen__z6 天前
GEE统计特定区域特定时间上的Landsat/Sentinel的影像信息
python·sentinel·gee·geemap·landsat
我要去腾讯6 天前
Springcloud核心组件之Sentinel详解
java·spring cloud·sentinel
哦你看看11 天前
Redis Sentinel哨兵集群
linux·redis·bootstrap·sentinel
2501_9387900712 天前
Spring Cloud Alibaba 2023 版:Nacos 服务发现与 Sentinel 限流的整合方案
sentinel·服务发现
青鱼入云13 天前
Sentinel介绍
微服务·sentinel
青鱼入云13 天前
Feign如何集成Sentinel
spring cloud·微服务·sentinel
一周困⁸天.14 天前
Redis Sentinel哨兵集群
redis·bootstrap·sentinel
一条懒鱼66615 天前
Redis Sentinel哨兵集群
数据库·redis·sentinel
2301_7976042415 天前
d44:Sentinel 微服务流量控制与 Seata 分布式事务
分布式·微服务·sentinel
新手小白*16 天前
Redis Sentinel哨兵集群
数据库·redis·sentinel