3

ShardingSphere的注册中心

 1 year ago
source link: https://blog.51cto.com/u_15460453/5780269
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ShardingSphere的注册中心

本篇文章源码基于4.0.1版本

注册中心在ShardingSphere的作用就是用来管理各种数据源,在使用的时候,所有数据源通过向注册中心的指定目录下创建节点,所有使用这些数据源的服务监听这个目录,当有新的数据源加入的时候会通知这些服务,当数据源宕机的时候,也会收到通知,ShardingSphere支持的注册中心有Zookeeper、Etcd等等

在进行加载数据源的时候构造OrchestrationShardingDataSource对象,它的构造方法会创建分片编排门面类ShardingOrchestrationFacade,这个类一看名字就知道了这里使用了门面模式。它的成员变量有注册中心、配置服务、状态服务和监听管理器。

状态服务保存的是数据实例和数据源节点信息。

监听管理器ShardingOrchestrationListenerManager又分为配置变更监听管理器和状态变更监听管理器,初始化的过程是通过注册中心的watch()方法来发布相应的事件,被@Subscribe注解修饰的方法来处理相应的事件。

注册中心的加载是通过RegistryCenterServiceLoader的load()方法来进行加载,这里就使用了JDK的SPI机制来进行加载响应的注册中心

sharding-orchestration-reg模块是ShardingSphere注册中心的源码内容,它 的子模块有sharding-orchestration-reg-api、sharding-orchestration-reg-zookeeper-curator、sharding-orchestration-reg-nacos,分别是注册api提供的一些接口,Zookeeper注册中心的集成和nacos注册中心的集成,下面就分别看一下这三个模块

注册中心API

这一块的代码内容和我们上篇文章说的配置中心API模块的内容差不多,同样有监听类DataChangedEventListener、监听事件类DataChangedEvent、异常RegistryCenterException,注册中心配置类RegistryCenterConfiguration和注册中心接口RegistryCenter

它的实现类同样有两个,Zookeeper注册中心实现类CuratorZookeeperRegistryCenter和nacos注册中心实现类NacosRegistryCenter

Zookeeper模块

当看完配置中心模块的代码,再看注册中心模块的代码就很好理解了

使用Zookeeper配置中心

在使用上同样是加载CuratorZookeeperRegistryCenter实例,然后启动Zookeeper服务器,创建注册中心配置类,设置服务列表,调用CuratorZookeeperRegistryCenter的init()方法进行初始化,整体逻辑和配置中心十分相似,这里就不详细说了。

Nacos模块

使用Nacos注册中心

先进行初始化操作:

    public static void init() {
        Properties properties = new Properties();
        properties.setProperty("group", "SHARDING_SPHERE_DEFAULT_GROUP");
        properties.setProperty("timeout", "3000");
        RegistryCenterConfiguration configuration = new RegistryCenterConfiguration(nacosRegistryCenter.getType(), properties);
        configuration.setServerLists("127.0.0.1:8848");
        nacosRegistryCenter.init(configuration);
    }

调用了NacosRegistryCenter的init()方法:

    public void init(final RegistryCenterConfiguration config) {
        try {
            Properties properties = new Properties();
            properties.put("serverAddr", config.getServerLists());
            properties.put("namespace", null == config.getNamespace() ? "" : config.getNamespace());
            configService = NacosFactory.createConfigService(properties);
        } catch (final NacosException ex) {
            log.debug("exception for: {}", ex.toString());
        }
    }

设置服务地址和命名空间,利用Nacos工厂类NacosFactory来创建服务

在获取数据的时候根据dataId、组名获取响应的数据:

configService.getConfig(dataId, group, timeoutMs);

在进行持久化操作的时候直接调用了更新方法,根据dataId,组名来设置相应的值更新配置:

configService.publishConfig(dataId, group, value);

这篇文章我们将了ShardingSphere的注册中心,注册中心支持Nacos和Zookeeper,整体逻辑和配置中心的逻辑差不多,在注册中心加载的时候使用了JDK的SPI机制灵活加载注册中心,变更事件是通过 EventBus 来实现的。

❤️ 感谢大家

如果你觉得这篇内容对你挺有有帮助的话:

  1. 欢迎关注我❤️,点赞👍🏻,评论🤤,转发🙏
  2. 有不当之处欢迎批评指正。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK