创建ZkClient对象,从Children获取即可
pkg参数是包名
实际情况中会获取提供者会有多个,可能配置了禁用的提供者,所以需要获取全列表。
有dubboadmin的还是优先建议看dubboadmin
代码如下
public RestResponse<List<String>> getProviderIPList(@RestParam(name = "pkg") String pkg) throws IOException {
RestResponse<List<String>> result = new RestResponse();
String propertiyPath = this.getClass().getResource("/").getPath() + File.separator+"application.properties";
Properties pro = new Properties();
FileInputStream in = new FileInputStream(propertiyPath);
pro.load(in);
in.close();
String zkAddress = pro.getProperty("dubbo.zookeeperAddress");
String[] split = zkAddress.split("//");
StringBuffer stringBuffer = new StringBuffer();
if(split!=null){
stringBuffer.append(split[split.length - 1]);
}else{
stringBuffer.append("zk01.local:2181");
}
ZkClient zkClient = new ZkClient(stringBuffer.toString(), 5000);
List<String> list = zkClient.getChildren("/dubbo/"+pkg+"/providers");
Iterator<String> it = list.iterator();
List<String> providers =new ArrayList<>();
while (it.hasNext()) {
String str = (String)it.next();
providers.add(str);
}
result.setResult(providers);
return result;
}
引入包为
import org.I0Itec.zkclient.ZkClient;
POM引入依赖
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>