向zookeeper 注册服务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32423845/article/details/84784491
/**
 * 向zookeeper 注册服务
 * @param serviceName  服务名称
 * @param zkServiceIp  zookeeper服务地址
 * @param rootPath  根节点路径
 * @throws UnknownHostException
 */
public void init(String serviceName,String zkServiceIp,String rootPath) throws UnknownHostException {
    ZkClient zkClient=new ZkClient(zkServiceIp);
    boolean rootExists=zkClient.exists(rootPath);
    if(!rootExists){
        zkClient.createPersistent(rootPath);
    }
    boolean serviceExist=zkClient.exists(rootPath+"/"+serviceName);
    if(!serviceExist){
        //创建服务节点
        zkClient.createPersistent(rootPath+"/"+serviceName);
    }
    InetAddress inetAddress=InetAddress.getLocalHost();
    String ip=inetAddress.getHostAddress().toString();//获得本机Ip
    zkClient.createEphemeral(rootPath+"/"+serviceName+"/"+ip);
}

猜你喜欢

转载自blog.csdn.net/qq_32423845/article/details/84784491