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

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

相关推荐
kkkkk0211063 天前
微服务学习笔记(黑马商城)
java·spring boot·spring·spring cloud·sentinel·mybatis·java-rabbitmq
choice of4 天前
Sentinel:阿里云高并发流量控制
笔记·spring cloud·sentinel
W.Buffer6 天前
SpringCloud-Sentinel实战与源码分析:从流量防护到底层实现
spring·spring cloud·sentinel
TM_soul6 天前
Sentinel安装部署
sentinel
不能再留遗憾了9 天前
【SpringCloud】Sentinel
spring·spring cloud·sentinel
Chan1610 天前
流量安全优化:基于 Sentinel 实现网站流量控制和熔断
java·spring boot·安全·sentinel·intellij-idea·进程
@HNUSTer11 天前
基于 GEE 的 Sentinel-2 光谱、指数、纹理特征提取与 Sentinel-1 SAR 数据处理
云计算·sentinel·数据集·遥感大数据·gee·云平台·sar
@HNUSTer14 天前
基于 GEE 平台用 Sentinel-1 SAR 数据实现山区潜在滑坡检测
云计算·sentinel·数据集·遥感大数据·gee·云平台·sar
还是鼠鼠16 天前
《黑马商城》微服务保护-详细介绍【简单易懂注释版】
java·spring boot·spring·spring cloud·sentinel·maven
月夕·花晨19 天前
Gateway-过滤器
java·分布式·spring·spring cloud·微服务·gateway·sentinel