需求描述,某分布式系统中,有多台主节点服务器,可以动态的上下线,要求服务器可以实时的监听到主节点服务器的上下线。
其细节可以参考下图,值得注意的时,无论是服务器还是客户端,相对于zookeeper集群都是客户端,只不过服务器是向zookeeper集群写数据,而客户端是向zookeeper集群读数据。
那么现在分析下我们要做哪些事呢?
(1)启动zookeeper服务集群。
(2)实现服务器代码。
服务器主要要实现获取连接、注册节点(创建节点将服务器的信息写到zookeeper集群的节点中),服务器的业务逻辑。
- public class DistributeServer {
-
- private static String connectString="192.168.106.43:2182,192.168.106.44:2182,192.168.106.45:2182";
- private static int sessionTimeout = 2000;
- private ZooKeeper zk = null;
- private String parentNode = "/servers";
-
- // 创建到 zk 的客户端连接
- public void getConnect() throws IOException {
- zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
- @Override
- public void process(WatchedEvent event) {
- }
- });
- }
-
- // 注册服务器
- public void registServer(String hostname) throws Exception {
- String create = zk.create("/servers/" + hostname, hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
- System.out.println(hostname + " is online " + create);
- }
-
- // 业务功能
- public void business(String hostname) throws Exception {
- System.out.println(hostname + " is working ...");
- Thread.sleep(Long.MAX_VALUE);
- }
-
- public static void main(String[] args) throws Exception {
- // 1 获取 zk 连接
- DistributeServer server = new DistributeServer();
- server.getConnect();
- // 2 利用 zk 连接注册服务器信息
- server.registServer(args[0]);
- // 3 启动业务功能
- server.business(args[0]);
- }
-
-
- }
(3)实现客户端代码
客户端主要需要做的事情是,连接zookeeper服务集群,获取服务器列表并进行节点监听,客户端的业务逻辑。
- public class DistributeClient {
- private static String connectString="192.168.106.43:2182,192.168.106.44:2182,192.168.106.45:2182";
- private static int sessionTimeout = 2000;
- private ZooKeeper zk = null;
- private String parentNode = "/servers";
- // 创建到 zk 的客户端连接
- public void getConnect() throws IOException {
- zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
- @Override
- public void process(WatchedEvent event) {
- // 再次启动监听
- try {
- getServerList();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- // 获取服务器列表信息
- public void getServerList() throws Exception {
- // 1 获取服务器 子节点信息,并且对父节点进行监听
- List<String> children = zk.getChildren(parentNode, true);
- // 2 存储服务器信息列表
- ArrayList<String> servers = new ArrayList<>();
- // 3 遍历所有节点,获取节点中的主机名称信息
- for (String child : children) {
- byte[] data = zk.getData(parentNode + "/" + child,false, null);
- servers.add(new String(data));
- }
- // 4 打印服务器列表信息
- System.out.println(servers);
- }
- // 业务功能
- public void business() throws Exception{
- System.out.println("client is working ...");
- Thread.sleep(Long.MAX_VALUE);
- }
- public static void main(String[] args) throws Exception {
- // 1 获取 zk 连接
- DistributeClient client = new DistributeClient();
- client.getConnect();
- // 2 获取 servers 的子节点信息,从中获取服务器信息列表
- client.getServerList();
- // 3 业务进程启动
- client.business();
- }
-
- }
发现没有,原来zookeeper翻来翻去就是这么一点东西,简直太简单了。
最后做下测试吧。
先启动客户端,在zookeeper集群上手工用命令手工创建节点,看看客户端能否监听到呢。
- create -e -s /servers/hadoop102 "hadoop102"
- create -e -s /servers/hadoop103 "hadoop103"
客户端的输出如下。
删除节点。
- delete /servers/hadoop102
就不demo了,上面测试说明客户端没有问题。接下来测试服务端代码。
按照如下图所示方式,指定服务端启动时传入的参数。启动服务器。
测试结果如下。