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 = ;
private static final CountDownLatch countDownLatch = new CountDownLatch(); 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( * );
zooKeeper.close();
} }

项目结构:

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

github下载地址:

上一篇:Laravel 返回数据库中的随机一行数据


下一篇:spring 装配集合