沫言|Sentinel实战:规则持久化的5种方式( 三 )

<>(config, ruleKey, channel, parser);FlowRuleManager.register2Property(redisDataSource.getProperty());}NacosNacos 数据源的实现类是 NacosDataSource 。
首先引入依赖:
com.alibaba.cspsentinel-datasource-nacosx.y.z接入方法如下:
private void init() throws Exception { String remoteAddress = yourRemoteAddress(); String groupId = yourGroupId();String dataId = yourDataId(); Converter> parser = source -> JSON.parseObject(source,new TypeReference>() {});ReadableDataSource> nacosDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId, parser);FlowRuleManager.register2Property(nacosDataSource.getProperty());}ZkZk 数据源的实现类是 ZookeeperDataSource 。
首先引入依赖:
com.alibaba.cspsentinel-datasource-zookeeperx.y.z接入方法如下:
private void init() throws Exception { String remoteAddress = yourRemoteAddress(); String path = yourPath(); Converter> parser = source -> JSON.parseObject(source,new TypeReference>() {});ReadableDataSource> zookeeperDataSource = new ZookeeperDataSource<>(remoteAddress, path, parser);FlowRuleManager.register2Property(zookeeperDataSource.getProperty());}ApolloApollo 数据源的实现类是 ApolloDataSource 。
首先引入依赖:
com.alibaba.cspsentinel-datasource-apollox.y.z接入方法如下:
private void init() throws Exception { String namespaceName = yourNamespaceName(); String ruleKey = yourRuleKey();String defaultRules = yourDefaultRules(); Converter> parser = source -> JSON.parseObject(source,new TypeReference>() {});ReadableDataSource> apolloDataSource = new ApolloDataSource<>(namespaceName, ruleKey, path, defaultRules);FlowRuleManager.register2Property(apolloDataSource.getProperty());}可以看到5中持久化的方式基本上大同小异 , 主要还是对接每种配置中心 , 实现数据的转换 , 并且监听配置中心的数据变化 , 当接收到数据变化后能够及时的将最新的规则更新到 RuleManager 中去就可以了 。
【沫言|Sentinel实战:规则持久化的5种方式】公众号【码农架构】专注于系统架构、高可用、高性能、高并发类技术分享 。


推荐阅读