码迷,mamicode.com
首页 > 其他好文 > 详细

zookeeper实现HA入门案例

时间:2020-02-13 19:06:02      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:connect   max   void   nsa   delete   nec   str   main   ssl   

package com.zhileiedu.zk;

import java.util.Random;

import javax.net.ssl.SSLEngineResult.Status;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;

public class T {

static String status = "ACTIVE";

public static void main(String[] args) throws Exception {
    String connectString = "192.168.23.157:2181,192.168.23.157:2182,192.168.23.157:2183";
    ZooKeeper zk = new ZooKeeper(connectString, 5000, null);
    String IP = "202.106.29.138";
    String port = new Random().nextInt(500)+"";
    if(zk.exists("/zk01", new MasterWatcher()) != null) { // 节点已经创建
        status = "STANDBY";
    } else {
        zk.create("/zk01", (IP + ":"+port).getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        status = "ACTIVE";
        System.out.println("启动active");
    }
    
    Thread.sleep(Integer.MAX_VALUE);
    
}


static class MasterWatcher implements Watcher {

    public void process(WatchedEvent e ) {
        // TODO Auto-generated method stub
        if (e.getType() == EventType.NodeDeleted) {
            status = "ACTIVE";
            System.out.println("切换为active");
        }
    }
    
}

}

zookeeper实现HA入门案例

标签:connect   max   void   nsa   delete   nec   str   main   ssl   

原文地址:https://www.cnblogs.com/leigepython/p/12304471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!