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

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-最标准配置

官方参考文档&#xff1a; 动态规则扩展 alibaba/Sentinel Wiki GitHub 需要修改的代码如下&#xff1a; 为了便于后续版本集成nacos&#xff0c;简单讲一下集成思路 1.更改pom 修改sentinel-datasource-nacos的范围 将 <dependency><groupId>com.alibaba.c…...

【Linux】tail命令使用

tail 命令可用于查看文件的内容&#xff0c;有一个常用的参数 -f 常用于查阅正在改变的日志文件。 语法 tail [参数] [文件] tail命令 -Linux手册页 著者 由保罗鲁宾、大卫麦肯齐、伊恩兰斯泰勒和吉姆梅耶林撰写。 命令选项及作用 执行令 tail --help 执行命令结果 参…...

【数据结构】面试OJ题——时间复杂度2

目录 一&#xff1a;移除元素 思路&#xff1a; 二&#xff1a;删除有序数组中的重复项 思路&#xff1a; 三&#xff1a;合并两个有序数组 思路1&#xff1a; 什么&#xff1f;你不知道qsort&#xff08;&#xff09; 思路2&#xff1a; 一&#xff1a;移除元素 27. 移…...

LibreOffice编辑excel文档如何在单元格中输入手动换行符

用WPS编辑excel文档的时候&#xff0c;要在单元格中输入手动换行符&#xff0c;可以先按住Alt键&#xff0c;然后回车。 而用LibreOffice编辑excel文档&#xff0c;要在单元格中输入手动换行符&#xff0c;可以先按住Ctrl键&#xff0c;然后回车。例如&#xff1a;...

ideaSSM在线商务管理系统VS开发mysql数据库web结构java编程计算机网页源码maven项目

一、源码特点 SSM 在线商务管理系统是一套完善的信息管理系统&#xff0c;结合SSM框架和bootstrap完成本系统&#xff0c;对理解JSP java编程开发语言有帮助系统采用SSM框架&#xff08;MVC模式开发&#xff09;&#xff0c;系统具有完整的源代码 和数据库&#xff0c;系统主…...

数据结构 | 顺序表专题

数据结构 | 顺序表专题 文章目录 数据结构 | 顺序表专题课前准备1. 目标2. 需要的储备知识3. 数据结构相关概念 开始顺序表1、顺序表的概念及结构2、顺序表分类3、动态顺序表的实现初始化顺序表打印顺序表内存容量的检查顺序表的尾插顺序表的尾删顺序表的头插顺序表的头删在顺序…...

C++可视化 有穷自动机NFA 有穷自动机DFA

一、项目介绍 根据正则表达式,可视化显示NFA&#xff0c;DFA&#xff1b;词法分析程序 二、项目展示...

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> 效果&#xff1a; 3.通过npm下载并复制…...

MySQL安全基线检查

目录 安全基线检查基础知识MySQL 的安全基线检查MySQL 更严格的一些基线检查内容一、安全基线检查基础知识 1、安全基线的定义: 安全基线是一个信息系统的最小安全保证,即该信息系统最基本需要满足的安全要求。它是在安全付出成本与所能够承受的安全风险之间进行平衡的合理…...

Unity主程如何做好游戏项目管理

前言 很多小伙伴最近在面试或者考虑跳槽,可能工作了3~5年了想涨薪或想做技术总监或主程, 可自己还是个雏&#xff0c;没有做过项目技术管理&#xff0c;怎么办&#xff1f;今天我给大家梳理一下作为一个技术总监或主程你应该如何带好一个游戏项目&#xff0c;做好技术管理。接…...

103.linux5.15.198 编译 firefly-rk3399(2)

1. 平台&#xff1a; rk3399 firefly 2g16g 2. 内核&#xff1a;linux5.15.136 &#xff08;从内核镜像网站下载&#xff09; 3. 交叉编译工具 gcc version 7.5.0 (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04) 4. 宿主机&#xff1a;ubuntu18.04 5. 需要的素材和资料&#xff…...

如何从Android手机上轻松恢复误删除的短信 ?

当您使用 Android 手机时&#xff0c;您可能会误删除一些 Android 短信。如果这些消息对您很重要&#xff0c;您可能想要恢复它们。在这种情况下&#xff0c;您可以尝试使用U1tData安卓数据恢复&#xff08;奇客软件&#xff09; 来完成这项工作。这篇文章将向您展示更多信息。…...

毅速丨金属3D打印能替代传统制造吗?

金属3D打印技术已经逐渐被很多行业认可和应用&#xff0c;但是目前&#xff0c;金属3D打印多数被作为传统制造技术的一种补充&#xff0c;暂时还不能完全替代传统制造。 金属3D打印使用的是金属粉末进行选择性激光烧结&#xff0c;打印时在成型缸里铺上金属粉末&#xff0c;打印…...

21个新的ChatGPT应用

自从GPT有了图识别功能后变的更加强大&#xff0c;特别是ChatGPT的视觉技术&#xff0c;为我们提供了无数的可能性。本文将深入探讨这21种应用场景&#xff0c;帮助理解其在日常生活和工作中的实际价值。 生活助手&#xff1a;为日常生活增添色彩 健身计划定制&#xff1a;你…...

【通信原理】第二章|确知信号

前言 那么这里博主先安利一些干货满满的专栏了&#xff01; 首先是博主的高质量博客的汇总&#xff0c;这个专栏里面的博客&#xff0c;都是博主最最用心写的一部分&#xff0c;干货满满&#xff0c;希望对大家有帮助。 高质量博客汇总 文章目录 前言 第二章 确知信号1. 确知…...

【JVM】类加载器

【JVM】类加载器 文章目录 【JVM】类加载器0. 类加载器概述1. 类加载器的分类1.1 启动类加载器1.2 Java中的默认类加载器1.2.1 扩展类加载器1.2.2 应用程序类加载器 2. 双亲委派机制2.1 类的双亲委派机制是什么&#xff1f;2.2 打破双亲委派机制2.2.1 自定义类加载器2.2.2 线程…...

利用Excel支持JUnit参数化测试

在JUnit里面&#xff0c;可以使用CsvFileSource读取csv文件进行参数化测试&#xff0c;可是CSV文件不支持格式&#xff0c;编辑颇为麻烦&#xff0c;尤其是多次编辑&#xff0c;因此自然想到是否可以使用Excel文件&#xff0c;可以有各种格式&#xff0c;支持各类数据。 最新开…...

第三章 SysML入门|系统建模语言SysML实用指南学习

仅供个人学习记录 UML与SysML的联系 可以稍微参考UML与SysML的联系 UML&#xff08;统一建模语言&#xff09;和SysML&#xff08;系统建模语言&#xff09;是两种与建模相关的语言&#xff0c;它们之间存在联系和区别。 SysML的图分类如下图所示。 SysML 图概述 这里只…...

敏捷开发框架Scrum-概述

如果你是一个程序员&#xff0c;可能会觉得这是个程序开发框架。Scrum是一个敏捷开发框架。我们可以把Scrum理解成一个团队一次完成一小部分工作的方式。这种方式通过不断的实验和反馈循环来学习和改进。Scrum可以帮助团队以协作的方式逐步交付价值。 Scrum框架由一个Scrum团队…...

Hafnium启动过程分析

安全之安全(security)博客目录导读 目录 一、在安全世界中加载Hafnium和安全分区 二、通过TF-A启动 1、SP manifests 2、安全分区包 3、描述安全分区...

C++的std--allocator_traits分配器特性与自定义内存管理的适配

C标准库中的内存管理一直是个既基础又复杂的主题。std::allocator_traits作为C11引入的分配器特性模板&#xff0c;为自定义内存管理提供了统一的适配接口&#xff0c;让开发者能在不重写整套分配逻辑的情况下&#xff0c;灵活扩展内存管理策略。无论是实现高性能内存池&#x…...

2026实测不踩坑!6款成品PPT网站客观测评

2026实测不踩坑&#xff01;6款成品PPT网站客观测评作为常年深耕AI工具测评的博主&#xff0c;日常需应对各类PPT创作需求&#xff0c;也经常收到粉丝咨询相关工具选择。经过实测多款成品PPT网站后&#xff0c;整理出6款适配性较强的平台&#xff0c;涵盖不同需求场景&#xff…...

基于OpenCV的边缘梯度模板匹配:代码与分析

基于Opencv边缘梯度模板匹配源码&#xff0c;今天&#xff0c;我决定深入研究一下基于OpenCV的边缘梯度模板匹配算法。说实话&#xff0c;这个算法听起来有点高大上&#xff0c;但我觉得只要一步步来&#xff0c;一定能搞明白。 什么是边缘梯度模板匹配&#xff1f; 边缘梯度模…...

中兴光猫配置解密工具:突破运营商限制,掌握家庭网络自主权

中兴光猫配置解密工具&#xff1a;突破运营商限制&#xff0c;掌握家庭网络自主权 【免费下载链接】ZET-Optical-Network-Terminal-Decoder 项目地址: https://gitcode.com/gh_mirrors/ze/ZET-Optical-Network-Terminal-Decoder 在家庭网络管理中&#xff0c;你是否曾因…...

把openEuler当微服务跑:Docker Compose编排实战,管理Nginx+MySQL多容器应用

微服务架构下的openEuler容器化实践&#xff1a;NginxMySQL多容器编排指南 1. 云原生时代的轻量级操作系统选择 在容器化技术席卷全球的今天&#xff0c;开发者们越来越倾向于将操作系统本身也视为可编排的服务单元。openEuler作为一款专为云原生场景优化的Linux发行版&#xf…...

南北阁模型新玩法:一键部署极简WebUI,体验手机短信般AI对话

南北阁模型新玩法&#xff1a;一键部署极简WebUI&#xff0c;体验手机短信般AI对话 还在用那些界面老旧、反应迟钝的AI对话工具吗&#xff1f;每次发送问题后&#xff0c;只能盯着屏幕上的加载图标干等&#xff0c;几秒甚至十几秒后才能看到一大段文字“啪”地一下弹出来&…...

片上网络NOC:可生成RTL源代码与UVM验证环境的实用学习资料

片上网络NOC&#xff0c;可生成RTL源代码&#xff0c;生成uvm验证环境&#xff0c;内含有丰富的文档&#xff0c;带有readme文档&#xff0c;有例子工程&#xff0c;操作简单&#xff0c;是学习工作的好资料最近折腾NoC项目的时候挖到一个宝藏工具包&#xff0c;名字先不透露&a…...

Linux内核工程师面试高频问题解析

1. Linux内核工程师面试核心问题解析作为一名在Linux内核领域摸爬滚打多年的老手&#xff0c;我经历过无数次技术面试的洗礼。今天就把阿里云这类一线大厂在Linux内核工程师岗位上的高频面试题做个系统梳理&#xff0c;并附上我个人的解题思路和实战经验。这些题目看似基础&…...

MelonLoader终极指南:Unity游戏Mod加载器从入门到精通

MelonLoader终极指南&#xff1a;Unity游戏Mod加载器从入门到精通 【免费下载链接】MelonLoader The Worlds First Universal Mod Loader for Unity Games compatible with both Il2Cpp and Mono 项目地址: https://gitcode.com/gh_mirrors/me/MelonLoader 还在为Unity游…...

终端里的“皇帝新衣”:扒开 Claude Code 的源码,我看到了 Agent 的求生欲

下午三点&#xff0c;阳光斜着打在机械键盘的侧边&#xff0c;你刚解决完一个诡异的内存溢出&#xff0c;正打算接杯咖啡。 顺手更新了 Anthropic 刚发布的 Claude Code&#xff0c;这个号称能直接在终端里帮你写代码、改 bug、跑测试的“神级工具”。 [外链图片转存中…(img…...