从zookeeper中获取对应dubbo服务的ip和port

1、用zkclint,获取服务"xxx"下面的所有provider信息

2、正则表达式获取ip和port

3、ip:port为test3环境zookeeper链接信息

ZkClient zkClient = new ZkClient("ip:port", 5000);
list = zkClient.getChildren("/dubbo/xxx/providers");
Iterator<String> it = list.iterator();
while (it.hasNext()) {

    str = (String)it.next();
    System.out.println("返回provider信息:"+str);
    break;
    }
ip = getip(str);
port =getport(str);

Dubbo中,如果你想要从Zookeeper注册中心获取所有已注册的Dubbo服务,你需要通过Zookeeper客户端进行操作。以下是一个基本示例: ```java import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.ReferenceConfig; import com.alibaba.dubbo.config.RegistryConfig; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; public class ZookeeperServiceDiscovery { private static final String ZOOKEEPER_ADDRESS = "zk://your_zookeeper_address:port/path/to/dubbo"; public static void main(String[] args) throws Exception { ApplicationConfig application = new ApplicationConfig("my-dubbo-app"); RegistryConfig registry = new RegistryConfig(); registry.setAddress(ZOOKEEPER_ADDRESS); try (ZooKeeper zookeeper = new ZooKeeper(registry.getAddress(), 5000, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getType() == Event.EventType.NodeChildrenChanged) { // 更新服务列表 List<String> serviceNames = getServices(zookeeper); System.out.println("Services updated: " + serviceNames); } } })) { // Start watching for changes in the registered services zookeeper.exists("/dubbo", true); // Main loop or other logic here, as you need to keep listening for updates } } private static List<String> getServices(ZooKeeper zookeeper) throws InterruptedException, KeeperException { return zookeeper.getChildren("/dubbo", false); // 获取/services节点下的所有子节点,即服务名列表 } } ``` 在这个例子中,我们首先设置了一个ZooKeeper地址,然后创建一个ZooKeeper客户端并开启一个Watcher监听节点变化。当`/dubbo`路径下的子节点改变时(新服务注册或删除),`getServices`方法会被调用以获取最新的服务列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值