文档

自定义 Registry

更新时间:

说明

自定义注册中心的实现类可参考 com.alipay.sofa.rpc.registry.zk.ZookeeperRegistry,实现的具体信息请参见 Registry-ZK

自定义注册中心的流程如下:

实现自定义注册中心类

  1. 自定义注册中心类。

    自定义注册中心类时,需继承如下虚拟类:

    package com.alipay.sofa.rpc.registry;
    
    @Extensible(singleton =false)
    public abstract class Registry implements Initializable,Destroyable{
        public abstract boolean start();  //启动客户端 
        public abstract void register(ProviderConfig config);  //注册服务
        public abstract void unRegister(ProviderConfig config);  //取消注册,优雅关闭使用
        public abstract void batchUnRegister(List<ProviderConfig> configs); //批量取消注册
        public abstract List<ProviderGroup> subscribe(ConsumerConfig config); //订阅服务
        public abstract void unSubscribe(ConsumerConfig config); //取消订阅,优雅关闭使用
        public abstract void batchUnSubscribe(List<ConsumerConfig> configs); //批量取消订阅
    }
  2. 添加注解。

    示例如下:

    @Extension("zookeeper")  
    public class ZookeeperRegistry extends Registry {

    @Extension("") 用于指定注册中心别名,可自定义。您可以在业务指定注册中心时使用该别名。

将自定义注册中心配置到 RPC 架构

基于 SOFARPC 的 SPI 机制,您需要新建扩展文件 META-INF/services/sofa-rpc/com.alipay.sofa.rpc.registry.Registry,将自定义的注册中心配置到 RPC 架构。内容示例如下:

zookeeper=com.alipay.sofa.rpc.registry.zk.ZookeeperRegistry
Image 4

在业务中使用自定义注册中心

  1. application.properties 中配置注册中心源信息。

    配置格式为:com.alipay.sofa.rpc.registries.<注册中心用例名称>=协议://地址,协议名称为上面自定义的注册中心名称,例如 zookeeper。示例如下:

    com.alipay.sofa.rpc.registries.myRegistry=zookeeper://10.0.0.1:9600  //配置注册中心地址。
  2. 配置需要注册中心的服务。

    注册中心名称为上面自定义的注册中心用例名。示例如下:

    <sofa:service interface="com.alipay.sofa.facade.SampleService" ref="sampleService">
            <sofa:binding.bolt>
                <sofa:global-attrs registry="myRegistry"/>
            </sofa:binding.bolt>
    
        </sofa:service>

  • 本页导读 (0)
文档反馈