• 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下载地址: 

  • 相关阅读:
    红黑树(RBTree)
    js闭包简要分析
    html 5的localstorag
    浅析CSS中的haslayout
    初识sass框架
    BFC块级格式化上下文简述
    RESTful互联网框架
    javascript的框架演化
    浅析angular框架的cookie
    angular template浅析
  • 原文地址:https://www.cnblogs.com/moris5013/p/11239263.html
Copyright © 2020-2023  润新知