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

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

相关推荐
你是人间五月天11 小时前
sentinel实现控制台与nacos数据双向绑定
windows·sentinel
无名客011 小时前
sentinel限流常见的几种算法以及优缺点
算法·sentinel·限流
tsxchen15 小时前
centos9安装sentinel
sentinel
寒士obj18 小时前
Sentinel服务治理:服务降级、熔断与线程隔离
sentinel
iiYcyk18 小时前
Hystrix与Sentinel-熔断限流
hystrix·sentinel·springcloud
勇往直前plus19 小时前
Sentinel微服务保护
java·spring boot·微服务·sentinel
boy快快长大3 天前
【Spring Cloud Alibaba】Sentinel(一)
sentinel
java干货3 天前
Sentinel和Cluster,到底该怎么选?
sentinel
27^×3 天前
Sentinel 与 Feign 整合详解:实现服务调用的流量防护
sentinel
爬山算法4 天前
Redis(43)Redis哨兵(Sentinel)是什么?
redis·bootstrap·sentinel