Registering with multiple registries

更新时间:
复制 MD 格式

A service can be registered with multiple registries. Use one of the following methods:

Using XML

  1. Configure the registry source information in the application.properties file.

    The configuration format is com.alipay.sofa.rpc.registries.<custom_alias>=protocol://address. Supported protocols include dsr, sofa, zookeeper, consul, gateway, mesh, multicast, local, and nacos.

    The following code shows an example:

    com.alipay.sofa.rpc.registries.registryName1=dsr://10.0.0.1:9600
    com.alipay.sofa.rpc.registries.registryName2=dsr://10.0.0.2:9600

  2. Configure the service that requires multiple registries.

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

Using the API

  1. Create a registry.

    The following code shows an example of how to create a registry named consul:

    RegistryConfig registryA = new RegistryConfig().setProtocol("consul").setAddress("localhost:8500");
  2. Build multiple RegistryConfig objects and add them to the ProviderConfig object.

    Example:

    List<RegistryConfig> registryConfigs =new ArrayList<RegistryConfig>();
    registryConfigs.add(registryA);
    registryConfigs.add(registryB);
    providerConfig.setRegistry(registryConfigs);
  3. Call the set methods on a MethodConfig object to configure method-specific parameters.

    The following is an example:

    MethodConfig methodConfigA =new MethodConfig();
    MethodConfig methodConfigB =new MethodConfig();
    List<MethodConfig> methodConfigs =new ArrayList<MethodConfig>();
    methodConfigs.add(methodConfigA);
    methodConfigs.add(methodConfigB);
    providerConfig.setMethods(methodConfigs);    // Server-side setting
    consumerConfig.setMethods(methodConfigs);  // Client-side setting