zoukankan      html  css  js  c++  java
  • 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下载地址: 

  • 相关阅读:
    一LWIP学习笔记之数据包管理
    智能家居的发展趋势
    break和continue的区别
    TCP与UDP区别总结
    C语言变量和函数命名规范
    常用电子元件
    php 1018
    php 1016
    mysql 应用查询 三个表(学生表,课程表,学生课程分数表) student, course, score表
    mysql 聚合函数
  • 原文地址:https://www.cnblogs.com/moris5013/p/11239263.html
Copyright © 2011-2022 走看看