从Zookeeper获取dubbo提供者ip

创建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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值