sentinel规则持久化-规则同步nacos-最标准配置
官方参考文档:
动态规则扩展 · alibaba/Sentinel Wiki · GitHub
需要修改的代码如下:

为了便于后续版本集成nacos,简单讲一下集成思路
1.更改pom
修改sentinel-datasource-nacos的范围
将
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId><scope>test</scope>
</dependency>
改为
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId><!--<scope>test</scope>-->
</dependency>
2.拷贝示例
将test目录下的com.alibaba.csp.sentinel.dashboard.rule.nacos包下的内容拷贝到src的 com.alibaba.csp.sentinel.dashboard.rule的目录
test目录只包含限流,其他规则参照创建即可。
创建时注意修改常量,并且在NacosConfig实现各种converter
注意:授权规则和热点规则需要特殊处理,否则nacos配置不生效。
因为授权规则Entity比流控规则Entity多包了一层。
public class FlowRuleEntity implements RuleEntity
public class AuthorityRuleEntity extends AbstractRuleEntity<AuthorityRule>
以授权规则为例
AuthorityRuleNacosProvider.java
/** Copyright 1999-2018 Alibaba Group Holding Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package com.alibaba.csp.sentinel.dashboard.rule.nacos.authority;import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.ArrayList;
import java.util.List;/*** @author Eric Zhao* @since 1.4.0*/
@Component("authorityRuleNacosProvider")
public class AuthorityRuleNacosProvider implements DynamicRuleProvider<List<AuthorityRuleEntity>> {@Autowiredprivate ConfigService configService;@Autowiredprivate Converter<String, List<AuthorityRuleEntity>> converter;@Overridepublic List<AuthorityRuleEntity> getRules(String appName) throws Exception {String rules = configService.getConfig(appName + NacosConfigUtil.AUTHORITY_DATA_ID_POSTFIX,NacosConfigUtil.GROUP_ID, 3000);if (StringUtil.isEmpty(rules)) {return new ArrayList<>();}return converter.convert(this.parseRules(rules));}private String parseRules(String rules) {JSONArray newRuleJsons = new JSONArray();JSONArray ruleJsons = JSONArray.parseArray(rules);for (int i = 0; i < ruleJsons.size(); i++) {JSONObject ruleJson = ruleJsons.getJSONObject(i);AuthorityRuleEntity ruleEntity = JSON.parseObject(ruleJson.toJSONString(), AuthorityRuleEntity.class);JSONObject newRuleJson = JSON.parseObject(JSON.toJSONString(ruleEntity));AuthorityRule rule = JSON.parseObject(ruleJson.toJSONString(), AuthorityRule.class);newRuleJson.put("rule", rule);newRuleJsons.add(newRuleJson);}return newRuleJsons.toJSONString();}
}
AuthorityRuleNacosPublisher.java
/** Copyright 1999-2018 Alibaba Group Holding Ltd.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package com.alibaba.csp.sentinel.dashboard.rule.nacos.authority;import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.List;/*** @author Eric Zhao* @since 1.4.0*/
@Component("authorityRuleNacosPublisher")
public class AuthorityRuleNacosPublisher implements DynamicRulePublisher<List<AuthorityRuleEntity>> {@Autowiredprivate ConfigService configService;@Autowiredprivate Converter<List<AuthorityRuleEntity>, String> converter;@Overridepublic void publish(String app, List<AuthorityRuleEntity> rules) throws Exception {AssertUtil.notEmpty(app, "app name cannot be empty");if (rules == null) {return;}configService.publishConfig(app + NacosConfigUtil.AUTHORITY_DATA_ID_POSTFIX,NacosConfigUtil.GROUP_ID, this.parseRules(converter.convert(rules)));}private String parseRules(String rules) {JSONArray oldRuleJsons = JSONArray.parseArray(rules);for (int i = 0; i < oldRuleJsons.size(); i++) {JSONObject oldRuleJson = oldRuleJsons.getJSONObject(i);JSONObject ruleJson = oldRuleJson.getJSONObject("rule");oldRuleJson.putAll(ruleJson);oldRuleJson.remove("rule");}return oldRuleJsons.toJSONString();}
}
热点规则同理
3.修改controller
v2目录的FlowControllerV2
将
@Autowired@Qualifier("flowRuleDefaultProvider")private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;@Autowired@Qualifier("flowRuleDefaultPublisher")private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
改为
@Autowired@Qualifier("flowRuleNacosProvider")private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;@Autowired@Qualifier("flowRuleNacosPublisher")private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
controller目录的其他controller包括
AuthorityRuleController DegradeController FlowControllerV1 ParamFlowRuleController SystemController
做如下更改(以DegradeController为例)
将
@Autowiredprivate SentinelApiClient sentinelApiClient;
改成
@Autowired@Qualifier("degradeRuleNacosProvider")private DynamicRuleProvider<List<DegradeRuleEntity>> ruleProvider;@Autowired@Qualifier("degradeRuleNacosPublisher")private DynamicRulePublisher<List<DegradeRuleEntity>> rulePublisher;
将原有publishRules方法删除,统一改成
private void publishRules(/*@NonNull*/ String app) throws Exception {List<DegradeRuleEntity> rules = repository.findAllByApp(app);rulePublisher.publish(app, rules);}
之后解决报错的地方即可。
获取所有rules的地方
将
List<DegradeRuleEntity> rules = sentinelApiClient.fetchDegradeRuleOfMachine(app, ip, port);
改成
List<DegradeRuleEntity> rules = ruleProvider.getRules(app);
原有调用publishRules方法的地方,删除掉
在上一个try catch方法里加上
publishRules(entity.getApp());
这里的entity.getApp()也有可能是oldEntity.getApp()/app等变量。根据删除的publishRules代码片段推测即可。
4.修改前端文件
文件路径:src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html
将
<a ui-sref="dashboard.flowV1({app: entry.app})">
改成
<a ui-sref="dashboard.flow({app: entry.app})">
5.最后打包即可
执行命令打包成jar
mvn clean package
运行方法与官方jar一致,不做赘述
6.微服务程序集成
pom.xml添加
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId></dependency>
配置文件application.yml添加
spring:cloud:sentinel:datasource:# 名称随意flow:nacos:server-addr: localhost:8848dataId: ${spring.application.name}-flow-rulesgroupId: SENTINEL_GROUP# 规则类型,取值见:# org.springframework.cloud.alibaba.sentinel.datasource.RuleTyperule-type: flowdegrade:nacos:server-addr: localhost:8848dataId: ${spring.application.name}-degrade-rulesgroupId: SENTINEL_GROUPrule-type: degradesystem:nacos:server-addr: localhost:8848dataId: ${spring.application.name}-system-rulesgroupId: SENTINEL_GROUPrule-type: systemauthority:nacos:server-addr: localhost:8848dataId: ${spring.application.name}-authority-rulesgroupId: SENTINEL_GROUPrule-type: authorityparam-flow:nacos:server-addr: localhost:8848dataId: ${spring.application.name}-param-flow-rulesgroupId: SENTINEL_GROUPrule-type: param-flow
7.测试验证
在sentinel控制台界面添加几个流控规则后尝试关闭微服务和sentinel,然后重新打开sentinel和微服务,看流控规则是否还在。
8.最后把配置好的代码发给大家,大家可以自行下载,里边有运行访问流程
https://download.csdn.net/download/qq_34091529/88482757
相关文章:
sentinel规则持久化-规则同步nacos-最标准配置
官方参考文档: 动态规则扩展 alibaba/Sentinel Wiki GitHub 需要修改的代码如下: 为了便于后续版本集成nacos,简单讲一下集成思路 1.更改pom 修改sentinel-datasource-nacos的范围 将 <dependency><groupId>com.alibaba.c…...
【Linux】tail命令使用
tail 命令可用于查看文件的内容,有一个常用的参数 -f 常用于查阅正在改变的日志文件。 语法 tail [参数] [文件] tail命令 -Linux手册页 著者 由保罗鲁宾、大卫麦肯齐、伊恩兰斯泰勒和吉姆梅耶林撰写。 命令选项及作用 执行令 tail --help 执行命令结果 参…...
【数据结构】面试OJ题——时间复杂度2
目录 一:移除元素 思路: 二:删除有序数组中的重复项 思路: 三:合并两个有序数组 思路1: 什么?你不知道qsort() 思路2: 一:移除元素 27. 移…...
LibreOffice编辑excel文档如何在单元格中输入手动换行符
用WPS编辑excel文档的时候,要在单元格中输入手动换行符,可以先按住Alt键,然后回车。 而用LibreOffice编辑excel文档,要在单元格中输入手动换行符,可以先按住Ctrl键,然后回车。例如:...
ideaSSM在线商务管理系统VS开发mysql数据库web结构java编程计算机网页源码maven项目
一、源码特点 SSM 在线商务管理系统是一套完善的信息管理系统,结合SSM框架和bootstrap完成本系统,对理解JSP java编程开发语言有帮助系统采用SSM框架(MVC模式开发),系统具有完整的源代码 和数据库,系统主…...
数据结构 | 顺序表专题
数据结构 | 顺序表专题 文章目录 数据结构 | 顺序表专题课前准备1. 目标2. 需要的储备知识3. 数据结构相关概念 开始顺序表1、顺序表的概念及结构2、顺序表分类3、动态顺序表的实现初始化顺序表打印顺序表内存容量的检查顺序表的尾插顺序表的尾删顺序表的头插顺序表的头删在顺序…...
C++可视化 有穷自动机NFA 有穷自动机DFA
一、项目介绍 根据正则表达式,可视化显示NFA,DFA;词法分析程序 二、项目展示...
vite vue3 ts 使用sass 设置样式变量 和重置默认样式
1.安装scss 样式支持依赖 yarn add -D sass 2.使用sass <div><!-- 测试使用sass --><h1>测试使用sass</h1> </div><style scope lang"scss"> div {h1 {color: red;} } </style> 效果: 3.通过npm下载并复制…...
MySQL安全基线检查
目录 安全基线检查基础知识MySQL 的安全基线检查MySQL 更严格的一些基线检查内容一、安全基线检查基础知识 1、安全基线的定义: 安全基线是一个信息系统的最小安全保证,即该信息系统最基本需要满足的安全要求。它是在安全付出成本与所能够承受的安全风险之间进行平衡的合理…...
Unity主程如何做好游戏项目管理
前言 很多小伙伴最近在面试或者考虑跳槽,可能工作了3~5年了想涨薪或想做技术总监或主程, 可自己还是个雏,没有做过项目技术管理,怎么办?今天我给大家梳理一下作为一个技术总监或主程你应该如何带好一个游戏项目,做好技术管理。接…...
103.linux5.15.198 编译 firefly-rk3399(2)
1. 平台: rk3399 firefly 2g16g 2. 内核:linux5.15.136 (从内核镜像网站下载) 3. 交叉编译工具 gcc version 7.5.0 (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04) 4. 宿主机:ubuntu18.04 5. 需要的素材和资料ÿ…...
如何从Android手机上轻松恢复误删除的短信 ?
当您使用 Android 手机时,您可能会误删除一些 Android 短信。如果这些消息对您很重要,您可能想要恢复它们。在这种情况下,您可以尝试使用U1tData安卓数据恢复(奇客软件) 来完成这项工作。这篇文章将向您展示更多信息。…...
毅速丨金属3D打印能替代传统制造吗?
金属3D打印技术已经逐渐被很多行业认可和应用,但是目前,金属3D打印多数被作为传统制造技术的一种补充,暂时还不能完全替代传统制造。 金属3D打印使用的是金属粉末进行选择性激光烧结,打印时在成型缸里铺上金属粉末,打印…...
21个新的ChatGPT应用
自从GPT有了图识别功能后变的更加强大,特别是ChatGPT的视觉技术,为我们提供了无数的可能性。本文将深入探讨这21种应用场景,帮助理解其在日常生活和工作中的实际价值。 生活助手:为日常生活增添色彩 健身计划定制:你…...
【通信原理】第二章|确知信号
前言 那么这里博主先安利一些干货满满的专栏了! 首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助。 高质量博客汇总 文章目录 前言 第二章 确知信号1. 确知…...
【JVM】类加载器
【JVM】类加载器 文章目录 【JVM】类加载器0. 类加载器概述1. 类加载器的分类1.1 启动类加载器1.2 Java中的默认类加载器1.2.1 扩展类加载器1.2.2 应用程序类加载器 2. 双亲委派机制2.1 类的双亲委派机制是什么?2.2 打破双亲委派机制2.2.1 自定义类加载器2.2.2 线程…...
利用Excel支持JUnit参数化测试
在JUnit里面,可以使用CsvFileSource读取csv文件进行参数化测试,可是CSV文件不支持格式,编辑颇为麻烦,尤其是多次编辑,因此自然想到是否可以使用Excel文件,可以有各种格式,支持各类数据。 最新开…...
第三章 SysML入门|系统建模语言SysML实用指南学习
仅供个人学习记录 UML与SysML的联系 可以稍微参考UML与SysML的联系 UML(统一建模语言)和SysML(系统建模语言)是两种与建模相关的语言,它们之间存在联系和区别。 SysML的图分类如下图所示。 SysML 图概述 这里只…...
敏捷开发框架Scrum-概述
如果你是一个程序员,可能会觉得这是个程序开发框架。Scrum是一个敏捷开发框架。我们可以把Scrum理解成一个团队一次完成一小部分工作的方式。这种方式通过不断的实验和反馈循环来学习和改进。Scrum可以帮助团队以协作的方式逐步交付价值。 Scrum框架由一个Scrum团队…...
Hafnium启动过程分析
安全之安全(security)博客目录导读 目录 一、在安全世界中加载Hafnium和安全分区 二、通过TF-A启动 1、SP manifests 2、安全分区包 3、描述安全分区...
2026年腾讯云OpenClaw/Hermes Agent配置Token Plan搭建保姆教程
2026年腾讯云OpenClaw/Hermes Agent配置Token Plan搭建保姆教程。OpenClaw是开源的个人AI助手,Hermes Agent则是一个能自我进化的AI智能体框架。阿里云提供计算巢、轻量服务器及无影云电脑三种部署OpenClaw 与 Hermes Agent的方案、百炼Token Plan兼容主流 AI 工具&…...
城市供水管网抗震可靠性分析方法与系统开发【附程序】
✨ 长期致力于供水管网、抗震可靠性、修复策略、震害预测、系统开发研究工作,擅长数据搜集与处理、建模仿真、程序编写、仿真设计。 ✅ 专业定制毕设、代码 ✅ 如需沟通交流,点击《获取方式》 (1)场地效应预测模型与管道地震易损性…...
华硕笔记本终极控制神器:G-Helper轻量化替代方案完整指南
华硕笔记本终极控制神器:G-Helper轻量化替代方案完整指南 【免费下载链接】g-helper Lightweight Armoury Crate alternative for Asus laptops with nearly the same functionality. Works with ROG Zephyrus, Flow, TUF, Strix, Scar, ProArt, Vivobook, Zenbook,…...
本地虚拟机停电启动异常:原理、诊断与四步修复
1. 停电不是“按了关机键”,而是对虚拟化环境的一次暴力断电冲击你有没有经历过这样的场景:凌晨三点,小区突然跳闸,家里那台跑着三台生产级虚拟机的NUC主机黑屏了;第二天早上开机,宿主机系统能进࿰…...
【tomcat部署前台war包报错】
tomcat部署前台war包报错 背景:tomcat启动前台war包,由zip直接改文件后缀成war包,jdk8 同事好使,我不好使 部署平台日志: 报错一、正常tomcat执行时会把war包解压成对应文件夹,这里应该是没解压成功。没有具…...
软件架构分析方法SAAM、ATAM与CBAM
一、SAAM(软件架构分析方法) 1. 核心思路 基于场景,评估架构对可修改性(以及可移植性、可扩充性)的支持程度。 关键是区分 直接场景(现有架构直接支持)和 间接场景(需要修改架构)。 通过分析间接场景的数量与修改代价,定位高风险、高耦合的模块。 2. 典型案例:内…...
FinalBurn Neo:一场跨越时空的街机游戏考古之旅
FinalBurn Neo:一场跨越时空的街机游戏考古之旅 【免费下载链接】FBNeo FinalBurn Neo - We are Team FBNeo. 项目地址: https://gitcode.com/gh_mirrors/fb/FBNeo 在数字时代的洪流中,有一群守护者正在用代码为经典街机游戏搭建永生的方舟。Fina…...
为什么你的盐印相总像P图?:Midjourney v6.2最新盐印相渲染漏洞(已验证387组测试图)及绕过方案
更多请点击: https://intelliparadigm.com 第一章:为什么你的盐印相总像P图? 盐印相(Salted Paper Print)作为19世纪早期摄影工艺的代表,其本质是依靠氯化钠与硝酸银反应生成感光氯化银,在阳光…...
鸿蒙备考题库页面构建:今日计划与题目预览模块的详细解析
鸿蒙备考题库页面构建:今日计划与题目预览模块的详细解析 前言 在 HarmonyOS 6.0 应用开发中,在线教育类页面的学习计划展示和题目练习模块是用户停留时间最长的核心区域。本文将以“备考题库”应用中的“今日学习计划”任务列表和“题目预览”答题卡片为…...
企业级微服务架构解决方案:Abp Vnext Pro框架的5大技术优势解析
企业级微服务架构解决方案:Abp Vnext Pro框架的5大技术优势解析 【免费下载链接】abp-vnext-pro Abp Vnext 的 Vue 实现版本 项目地址: https://gitcode.com/gh_mirrors/ab/abp-vnext-pro Abp Vnext Pro是一个基于ABP框架和Vue.js技术栈构建的企业级开发平台…...
