A service can be registered with multiple registries. Use one of the following methods:
Using XML
Configure the registry source information in the
application.propertiesfile.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:9600Configure 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
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");Build multiple
RegistryConfigobjects and add them to theProviderConfigobject.Example:
List<RegistryConfig> registryConfigs =new ArrayList<RegistryConfig>(); registryConfigs.add(registryA); registryConfigs.add(registryB); providerConfig.setRegistry(registryConfigs);Call the
setmethods on aMethodConfigobject 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
该文章对您有帮助吗?