利用zk模拟dubbo等分布式调用系统动态感知服务上下线功能

  1. 模拟上游服务上下线操作
package com.ruiclear.test.zookeeper;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;

import java.util.List;
import java.util.Random;

/**
 * @author RuiClear
 * @date 2020-05-21 15:57
 */
public class Server {


    public static void main(String[] args) throws Exception{
        ZooKeeper zk = new ZooKeeper("localhost:2181",5000, (e) ->{
            System.out.println(e.getPath()+":"+e.getType());
        });
        new Thread(() ->{
            for(int i=0;i<40;i++) {
                try {
                    String s = zk.create("/rui/life", ("10.2.11."+i).getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);

                    System.out.println(s+" is online!");

                    Thread.sleep(2000);
                }catch (Exception e){
                    e.printStackTrace();
                }


            }
        }).start();
        Random random = new Random();

        new Thread(() ->{

            try {
                for(int i=0;i<100;i++) {
                    List<String> list  = zk.getEphemerals("/");
                    if(list.size() > 0){
                        String s = list.get(random.nextInt(list.size()));
                        zk.delete(s,-1);

                        System.out.println(s+" is offline!");
                    }
                    Thread.sleep(4000);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }
}

  1. 模拟下游感知上游服务上下线状态
package com.ruiclear.test.zookeeper;

        import org.apache.commons.lang.StringUtils;
        import org.apache.zookeeper.ZooKeeper;

        import java.util.ArrayList;
        import java.util.List;

/**
 * @author RuiClear
 * @date 2020-05-21 15:56
 */
public class Client {
    static ZooKeeper zk;
    public static void main(String[] args) throws Exception{
        zk = new ZooKeeper("localhost:2181",5000, (e) ->{
            try {
                getList();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        getList();

        Thread.sleep(Integer.MAX_VALUE);

    }


    public static void  getList() throws Exception{
        List<String> children = zk.getChildren("/rui", true);

        List<String> servers = new ArrayList<>();
        for (String s:children){
            byte[] data = zk.getData("/rui/"+s, false, null);
            servers.add(new String(data));
        }
        System.out.println("servers: "+StringUtils.join(servers,","));
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值