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
相关推荐
guozhetao4 分钟前
【ST表、倍增】P7167 [eJOI 2020] Fountain (Day1)
java·c++·python·算法·leetcode·深度优先·图论
技术思考者4 分钟前
基础很薄弱如何规划考研
java·经验分享·考研
●VON28 分钟前
重生之我在暑假学习微服务第二天《MybatisPlus-下篇》
java·学习·微服务·架构·mybatis-plus
老华带你飞28 分钟前
口腔助手|口腔挂号预约小程序|基于微信小程序的口腔门诊预约系统的设计与实现(源码+数据库+文档)
java·数据库·微信小程序·小程序·论文·毕设·口腔小程序
hqxstudying38 分钟前
J2EE模式---服务层模式
java·数据库·后端·spring·oracle·java-ee
GM_82843 分钟前
【最新最完整】SpringAI-1.0.0开发MCP Server,搭建MCP Client 实战笔记(进阶+详细+完整代码)
java·后端·ai编程·springai·mcp
都叫我大帅哥44 分钟前
Java DelayQueue:时间管理大师的终极武器
java
秋千码途1 小时前
小架构step系列27:Hibernate提供的validator
java·spring·架构·hibernate
都叫我大帅哥1 小时前
TOGAF迁移规划阶段全解密:从菜鸟到达人的通关秘籍
java
探索java1 小时前
深入理解 Spring 中的 XmlBeanFactory 原理及实践
java·spring·xmlbeanfactory