*maven
非必须的*
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>{Dubbo版本号}</version>
</dependency>
在服务消费者端的代码中获取Zookeeper注册中心上的全部可用服务
// 创建Zookeeper注册中心实例
RegistryService registryService = ExtensionLoader.getExtensionLoader(RegistryFactory.class)
.getExtension("zookeeper")
.getRegistry(new URL("zookeeper", "localhost", 2181));
// 获取可用的服务列表
List<URL> urls = registryService.lookup(new URL("dubbo", "localhost", 0, "org.example.UserService"));
for (URL url : urls) {
System.out.println(url.getHost() + ":" + url.getPort());
}
以上示例代码中,通过RegistryFactory的getExtension方法获取到ZookeeperRegistryFactory实例,然后通过其getRegistry方法构造一个Zookeeper注册中心实例。然后,通过调用RegistryService的lookup方法获取到全部可用服务的URL列表。