当前位置: 首页 > news >正文

Sentinel规则持久化

在这里插入图片描述

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

示例代码:

1.编写处理类

//规则持久化
public class FilePersistence implements InitFunc {@Value("spring.application:name")private String appcationName;@Overridepublic 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 在文件中添加配置类的全路径

com.zxt.config.FilePersistence
http://www.lryc.cn/news/130831.html

相关文章:

  • Transformer 相关模型的参数量计算
  • 企业信息化过程----应用管理平台的构建过程
  • 揭秘程序员的鄙视链,你在哪一层?看完我想哭
  • 在docker下进行mysql的主从复制
  • 【机器学习】处理不平衡的数据集
  • JVM前世今生之JVM内存模型
  • redis事务对比Lua脚本区别是什么
  • Java“牵手”根据店铺ID获取1688店铺所有商品数据方法,1688API实现批量店铺商品数据抓取示例
  • linux-shell脚本收集
  • 使用 MBean 和 日志查看 Tomcat 线程池核心属性数据
  • Visual Studio 2019源码编译cpu版本onnxruntime
  • Go和Java实现模板模式
  • angular:quill align的坑
  • 设计模式篇---抽象工厂(包含优化)
  • Azure创建可用性集
  • SpringBoot中优雅的实现隐私数据脱敏(提供Gitee源码)
  • Elasticsearch集群shard过多后导致的性能问题分析
  • Unity框架学习--5 事件中心管理器
  • (二)结构型模式:3、过滤器模式(Filter、Criteria Pattern)(C++示例)
  • 谷歌在Chrome浏览器中推进抗量子加密技术
  • Kotlin的数组
  • centos 安装docker
  • Oracle-如何判断字符串包含中文字符串(汉字),删除中文内容及保留中文内容
  • File 类的用法, InputStream和Reader, OutputStream和Writer 的用法
  • AtCoder Beginner Contest 315 Task:A/B/C/E
  • 【项目实践】基于LSTM的一维数据扩展与预测
  • webshell实践,在nginx上实现负载均衡
  • LVS+Keepalived集群
  • Java的网络编程
  • kafka配置远程连接