Sentinel规则持久化

首先 Sentinel 控制台通过 API 将规则推送至客户端并更新到内存中,接着注册的写数据源会将新的规则保存到本地的文件中。

示例代码:

1.编写处理类

java 复制代码
//规则持久化
public class FilePersistence implements InitFunc {
	@Value("spring.application:name")
	private String appcationName;
	
	@Override
	public void init() throws Exception {
	String ruleDir = System.getProperty("user.home") + "/sentinelrules/"+appcationName;
	String flowRulePath = ruleDir + "/flow-rule.json";
	String degradeRulePath = ruleDir + "/degrade-rule.json";
	String systemRulePath = ruleDir + "/system-rule.json";
	String authorityRulePath = ruleDir + "/authority-rule.json";
	String paramFlowRulePath = ruleDir + "/param-flow-rule.json";
	this.mkdirIfNotExits(ruleDir);
	this.createFileIfNotExits(flowRulePath);
	this.createFileIfNotExits(degradeRulePath);
	this.createFileIfNotExits(systemRulePath);
	this.createFileIfNotExits(authorityRulePath);
	this.createFileIfNotExits(paramFlowRulePath);
	
	// 流控规则
	ReadableDataSource<String, List<FlowRule>> flowRuleRDS = new FileRefreshableDataSource<>(
		flowRulePath,
		flowRuleListParser
	);
	FlowRuleManager.register2Property(flowRuleRDS.getProperty());
	WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(
		flowRulePath,
		this::encodeJson
	);
	WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);
	
	// 降级规则
	ReadableDataSource<String, List<DegradeRule>> degradeRuleRDS = new FileRefreshableDataSource<>(
		degradeRulePath,
		degradeRuleListParser
	);
	DegradeRuleManager.register2Property(degradeRuleRDS.getProperty());
	WritableDataSource<List<DegradeRule>> degradeRuleWDS = new FileWritableDataSource<>(
		degradeRulePath,
		this::encodeJson
	);
	WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS);
	
	// 系统规则
	ReadableDataSource<String, List<SystemRule>> systemRuleRDS = new FileRefreshableDataSource<>(
		systemRulePath,
		systemRuleListParser
	);
	SystemRuleManager.register2Property(systemRuleRDS.getProperty());
	WritableDataSource<List<SystemRule>> systemRuleWDS = new FileWritableDataSource<>(
		systemRulePath,
		this::encodeJson
	);
	WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS);
	
	// 授权规则
	ReadableDataSource<String, List<AuthorityRule>> authorityRuleRDS = new FileRefreshableDataSource<>(
		authorityRulePath,
		authorityRuleListParser
	);
	AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty());
	WritableDataSource<List<AuthorityRule>> authorityRuleWDS = new FileWritableDataSource<>(
		authorityRulePath,
		this::encodeJson
	);
	WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS);
	
	// 热点参数规则
	ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRDS = new FileRefreshableDataSource<>(
		paramFlowRulePath,
		paramFlowRuleListParser
	);
	ParamFlowRuleManager.register2Property(paramFlowRuleRDS.getProperty());
	WritableDataSource<List<ParamFlowRule>> paramFlowRuleWDS = new FileWritableDataSource<>(
		paramFlowRulePath,
		this::encodeJson
	);
	ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS);
	}
	private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(
		source,
		new TypeReference<List<FlowRule>>() {
		}
	);
	private Converter<String, List<DegradeRule>> degradeRuleListParser = source -> JSON.parseObject(
		source,
		new TypeReference<List<DegradeRule>>() {
		}
	);
	private Converter<String, List<SystemRule>> systemRuleListParser = source -> JSON.parseObject(
		source,
		new TypeReference<List<SystemRule>>() {
		}
	);
	private Converter<String, List<AuthorityRule>> authorityRuleListParser = source -> JSON.parseObject(
		source,
		new TypeReference<List<AuthorityRule>>() {
		}
	);
	private Converter<String, List<ParamFlowRule>> paramFlowRuleListParser = source -> JSON.parseObject(
		source,
		new TypeReference<List<ParamFlowRule>>() {
		}
	);
	private void mkdirIfNotExits(String filePath) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.mkdirs();
		}
	}
	private void createFileIfNotExits(String filePath) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
	}
	private <T> String encodeJson(T t) {
		return JSON.toJSONString(t);
	}
}

2.添加配置

在resources下创建配置目录 META-INF/services ,然后添加文件 com.alibaba.csp.sentinel.init.InitFunc 在文件中添加配置类的全路径

java 复制代码
com.zxt.config.FilePersistence
相关推荐
互联网全栈架构28 分钟前
遨游Spring AI:第一盘菜Hello World
java·人工智能·后端·spring
优秀的颜1 小时前
计算机基础知识(第五篇)
java·开发语言·分布式
BillKu1 小时前
Java严格模式withResolverStyle解析日期错误及解决方案
java
网安INF2 小时前
ElGamal加密算法:离散对数难题的安全基石
java·网络安全·密码学
AWS官方合作商3 小时前
在CSDN发布AWS Proton解决方案:实现云原生应用的标准化部署
java·云原生·aws
gadiaola3 小时前
【JVM】Java虚拟机(二)——垃圾回收
java·jvm
coderSong25686 小时前
Java高级 |【实验八】springboot 使用Websocket
java·spring boot·后端·websocket
Mr_Air_Boy7 小时前
SpringBoot使用dynamic配置多数据源时使用@Transactional事务在非primary的数据源上遇到的问题
java·spring boot·后端
豆沙沙包?7 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
年老体衰按不动键盘8 小时前
快速部署和启动Vue3项目
java·javascript·vue