dubbo调用服务时,利用zookeeper实现本地动态负载均衡

利用了zookeeper的临时节点的特点,生产者将自己的服务信息注册到zookeeper当中,消费者去zookeeper当中取出服务信息的集合,本地实现负载均衡

public class TestCreateNode {

    // 连接地址
    private static final String ADDRES = "127.0.0.1:2181";
    // 连接地址
    private static final int SESSIN_TIME_OUT = 2000;
    private static final CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        ZooKeeper zooKeeper = new ZooKeeper(ADDRES, SESSIN_TIME_OUT, new Watcher() {

            public void process(WatchedEvent event) {
                // 获取事件状态
                KeeperState keeperState = event.getState();
                // 获取事件类型
                EventType eventType = event.getType();

                if (KeeperState.SyncConnected == keeperState) {
                    if (EventType.None == eventType) {
                        countDownLatch.countDown();
                        System.out.println("开启连接............");
                    }
                }
            }
        });
        countDownLatch.await();
        // 创建节点
        String result = zooKeeper.create("/test", "irish".getBytes(), Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
        System.out.println("result:" + result);
        Thread.sleep(1000 * 10);
        zooKeeper.close();
    }

}

项目结构:

 

github下载地址: 

 

转载于:https://www.cnblogs.com/moris5013/p/11239263.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值