ShardingSphere实现本地注册中心,动态刷新actualDataNodes
看过ShardingSphere4.0.0-RC1实现动态刷新actualDataNodes,不使用zookeeper等第三方注册中心这篇文章,但是发现其可操作性还是写的太差,很多还需要自己琢磨,故此写下这篇文章记录自己的琢磨过程
坑1–RegistryCenter 这个类在哪儿
需要引入sharding的服务编排核心jar包,举例:gradle
compile group: 'org.apache.shardingsphere', name: 'sharding-orchestration-core', version: '4.0.1'
坑2–OrchestrationShardingDataSourceFactory怎么也没有
接着引包
compile group: 'org.apache.shardingsphere', name: 'sharding-jdbc-orchestration-spring-namespace', version: '4.0.1'
工程代码
关于sharding SPI方面的说明请自行查看官方文档
步骤1
- 在resources/META-INF/services下建立文件
org.apache.shardingsphere.orchestration.reg.api.RegistryCenter - 建立类
com.changan.carbond.shardingjdbc.LocalRegistryCenter - 文件中的内容
com.xstudio.shardingjdbc.LocalRegistryCenter和上述类保持一致【SPI注册】
步骤2–LocalRegistryCenter
public class LocalRegistryCenter implements RegistryCenter {
public static Map<String, DataChangedEventListener> listeners = new ConcurrentHashMap<>();
private RegistryCenterConfiguration config;
private Properties properties;
/**
* public 是为了在重置节点的时候减少去重新读配置
*/
public static Map<String, String> values = new ConcurrentHashMap<>();
@Override
public void init

最低0.47元/天 解锁文章
344





