Sentinel dashboard持久化,持久化到nacos和数据库中(基于sentinel 1.8.2)

2023-11-21 08:41

本文主要是介绍Sentinel dashboard持久化,持久化到nacos和数据库中(基于sentinel 1.8.2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我们知道,Sentinel dashboard默认是是没有持久化功能的,都是保存在内存中的,对于sentinel客户端同样如此,当在sentinel dashboard配置规则的时候,dashboard会获取对应应用配置的dashboard给应用传递消息的http,将规则通过HTTP请求发送给sentinel客户端,同样,sentinel客户端也是没有持久化的都是放在内存中的。
sentinel dashbord通过HTTP向sentinel客户端获取客户端的限流配置,而在dashboard更新配置之后则通过HTTP向sentinel客户端推送更新的限流配置。

sentinel dashboard和sentinel客户端两个都各自开启了HTTP服务用来通信,sentinel dashboard开启的是一个正常的web应用,比如默认用spingboot就是基于tomcat,而sentinel客户端同样也会开启端口,让dashbaoard请求发送消息给sentinel客户端,值只不过这个客户端是一个简易的HTTP实现,实现就是基于常规的ServerSocket去实现的。而不管是sentinel dashboard将规则推送给sentinel客户端,还是sentinel客户端将相关信息推送给sentinel dashboard,sentinel dashboatd和sentinel客户端都是保存在内存中的
如果sentinel-dashboard重启,或者sentinel客户端重启,两者数据都会丢失,这对于生产肯定是不行的,因此需要进行持久化。

查看相关源码之后,我觉得可以通过两种方式

  • sentinel提供了DataSource方式,将需要保存的数据放在datasouce中,dashboard操作datasouce中数据,而sentinel客户端则从datasouce中拉取数据

这里我们以网上常说的基于Nacos为例来说明。

Sentinel Datasource

在说这个之前,需要说下sentinel中的Datasource,可以理解为这是对sentinel配置存储的一个抽象,
我们以大家常见的spring cloud位列,说下这个是怎么生效的。
在sentinel spring cloud中,提供了SentinelProperties的配置类,其中有一个:
private Map<String, DataSourcePropertiesConfiguration> datasource = new TreeMap<>( String.CASE_INSENSITIVE_ORDER);则是配置Datasource的入口,
DataSourcePropertiesConfiguration中有如下几种配置:

public class DataSourcePropertiesConfiguration {private FileDataSourceProperties file;private NacosDataSourceProperties nacos;private ZookeeperDataSourceProperties zk;private ApolloDataSourceProperties apollo;private RedisDataSourceProperties redis;private ConsulDataSourceProperties consul;
}

如果我们配置了nacos,那么这里就会对NacosDataSourceProperties进行实例化,
而所有的DataSourcePropertiesConfiguration会在 SentinelDataSourceHandler中进行处理,其实现了SmartInitializingSingleton,当spring容器就绪后,会调用其afterSingletonsInstantiated,这里就会向spring容器注入Datasocue对应FactoryBean,

public NacosDataSourceProperties() {super(NacosDataSourceFactoryBean.class.getName());}

NacosDataSourceFactoryBeangetObject工厂方法实现如下:

public NacosDataSource getObject() throws Exception {Properties properties = new Properties();if (!StringUtils.isEmpty(this.serverAddr)) {properties.setProperty(PropertyKeyConst.SERVER_ADDR, this.serverAddr);}else {properties.setProperty(PropertyKeyConst.ENDPOINT, this.endpoint);}if (!StringUtils.isEmpty(this.accessKey)) {properties.setProperty(PropertyKeyConst.ACCESS_KEY, this.accessKey);}if (!StringUtils.isEmpty(this.secretKey)) {properties.setProperty(PropertyKeyConst.SECRET_KEY, this.secretKey);}if (!StringUtils.isEmpty(this.namespace)) {properties.setProperty(PropertyKeyConst.NAMESPACE, this.namespace);}if (!StringUtils.isEmpty(this.username)) {properties.setProperty(PropertyKeyConst.USERNAME, this.username);}if (!StringUtils.isEmpty(this.password)) {properties.setProperty(PropertyKeyConst.PASSWORD, this.password);}return new NacosDataSource(properties, groupId, dataId, converter);}

可以看到,这里会向spring容器注入一个NacosDataSource.
而在SentinelDataSourceHandler中,当NacosDataSource实例化之后,会调用dataSourceProperties.postRegister(newDataSource);:

public void postRegister(AbstractDataSource dataSource) {switch (this.getRuleType()) {case FLOW:FlowRuleManager.register2Property(dataSource.getProperty());break;case DEGRADE:DegradeRuleManager.register2Property(dataSource.getProperty());break;case PARAM_FLOW:ParamFlowRuleManager.register2Property(dataSource.getProperty());break;case SYSTEM:SystemRuleManager.register2Property(dataSource.getProperty());break;case AUTHORITY:AuthorityRuleManager.register2Property(dataSource.getProperty());break;case GW_FLOW:GatewayRuleManager.register2Property(dataSource.getProperty());break;case GW_API_GROUP:GatewayApiDefinitionManager.register2Property(dataSource.getProperty());break;default:break;}}

而这个方法的作用,是每隔规则的配置都会向NacosDataSourceProperties注册一个监听器,NacosDataSource中会通过Nacos客户端注册configListener当有配置发生更新的时候,会通过configListener调用NacosDataSourceProperties.updateValue而在updateValue则通知上面注册的监听器,每隔规则管理角色就会更新对应的规则信息。

public NacosDataSource(final Properties properties, final String groupId, final String dataId,Converter<String, T> parser) {super(parser);if (StringUtil.isBlank(groupId) || StringUtil.isBlank(dataId)) {throw new IllegalArgumentException(String.format("Bad argument: groupId=[%s], dataId=[%s]",groupId, dataId));}this.groupId = groupId;this.dataId = dataId;this.properties = properties;this.configListener = new Listener() {@Overridepublic Executor getExecutor() {return pool;}public void receiveConfigInfo(final String configInfo) {T newValue = NacosDataSource.this.parser.convert(configInfo);getProperty().updateValue(newValue);}};initNacosListener();loadInitialConfig();}
private void initNacosListener() {try {this.configService = NacosFactory.createConfigService(this.properties);configService.addListener(dataId, groupId, configListener);} catch (Exception e) {}}

所以如果我们在sentinel的客户端配置了nacos datasource,那么理论上sentinel能够获取和动态感知配置的变化做出响应

那么sentinel dashboard中是否能够将配置写入到sentinel中呢 ?

Sentinel dashboard写入到Nacos中

在Sentinel1.81中对于Dashbord提供了V2处理方式,新增了DynamicRuleProviderDynamicRulePublisher,而在V1中都是通过SentinelApiClient方式,即简单通过HTTP向Sentinel客户端发送请求更新数据。DynamicRuleProvider主要是dashboard来获取已经配置的规则,而DynamicRulePublisher则是更新规则后发布。说白了就是,通过DynamicRuleProvider获取配置的规则,通过DynamicRulePublisher写入修改的配置。
在V1中,获取配置的规则则是通过HTTP请求向sentinel客户端获取,更新配置则是通过HTTP请求向sentinel客户端推送配置。

在V2中虽然在正式版本里面没有提供相关的实现(V2默认依然是通过HTTP请求获取),但是在项目的test中则提供了Nacos的相关实现:
在这里插入图片描述
主要有如下四个类:

  • FlowRuleNacosProvider 从nacos中获取配置
  • FlowRuleNacosPublisher 将配置写入到nacos中
  • NacosConfig nacos的相关配置类
  • NacosConfigUtil nacos默认group dataid配置。

下面开始讲解怎么调整:

  1. 首选在sentinel dashboard中调整页面流控访问接口,在这里插入图片描述
    调整sidebar.html中如下代码:
    <!----><li ui-sref-active="active" ng-if="entry.appType==0"><!----><a ui-sref="dashboard.flow({app: entry.app})"><!----><i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则 V2</a><!----></li>

去掉上述代码的注释,注释或者去掉如下代码:

     <li ui-sref-active="active" ng-if="!entry.isGateway"><a ui-sref="dashboard.flowV1({app: entry.app})"><i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则</a></li>

然后在代码目录下新建com.alibaba.csp.sentinel.dashboard.rule.nacos包,将刚才提到的四个类放到该包下:
在这里插入图片描述
其中需要对NacosConfig类进行调整:

public class NacosConfig {
@Value("${sentinel.nacos.addr}")private String addr;@Beanpublic Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {return JSON::toJSONString;}@Beanpublic Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {return s -> JSON.parseArray(s, FlowRuleEntity.class);}@Beanpublic ConfigService nacosConfigService() throws Exception {System.out.println("nacosConfigService with "+addr);return ConfigFactory.createConfigService(addr);}}

主要就是调整了读取nacos的配置,默认是直接localhost,然后FlowControllerV2中调整如下:

 @Autowired//@Qualifier("flowRuleDefaultProvider")@Qualifier("flowRuleNacosProvider")private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;@Autowired//@Qualifier("flowRuleDefaultPublisher")@Qualifier("flowRuleNacosPublisher")private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

然后在application.properties中增加nacos配置如下

sentinel.nacos.addr=xxx

调整完之后,需要在Nacos增加对应项目的流控配置:
在这里插入图片描述

如果不想在nacos中配置规则,在sentinel dashbaord中配置,这里配置一个[]即可
这样sentinel dashboard端就调整完了

接下来调整客户端
客户端pom依赖如下:

<dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId><version>2.2.7.RELEASE</version></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId><version>1.8.2</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2.2.7.RELEASE</version><!----><exclusions><exclusion><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId></exclusion></exclusions></dependency><!----><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId><version>1.4.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.3.2.RELEASE</version></dependency></dependencies>

application.properties配置如下:

spring.application.name=sentinel-test-001
server.port=8080
spring.cloud.sentinel.transport.dashboard=localhost:8081
spring.cloud.sentinel.datasource.ds1.nacos.server-addr=xxxx
spring.cloud.sentinel.datasource.ds1.nacos.data-id=${spring.application.name}-flow-rules
spring.cloud.sentinel.datasource.ds1.nacos.group-id=SENTINEL_GROUP
spring.cloud.sentinel.datasource.ds1.nacos.data-type=json
spring.cloud.sentinel.datasource.ds1.nacos.rule-type=flow

然后我们启动sentinel dashboard和sentinel客户端,然后在dashboard中配置:
在这里插入图片描述

在这里插入图片描述

关闭客户端重启,发现规则依然生效。
这里需要注意的是,这个地方只是实现了流控这一种规则,其他规则需要自己在实现。
其实不知道大家有没有发现,这里如果不对sentinel-dashboard调整,而是直接调整客户端基于datasource方式来获取配置,也是可以的,但是这时候就不能在sentinel dashboard里面配置规则了,而需要在nacos中配置

这应该是目前网上比较流行的一种实例化的方法,这里我在另外提出一种方法,大家可以试验下:
sentinel dashboard保存的时候,我们将其保存在数据库中,并同时推送给客户端,在获取配置的时候,不采用默认从客户端获取的方式,而是从数据库中读取。sentinel 中默认数据都是放在内存中的都是基于InMemoryRuleRepositoryAdapter这个抽象类,每个规则有自己不同实现,我们自己实现这个类,对每个规则都保存到数据库中。

这篇关于Sentinel dashboard持久化,持久化到nacos和数据库中(基于sentinel 1.8.2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/401185

相关文章

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

MySQL数据库宕机,启动不起来,教你一招搞定!

作者介绍:老苏,10余年DBA工作运维经验,擅长Oracle、MySQL、PG、Mongodb数据库运维(如安装迁移,性能优化、故障应急处理等)公众号:老苏畅谈运维欢迎关注本人公众号,更多精彩与您分享。 MySQL数据库宕机,数据页损坏问题,启动不起来,该如何排查和解决,本文将为你说明具体的排查过程。 查看MySQL error日志 查看 MySQL error日志,排查哪个表(表空间

深入理解数据库的 4NF:多值依赖与消除数据异常

在数据库设计中, "范式" 是一个常常被提到的重要概念。许多初学者在学习数据库设计时,经常听到第一范式(1NF)、第二范式(2NF)、第三范式(3NF)以及 BCNF(Boyce-Codd范式)。这些范式都旨在通过消除数据冗余和异常来优化数据库结构。然而,当我们谈到 4NF(第四范式)时,事情变得更加复杂。本文将带你深入了解 多值依赖 和 4NF,帮助你在数据库设计中消除更高级别的异常。 什么是

DM8数据库安装后配置

1 前言 在上篇文章中,我们已经成功将库装好。在安装完成后,为了能够更好地满足应用需求和保障系统的安全稳定运行,通常需要进行一些基本的配置。下面是一些常见的配置项: 数据库服务注册:默认包含14个功能模块,将这些模块注册成服务后,可以更好的启动和管理这些功能;基本的实例参数配置:契合应用场景和发挥系统的最大性能;备份:有备无患;… 2 注册实例服务 注册了实例服务后,可以使用系统服务管理,

速了解MySQL 数据库不同存储引擎

快速了解MySQL 数据库不同存储引擎 MySQL 提供了多种存储引擎,每种存储引擎都有其特定的特性和适用场景。了解这些存储引擎的特性,有助于在设计数据库时做出合理的选择。以下是 MySQL 中几种常用存储引擎的详细介绍。 1. InnoDB 特点: 事务支持:InnoDB 是一个支持 ACID(原子性、一致性、隔离性、持久性)事务的存储引擎。行级锁:使用行级锁来提高并发性,减少锁竞争

开源分布式数据库中间件

转自:https://www.csdn.net/article/2015-07-16/2825228 MyCat:开源分布式数据库中间件 为什么需要MyCat? 虽然云计算时代,传统数据库存在着先天性的弊端,但是NoSQL数据库又无法将其替代。如果传统数据易于扩展,可切分,就可以避免单机(单库)的性能缺陷。 MyCat的目标就是:低成本地将现有的单机数据库和应用平滑迁移到“云”端

ORACLE 11g 创建数据库时 Enterprise Manager配置失败的解决办法 无法打开OEM的解决办法

在win7 64位系统下安装oracle11g,在使用Database configuration Assistant创建数据库时,在创建到85%的时候报错,错误如下: 解决办法: 在listener.ora中增加对BlueAeri-PC或ip地址的侦听,具体步骤如下: 1.启动Net Manager,在“监听程序”--Listener下添加一个地址,主机名写计

MyBatis 切换不同的类型数据库方案

下属案例例当前结合SpringBoot 配置进行讲解。 背景: 实现一个工程里面在部署阶段支持切换不同类型数据库支持。 方案一 数据源配置 关键代码(是什么数据库,该怎么配就怎么配) spring:datasource:name: test# 使用druid数据源type: com.alibaba.druid.pool.DruidDataSource# @需要修改 数据库连接及驱动u

Sentinel 高可用流量管理框架

Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。 Sentinel 具有以下特性: 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应

CentOS下mysql数据库data目录迁移

https://my.oschina.net/u/873762/blog/180388        公司新上线一个资讯网站,独立主机,raid5,lamp架构。由于资讯网是面向小行业,初步估计一两年内访问量压力不大,故,在做服务器系统搭建的时候,只是简单分出一个独立的data区作为数据库和网站程序的专区,其他按照linux的默认分区。apache,mysql,php均使用yum安装(也尝试