pull模式规则持久化

pull 模式的数据源(如本地文件、RDBMS 等)一般是可写入的。使用时需要在客户端注册数据源:将对应的读数据源注册至对应的 RuleManager,将写数据源注册至 transport 的 WritableDataSourceRegistry 中。[1]

本地文件数据源会定时轮询文件的变更,读取规则。这样我们既可以在应用本地直接修改文件来更新规则,也可以通过 Sentinel 控制台推送规则。以本地文件数据源为例,推送过程如下图所示:

首先 Sentinel 控制台通过 API 将规则推送至客户端并更新到内存中,接着注册的写数据源会将新的规则保存到本地的文件中。使用 pull 模式的数据源时一般不需要对 Sentinel 控制台进行改造。

这种实现方法好处是简单,不引入新的依赖,坏处是无法保证监控数据的一致性。

定义数据源

这里将数据源定义在了classes目录下了。

package com.zjw.sentinel;

import com.alibaba.csp.sentinel.command.handler.ModifyParamFlowRulesCommandHandler;
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
import com.alibaba.csp.sentinel.datasource.FileWritableDataSource;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.WritableDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference; import java.io.File;
import java.io.IOException;
import java.util.List; /**
* @since 2023/12/07 22:10
*/
public class FileDataSourceInit implements InitFunc { @Override
public void init() throws Exception {
// 获取当前类的ClassLoader
ClassLoader classLoader = FileDataSourceInit.class.getClassLoader();
// File ruleDir = new File(System.getProperty("user.dir") + "/rules");
File ruleDir = new File(classLoader.getResource("").getPath() + File.separator+"rules");
if (!ruleDir.exists()) {
ruleDir.mkdirs();
}
// 规则文件路径
String ruleDirPath = ruleDir.getPath();
readWriteRuleFileFlow(ruleDirPath,"flow-rule.json");
readWriteRuleFileDegrade(ruleDirPath,"degrade-rule.json");
readWriteRuleFileParamFlow(ruleDirPath,"param-rule.json");
readWriteRuleFileAuthority(ruleDirPath,"authority-rule.json");
readWriteRuleFileSystem(ruleDirPath,"system-rule.json");
} /**
* 流控规则
*/
private void readWriteRuleFileFlow(String rulePath, String ruleFile) throws IOException {
//创建规则文件
String ruleFilePath = rulePath + File.separator + ruleFile;
createRuleFile(ruleFilePath); ReadableDataSource<String, List<FlowRule>> ds = new FileRefreshableDataSource<>(
ruleFilePath, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})
);
// 将可读数据源注册至 FlowRuleManager.
FlowRuleManager.register2Property(ds.getProperty()); WritableDataSource<List<FlowRule>> wds = new FileWritableDataSource<>(ruleFilePath, this::encodeJson);
// 将可写数据源注册至 transport 模块的 WritableDataSourceRegistry 中.
// 这样收到控制台推送的规则时,Sentinel 会先更新到内存,然后将规则写入到文件中.
WritableDataSourceRegistry.registerFlowDataSource(wds);
} /**
* 熔断规则
*/
private void readWriteRuleFileDegrade(String rulePath, String ruleFile) throws IOException {
//创建规则文件
String ruleFilePath = rulePath + File.separator + ruleFile;
createRuleFile(ruleFilePath); ReadableDataSource<String, List<DegradeRule>> ds = new FileRefreshableDataSource<>(
ruleFilePath, source -> JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {})
);
// 将可读数据源注册至 FlowRuleManager.
DegradeRuleManager.register2Property(ds.getProperty()); WritableDataSource<List<DegradeRule>> wds = new FileWritableDataSource<>(ruleFilePath, this::encodeJson);
// 将可写数据源注册至 transport 模块的 WritableDataSourceRegistry 中.
// 这样收到控制台推送的规则时,Sentinel 会先更新到内存,然后将规则写入到文件中.
WritableDataSourceRegistry.registerDegradeDataSource(wds);
} /**
* 热点规则
*/
private void readWriteRuleFileParamFlow(String rulePath, String ruleFile) throws IOException {
//创建规则文件
String ruleFilePath = rulePath + File.separator + ruleFile;
createRuleFile(ruleFilePath); ReadableDataSource<String, List<ParamFlowRule>> ds = new FileRefreshableDataSource<>(
ruleFilePath, source -> JSON.parseObject(source, new TypeReference<List<ParamFlowRule>>() {})
);
// 将可读数据源注册至 FlowRuleManager.
ParamFlowRuleManager.register2Property(ds.getProperty()); WritableDataSource<List<ParamFlowRule>> wds = new FileWritableDataSource<>(ruleFilePath, this::encodeJson);
// 将可写数据源注册至 transport 模块的 WritableDataSourceRegistry 中.
// 这样收到控制台推送的规则时,Sentinel 会先更新到内存,然后将规则写入到文件中.
ModifyParamFlowRulesCommandHandler.setWritableDataSource(wds);
} /**
* 系统规则
*/
private void readWriteRuleFileSystem(String rulePath, String ruleFile) throws IOException {
//创建规则文件
String ruleFilePath = rulePath + File.separator + ruleFile;
createRuleFile(ruleFilePath); ReadableDataSource<String, List<SystemRule>> ds = new FileRefreshableDataSource<>(
ruleFilePath, source -> JSON.parseObject(source, new TypeReference<List<SystemRule>>() {})
);
// 将可读数据源注册至 FlowRuleManager.
SystemRuleManager.register2Property(ds.getProperty()); WritableDataSource<List<SystemRule>> wds = new FileWritableDataSource<>(ruleFilePath, this::encodeJson);
// 将可写数据源注册至 transport 模块的 WritableDataSourceRegistry 中.
// 这样收到控制台推送的规则时,Sentinel 会先更新到内存,然后将规则写入到文件中.
WritableDataSourceRegistry.registerSystemDataSource(wds);
} /**
* 授权规则
*/
private void readWriteRuleFileAuthority(String rulePath, String ruleFile) throws IOException {
//创建规则文件
String ruleFilePath = rulePath + File.separator + ruleFile;
createRuleFile(ruleFilePath); ReadableDataSource<String, List<AuthorityRule>> ds = new FileRefreshableDataSource<>(
ruleFilePath, source -> JSON.parseObject(source, new TypeReference<List<AuthorityRule>>() {})
);
// 将可读数据源注册至 FlowRuleManager.
AuthorityRuleManager.register2Property(ds.getProperty()); WritableDataSource<List<AuthorityRule>> wds = new FileWritableDataSource<>(ruleFilePath, this::encodeJson);
// 将可写数据源注册至 transport 模块的 WritableDataSourceRegistry 中.
// 这样收到控制台推送的规则时,Sentinel 会先更新到内存,然后将规则写入到文件中.
WritableDataSourceRegistry.registerAuthorityDataSource(wds);
} private void createRuleFile(String ruleFilePath) throws IOException {
File file = new File(ruleFilePath);
if (!file.exists()) {
file.createNewFile();
}
} private <T> String encodeJson(T t) {
return JSON.toJSONString(t);
}
}

定义SPI接口文件

pull模式的 Datasource 扩展采用的是 SPt 服务发现机制。按照 SPI 规范,需要在/resources目录下新建 META-INF/services 目录,然后在该目录下新建一个文本文件。文件名为扩展类实现接口的全限定性接口名,文件内容则为该接口的实现类的全限定性类名。

com.alibaba.csp.sentinel.init.InitFunc

com.zjw.sentinel.FileDataSourceInit

测试

启动服务后,发现创建了规则文件。

在sentinel控制台配置了规则后会自动保存在规则文件中。文件中修改了也会在sentinel控制台中修改。服务重启后配置仍然存在。


  1. https://github.com/alibaba/Sentinel/wiki/在生产环境中使用-Sentinel#pull模式

Sentinel——pull模式规则持久化的更多相关文章

  1. [Spring-Cloud-Alibaba] Sentinel 规则持久化

    在之前的练习中,只要应用重启,就需要重新配置,这样在我们实际的项目是非常不实用的,那么有没有办法把我们配置的规则保存下来呢?答案是YES,那么接下来,给大家来介绍如何将Sentinel规则持久化. D ...

  2. Spring Cloud Alibaba学习笔记(7) - Sentinel规则持久化及生产环境使用

    Sentinel 控制台 需要具备下面几个特性: 规则管理及推送,集中管理和推送规则.sentinel-core 提供 API 和扩展接口来接收信息.开发者需要根据自己的环境,选取一个可靠的推送规则方 ...

  3. Sentinel Dashboard(基于1.8.1)流控规则持久化到Nacos——涉及部分Sentinel Dashboard源码改造

    前言 之前虽然也一直在使用sentinel实现限流熔断功能,但却没有好好整理之前看的源码与资料,今天有时间将之前自己整理过的资料写成一篇博文,或者是是一篇关于Sentinel(基于目前最近版本1.8, ...

  4. sentinel 规则持久化到nacos

    问题描述 Sentinel Dashboard中添加的规则是存储在内存中的,只要项目一重启规则就丢失了 此处将规则持久化到nacos中,在nacos中添加规则,然后同步到dashboard中: 后面研 ...

  5. Sentinel基于Apollo的持久化改造

    Sentinel基于Apollo的持久化改造 sentinel默认是将用户在dashboard中编辑过的流控信息保存在内存中,所以在重启后,所有之前配置过的流控规则也就不见了.但是sentinel给用 ...

  6. (六) Docker 部署 Redis 高可用集群 (sentinel 哨兵模式)

    参考并感谢 官方文档 https://hub.docker.com/_/redis GitHub https://github.com/antirez/redis happyJared https:/ ...

  7. 什么!Sentinel流控规则可以这样玩?

    项目源码地址:公众号回复 sentinel,即可免费获取源码 前言 上一篇文章中,我们讲解了关于sentinel基本介绍以及流控规则中直接和快速失败的效果,有兴趣的可以去看上一篇文章,今天,我们给大家 ...

  8. 微博feed系统的推(push)模式和拉(pull)模式和时间分区拉模式架构探讨

    sns系统,微博系统都应用到了feed(每条微博或者sns里的新鲜事等我们称作feed)系统,不管是twitter.com或者国内的新浪微博,人人网等,在各种技术社区,技术大会上都在分享自己的feed ...

  9. C语言的本质(37)——makefile之隐含规则和模式规则

    Makefile有很多灵活的写法,可以写得更简洁,同时减少出错的可能.本节我们来看看这样一个例子还有哪些改进的余地. 一个目标依赖的所有条件不一定非得写在一条规则中,也可以拆开写,例如: main.o ...

  10. Redis笔记-Sentinel哨兵模式

    Redis以主从的模式搭建集群后,如果主节点Master挂掉,虽然可以实现将备用节点Slave切换成主节点,但是Redis本身并没有自动监控机制,需要借助Sentinel哨兵模式,实现监控并实现自动切 ...

随机推荐

  1. [记录点滴]编译安装luarocks、luacheck、luautf8

    [记录点滴]编译安装luarocks.luacheck.luautf8 0x00 摘要 记录一次安装luarocks&第三方库的过程. 0x01 luarocks 如今每个语言体系中都有一个包 ...

  2. mysql之PreparedStatement的增删改

    编写配置文件[db.properties]: driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/jdbcStudy?useUni ...

  3. .NET Core 托管堆内存泄露/CPU异常的常见思路

    常见的思路 内存泄露 托管内存暴涨大多数原因都是因为对象被GC Root(stack,gchandle,finalizequeue)持有,所以一直无法释放,所以观察的重点都在对象的可疑GC Root ...

  4. YASKAWA安川机器人DX100轴控制基板维修解析知识

    ASKAWA安川机器人DX100轴控制基板的维修是一项复杂而精细的工作,要求具备丰富的知识和实践经验.通过与子锐机器人维修联系,希望能企业提供一些有益的参考和帮助,在面对轴板故障时能够迅速准确地找到问 ...

  5. 火爆的 幻兽帕鲁/Palworld 单机➕联机 电脑游戏 免费畅游

    在广阔的世界中收集神奇的生物"帕鲁",派他们进行战斗.建造.做农活,工业生产等,这是一款支持多人游戏模式的全新开放世界生存制作游戏. ▼补丁主要内容 ・修复加载世界数据时,加载画面 ...

  6. gitlab - [02] 安装部署

    安装部署篇 一.5分钟搭建私人代码仓库 (1)设置环境变量:export GITLAB_HOME=/src/gitlab (2)编写docker-compose.yml mkdir -p /opt/d ...

  7. 【渗透测试】Vulnhub GROTESQUE 1.0.1

    渗透环境 攻击机:   IP: 192.168.10.18(Kali) 靶机:     IP:192.168.10.9 靶机下载地址:https://www.vulnhub.com/entry/gro ...

  8. python基础-数据类型、字典、集合、文件操作(打开、关闭、读写、追加等)

    前言 !!!注意:本系列所写的文章全部是学习笔记,来自于观看视频的笔记记录,防止丢失.观看的视频笔记来自于:哔哩哔哩武沛齐老师的视频:2022 Python的web开发(完整版) 入门全套教程,零基础 ...

  9. 使用nodejs安装并使用vue操作步骤

    1.下载安装nodejs 官网下载nodejs并安装,我这边选择Windows的20版本 下载地址:https://nodejs.org/en/download/prebuilt-installer ...

  10. git码云安装及使用菜鸟教程

    1.下载Windows本地码云 https://mirrors.huaweicloud.com/git-for-windows/(华为镜像下载),选择合适的版本下载,此处下载速度要快些 2.登录码云官 ...