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的菜单,进行各种规则配置。

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

相关推荐
柳贯一(逆流河版)1 天前
Sentinel 深度解析:限流与熔断降级的微服务稳定性保障实践
微服务·架构·sentinel
耳东哇1 天前
sentinel docker gateway k8s 集群 主从
docker·gateway·sentinel
遥感之家2 天前
AWS下载sentinel-2原始影像
云计算·sentinel·aws
月夕·花晨3 天前
Gateway-断言
java·开发语言·分布式·spring cloud·微服务·nacos·sentinel
齐 飞4 天前
Spring Cloud Alibaba快速入门-Sentinel熔断规则
spring boot·spring cloud·sentinel
齐 飞6 天前
Spring Cloud Alibaba快速入门-Sentinel流量控制(FlowRule)
spring cloud·微服务·sentinel
Takumilove7 天前
Spring Boot 接入 Redis Sentinel:自动主从切换与读写分离实战(修复单机多实例与 Sentinel 配置坑)
spring boot·redis·sentinel
波波烤鸭15 天前
Redis 高可用实战源码解析(Sentinel + Cluster 整合应用)
数据库·redis·sentinel
hzzzzzo017 天前
微服务保护全攻略:从雪崩到 Sentinel 实战
数据库·微服务·sentinel
没有bug.的程序员18 天前
Redis Sentinel:高可用架构的守护者
java·redis·架构·sentinel