Sentinel——pull模式规则持久化
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控制台中修改。服务重启后配置仍然存在。
Sentinel——pull模式规则持久化的更多相关文章
- [Spring-Cloud-Alibaba] Sentinel 规则持久化
在之前的练习中,只要应用重启,就需要重新配置,这样在我们实际的项目是非常不实用的,那么有没有办法把我们配置的规则保存下来呢?答案是YES,那么接下来,给大家来介绍如何将Sentinel规则持久化. D ...
- Spring Cloud Alibaba学习笔记(7) - Sentinel规则持久化及生产环境使用
Sentinel 控制台 需要具备下面几个特性: 规则管理及推送,集中管理和推送规则.sentinel-core 提供 API 和扩展接口来接收信息.开发者需要根据自己的环境,选取一个可靠的推送规则方 ...
- Sentinel Dashboard(基于1.8.1)流控规则持久化到Nacos——涉及部分Sentinel Dashboard源码改造
前言 之前虽然也一直在使用sentinel实现限流熔断功能,但却没有好好整理之前看的源码与资料,今天有时间将之前自己整理过的资料写成一篇博文,或者是是一篇关于Sentinel(基于目前最近版本1.8, ...
- sentinel 规则持久化到nacos
问题描述 Sentinel Dashboard中添加的规则是存储在内存中的,只要项目一重启规则就丢失了 此处将规则持久化到nacos中,在nacos中添加规则,然后同步到dashboard中: 后面研 ...
- Sentinel基于Apollo的持久化改造
Sentinel基于Apollo的持久化改造 sentinel默认是将用户在dashboard中编辑过的流控信息保存在内存中,所以在重启后,所有之前配置过的流控规则也就不见了.但是sentinel给用 ...
- (六) Docker 部署 Redis 高可用集群 (sentinel 哨兵模式)
参考并感谢 官方文档 https://hub.docker.com/_/redis GitHub https://github.com/antirez/redis happyJared https:/ ...
- 什么!Sentinel流控规则可以这样玩?
项目源码地址:公众号回复 sentinel,即可免费获取源码 前言 上一篇文章中,我们讲解了关于sentinel基本介绍以及流控规则中直接和快速失败的效果,有兴趣的可以去看上一篇文章,今天,我们给大家 ...
- 微博feed系统的推(push)模式和拉(pull)模式和时间分区拉模式架构探讨
sns系统,微博系统都应用到了feed(每条微博或者sns里的新鲜事等我们称作feed)系统,不管是twitter.com或者国内的新浪微博,人人网等,在各种技术社区,技术大会上都在分享自己的feed ...
- C语言的本质(37)——makefile之隐含规则和模式规则
Makefile有很多灵活的写法,可以写得更简洁,同时减少出错的可能.本节我们来看看这样一个例子还有哪些改进的余地. 一个目标依赖的所有条件不一定非得写在一条规则中,也可以拆开写,例如: main.o ...
- Redis笔记-Sentinel哨兵模式
Redis以主从的模式搭建集群后,如果主节点Master挂掉,虽然可以实现将备用节点Slave切换成主节点,但是Redis本身并没有自动监控机制,需要借助Sentinel哨兵模式,实现监控并实现自动切 ...
随机推荐
- flutter-解决长按TextField出现英文(复制粘贴)问题
第一步 引入依赖 dependencies: flutter: sdk: flutter flutter_localizations: sdk: flutter 第二步 在main.dart中添加代码 ...
- 使用idea合并 dev分支合并到test分支
这里展示将dev分支合并到test分支首先切换到test分支 按下图所示操作
- 若依单体Vue版本新增多环境配置
若依框架是一个简单的web后台管理脚手架,基于SpringBoot+Vue开发的.本次更改版本为3.8.7截止2023年8月14日,最新版本! 若依自带只有 application.yml 与 app ...
- 【Spring Boot】我的第一个Spring Boot练习
背景 Spring 在 Java 生态的企业级开发项目中极其常用,通常我们为项目引入一项新技术时,不得不考虑如何将新技术与 Spring 整合在一起. 我们知道,预研一项新技术,我们基于 MVP(最简 ...
- PHP中处理html相关函数集锦
1.html_entity_decode() 函数把 HTML 实体转换为字符. Html_entity_decode() 是 htmlentities() 的反函数. 例子: <?Php $s ...
- 一段VUE代码:通过组件封装全局方法、自定义指令和注册组件
index.js // 插件定义第一种方式,对象:拥有 install() 方法的对象 const myPlugin = { install(app, options) { // 配置全局方法 app ...
- 解决 Ubuntu 22.04 下 flameshot 截图工具无法使用的问题
问题描述 flameshot 是 Linux 端广受好评的一款截图工具,但在 Ubuntu 22.04 中,安装完成后却不能使用,表现为截图命令无响应,或截图过程报错. 通过查阅 flameshot ...
- PHP Fatal error: Uncaught RedisException: Redis server went away in
PHP Fatal error: Uncaught RedisException: Redis server went away in 导致这个问题的原因可能有 1.redis未安装,php没有开启r ...
- RocketMq安装踩坑:docker0网桥冲突
前言 最近项目用到了RocketMq,需要在Cento7系统上搭建一套集群环境用于测试.整个的环境搭建过程中,我遇到了一个比较初级的问题:启动RocketMq的broker失败. 问题经过 首先我 ...
- 【SpringCloud】OpenFeign服务接口调用
OpenFeign服务接口调用 概述 我的理解: feign 为什么叫伪装? Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样.你不用再自己拼接url,拼 ...