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

Sentinel Dashboard与客户端持久化配置实践
本文详细介绍了Sentinel Dashboard和客户端如何通过Nacos进行规则持久化,避免数据丢失。Sentinel客户端和Dashboard默认规则存储在内存中,可通过DataSource(如Nacos)实现持久化。在客户端,SentinelDataSource监听Nacos配置变化并更新规则;在Dashboard,通过DynamicRuleProvider和DynamicRulePublisher实现规则的获取和更新,调整相关配置后,使得Dashboard能将规则写入Nacos。这种方式确保了Sentinel在重启后仍能保持规则配置。

我们知道,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.
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值