IDEA模拟客户端及服务器连接ZooKeeper集群

该博客介绍了如何通过Java实现ZooKeeper的分布式服务注册与发现。DistributeServer类创建并连接到ZooKeeper集群,将服务器信息注册为临时顺序节点。DistributeClient则获取服务器列表并进行业务操作。通过ZooKeeper的监听机制,实现了动态服务列表更新。
摘要由CSDN通过智能技术生成

前提:具体操作见https://blog.csdn.net/weixin_41265887/article/details/113974315

1.创建DistributeServer.java类

package cn.tod;

import org.apache.zookeeper.*;

import java.io.IOException;

public class DistributeServer {
    private static String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
    private static int sessionTimeout = 4000;
    private ZooKeeper zk = null;

    private String parentNode = "/servers";

    public void getConnect() throws IOException {
        zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }

    public void registServer(String hostname) throws KeeperException, InterruptedException {
        String node = zk.create(parentNode + "/server", hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(hostname + " is online " + node);
    }

    public void business(String hostname) throws InterruptedException {
        System.out.println(hostname + " is working ...");
        Thread.sleep(Long.MAX_VALUE);
    }

    public void close() throws InterruptedException {
        zk.close();
    }

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        DistributeServer server = new DistributeServer();
        server.getConnect();
        server.registServer(args[0]);
        server.business(args[0]);
        server.close();
    }
}

2.创建DistributeClient.java类

package cn.tod;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DistributeClient {
    private static String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
    private static int sessionTimeout = 4000;
    private ZooKeeper zk = null;

    private String parentNode = "/servers";

    public void getConnect() throws IOException {
        zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                try {
                    getServerList();
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getServerList() throws KeeperException, InterruptedException {
        List<String> children = zk.getChildren(parentNode, true);
        ArrayList<String> servers = new ArrayList<String>();
        for (String child : children) {
            byte[] data = zk.getData(parentNode + "/" + child, false, null);
            servers.add(new String(data));
        }

        System.out.println(servers);
    }

    public void business() throws InterruptedException {
        System.out.println("Client is working ...");
        Thread.sleep(Long.MAX_VALUE);
    }

    public void close() throws InterruptedException {
        zk.close();
    }

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        DistributeClient client = new DistributeClient();
        client.getConnect();
        client.getServerList();
        client.business();
        client.close();
    }
}

3.在ZooKeeper集群中创建节点servers

[root@hadoop102 bin]# pwd
/opt/module/zookeeper-3.4.10/bin
[root@hadoop102 bin]# zkCli.sh 
[zk: localhost:2181(CONNECTED) 0] create /servers "servers"

4.在IDEA中配置多个程序运行,在DistributeServer程序的Program arguments中添加参数,运行即可模拟。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值