江苏系统建站怎么用,昌邑微信网站建设公司,企业营销网站建设费用,重庆网站seo昔年优化前言#xff1a;
前面系列文章我们对 Sentinel 的作用及工作流程源码进行了分析#xff0c;我们知道 Sentinel 的众多功能都是通过规则配置完成的#xff0c;但是我们前面在演示的时候#xff0c;发现 Sentinel 一重启#xff0c;配置的规则就没有了#xff0c;这是因为…前言
前面系列文章我们对 Sentinel 的作用及工作流程源码进行了分析我们知道 Sentinel 的众多功能都是通过规则配置完成的但是我们前面在演示的时候发现 Sentinel 一重启配置的规则就没有了这是因为规则存储在内存中本篇我们来实现 Sentinel 规则持久化到 Nacos 中。
Sentinel 系列文章传送门
Sentinel 初步认识及使用
Sentinel 核心概念和工作流程详解
Spring Cloud 整合 Nacos、Sentinel、OpenFigen 实战【微服务熔断降级实战】
Sentinel 源码分析入门【Entry、Chain、Context】
Sentine 源码分析之–NodeSelectorSlot、ClusterBuilderSlot、StatisticSlot
Sentine 源码分析之–AuthoritySlot、SystemSlot、GatewayFlowSlot
Sentine 源码分析之–ParamFlowSlot
Sentine 源码分析之–FlowSlot
Sentinel 滑动时间窗口源码分析
Sentinel 的三种规则管理模式
原始模式规则直接保存在内存中这种模式比较简单不依赖外部组件但是有一个致命的问题重启规则就不存在了生产环境不可能使用这种模式。Pull 模式控制台将配置的规则推送到 Sentinel客户端而客户端会将配置规则保存在本地文件或数据库中定时去本地文件或数据库中查询更新本地规则这种模式也比较简单不依赖外部组件但是实时性不保证拉取过于频繁也可能会有性能问题因此也不建议生产环境使用。Push 模式控制台将配置规则推送到远程配置中心例如 Nacos、ZooKeeper、Apollo 等Sentinel 客户端监听 Nacos、ZooKeeper、Apollo获取配置变更的推送消息完成本地配置更新推荐生产环境使用。
本篇分享的是使用 Nacos 来实现 Sentinel 规则持久化。
Sentinel 源码改造
后端源码修改
Sentinel Dashboard 默认不支持 Nacos 的持久化需要修改 Sentinel Dashboard 源码。
1.修改 pom.xml 文件在 sentinel-dashboard 源码 pom 文件中Nacos 的依赖默认的 scope 是 test表示只能在测试时使用这里要去掉如下
!-- for Nacos rule publisher sample --
dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-datasource-nacos/artifactId!-- scopetest/scope--
/dependency2.添加对 Nacos 的支持Sentinel 已经提供了 Nacos 的支持只不过代码在 src/test/rule 下面我们需要把他复制到 src/test/rule 下面如下 3.修改 Nacos 地址也就是对 NacosConfig 源码进行修改修改后的源码如下
package com.alibaba.csp.sentinel.dashboard.rule.nacos;import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.List;
import java.util.Properties;/*** author Eric Zhao* since 1.4.0*/
Configuration
ConfigurationProperties(prefix nacos)//需要增加的
public class NacosConfig {//需要自己增加的 Nacos 地址private String addr;//需要自己增加的 Nacos 地址private String namespace;//需要自己增加的public String getAddr() {return addr;}//需要自己增加的public void setNamespace(String namespace) {this.namespace namespace;}//需要自己增加的public String getNamespace() {return namespace;}//需要自己增加的public void setAddr(String addr) {this.addr addr;}Beanpublic ConverterListFlowRuleEntity, String flowRuleEntityEncoder() {return JSON::toJSONString;}Beanpublic ConverterString, ListFlowRuleEntity flowRuleEntityDecoder() {return s - JSON.parseArray(s, FlowRuleEntity.class);}/*Beanpublic ConfigService nacosConfigService() throws Exception {return ConfigFactory.createConfigService(localhost);}*///修改后的注入 ConfigService 方法Beanpublic ConfigService nacosConfigService() throws Exception {Properties properties new Properties();properties.put(PropertyKeyConst.SERVER_ADDR, addr);properties.put(PropertyKeyConst.NAMESPACE, namespace);return ConfigFactory.createConfigService(properties);}
}
修改 Nacos 源码后还需要修改 sentinel-dashboard 的 application.properties 中的 nacos 地址如下
nacos.addrlocalhost:8848
nacos.namespacesentinel
配置nacos数据源需要修改 com.alibaba.csp.sentinel.dashboard.controller.v2 包下的 FlowControllerV2 类修改源码如下
public class FlowControllerV2 {private final Logger logger LoggerFactory.getLogger(FlowControllerV2.class);Autowiredprivate InMemoryRuleRepositoryAdapterFlowRuleEntity repository;Autowired//注释掉默认的 flowRuleDefaultProvider//Qualifier(flowRuleDefaultProvider)Qualifier(flowRuleNacosProvider)private DynamicRuleProviderListFlowRuleEntity ruleProvider;Autowired注释掉默认的 flowRuleDefaultPublisher//Qualifier(flowRuleDefaultPublisher)Qualifier(flowRuleNacosPublisher)private DynamicRulePublisherListFlowRuleEntity rulePublisher;//省略部分代码。。。。。。}
前端源码修改
1.修改流控规则找到 resources/app/scripts/directives/sidebar/sidebar.html 文件中的流控规则修改后源码如下 2.修改簇点链路找到resources/app/scripts/controllers/identity.js 文件把 FlowServiceV1 改成 FlowServiceV2修改后源码如下 3.修改保持流控规则接口在 identity.js 文件中找到 saveFlowRule 方法进行改动把 /dashboard/flow/ 换成 /dashboard/v2/flow/修改后源码如下 项目准备
1.引入 Sentinel 和 Nacos 依赖如下:
!--引入 Nacos 支持--
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactIdversion2021.1/version
/dependencydependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactIdversion2021.1/version
/dependency!--引入 sentinel 支持--
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactIdversion2021.1/version
/dependencydependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-datasource-nacos/artifactIdversion1.8.0/version
/dependency文末会附上完整的 pom.xml 文件。
2.bootstrap.properties 中的 Nacos 和 Sentinel 配置如下
#服务名称
spring.application.nameorder-service
#服务端口
server.port8081
#区分不同环境的配置文件
spring.profiles.activedev
#nacos 账号密码
spring.cloud.nacos.usernamenacos
spring.cloud.nacos.passwordnacos
#nacos 地址
spring.cloud.nacos.server-addr127.0.0.1:8848
#名称空间 默认 public
spring.cloud.nacos.discovery.namespacesentinel#命名空间 ID 用于区分不同环境和应用 默认的 public 空间时候无需配置(或者直接留空即可) 否侧配置中心不生效
#spring.cloud.nacos.config.namespaced5a53ce5-4288-401f-a748-f5c79bbd3ab3
spring.cloud.nacos.config.namespace9bd69b59-f311-4f34-81c3-aaa4314dfbeb
#配置分组 默认即可 也可以自定义分组
spring.cloud.nacos.config.groupDEFAULT_GROUP
#默认为 spring.application.name 的值 也可以通过配置项 spring.cloud.nacos.config.prefix 来配置
spring.cloud.nacos.config.prefixmy-study-spring-boot
#配置名称 首先使用配置的前缀 然后再使用名称 最后使用 spring.application.name
spring.cloud.nacos.config.namemy-study-spring-boot
#配置文件格式后缀 默认为 properties
spring.cloud.nacos.config.file-extensionproperties
#用于控制是否启用配置刷新功能 默认为true
spring.cloud.nacos.config.refresh-enabledtrue
#配置拉取长轮询超时时间 单位为毫秒 默认为 30000 毫秒
spring.cloud.nacos.config.timeout3000#sentinel dashboard 地址
spring.cloud.sentinel.transport.dashboardlocalhost:8080
spring.cloud.sentinel.transport.port8719
#Nacos 地址
spring.cloud.sentinel.datasource.flow.nacos.server-addr127.0.0.1:8848
#spring.cloud.sentinel.datasource.flow.nacos.namespace9bd69b59-f311-4f34-81c3-aaa4314dfbeb
#这里的这个配置没有生效 目前还没有搞清楚为啥不生效 欢迎各位老大指导
spring.cloud.sentinel.datasource.flow.nacos.namespacesentinel
#生产的规则文件名称
spring.cloud.sentinel.datasource.flow.nacos.data-id${spring.application.name}-flow-rules
#group id 默认 SENTINEL_GROUP
spring.cloud.sentinel.datasource.flow.nacos.group-idSENTINEL_GROUP
#配置数据格式 使用 json
spring.cloud.sentinel.datasource.flow.nacos.data-typejson
# 规则类型 degrade、authority、param-flow
spring.cloud.sentinel.datasource.flow.nacos.rule-typeflow
Sentinel 流控规则验证
启动 Nocos 服务。启动 Sentinel Dashboard。启动 order-service 服务。创建流控规则如下 Nacos 配置中心规则如下 至此Sentinel 规则持久化到 Nacos 已经完成我们从 Sentinel 控制台创建的规则会持久化到 Nacos 中我们在 Nacos 中修改规则也可以直接在 Sentinel 上看到。
未解之谜
不同应用服务的流控规则想持久化到 Nacos 的不同 Namespace 下如何实现?
多次尝试发现如下配置不生效。
#这里的这个配置没有生效 目前还没有搞清楚为啥不生效 欢迎各位老大指导
spring.cloud.sentinel.datasource.flow.nacos.namespacesentinel
如果不在 Sentinel Dashboard 中指定 Nacos NamespaceSentinel 的流控规则将会默认在 public 名称空间下在 Sentinel Dashboard 中指定 Nacos Namespace 配置则所有应用的流控规则都会到同一个 Namespace 下了。
所以如何实现不同应用服务的流控规则持久化到 Nacos 的不同 Namespace
最后附上 order-service 的完整 pom.xml 文件如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.4.5/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.order.service/groupIdartifactIdorder-service/artifactIdversion0.0.1-SNAPSHOT/versionnameorder-service/namedescriptionorder-service/descriptionurl/dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency!--引入 Nacos 支持--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactIdversion2021.1/version/dependencydependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactIdversion2021.1/version/dependency!--引入 sentinel 支持--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactIdversion2021.1/version/dependencydependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-datasource-nacos/artifactIdversion1.8.0/version/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-bootstrap/artifactIdversion3.0.0/version/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-commons/artifactIdversion2.2.2.RELEASE/versionscopecompile/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project总结本篇分享了如何使用 Nacos 持久化 Sentinel 规则Nacos 提供了注册中心和配置中心的能力是微服务技术栈中不可或缺的组件因此建议使用 Nacos 来持久化 Sentinel 规则。
如有不正确的地方请各位指出纠正。