【zookeeper】4、创建java客户端(服务监听demo)

本文介绍了一种基于ZooKeeper的服务器客户端实现方案,详细展示了如何通过Java代码创建ZooKeeper实例,注册服务器节点,并在客户端监听节点变化,获取在线服务器列表。此方案适用于构建分布式系统的服务发现与注册。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ZKServer.java

package com.univer.server;

import java.io.IOException;

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

public class ZKServer {
	private String connectString = "hadoop:201:2181,hadoop202:2181,hadoop203:2181";
	private int sessionTimeout = 2000;

	private ZooKeeper zooKeeper = null;

	private String parentNode = "/servers";

	public void getZKClient() throws IOException {
		zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
				System.out.println("get watcher,msg :");
				System.out.println("\t eventType : " + event.getType());
				System.out.println("\t eventPath : " + event.getPath());
			}
		});
	}

	public void regist(String hostname) throws KeeperException, InterruptedException {
		String create = zooKeeper.create(parentNode + "/" + hostname, ("this is " + hostname).getBytes(),
				Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
		System.out.println(hostname + " is online " + create);
	}

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

	public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
		// 获取连接
		ZKServer zkServer = new ZKServer();
		zkServer.getZKClient();
		// 注册
		zkServer.regist(args[0]);
		// server业务
		zkServer.business(args[0]);
	}
}

 

ZKClient

package com.univer.client;

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

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

public class ZKClient {
	private String connectString = "hadoop:201:2181,hadoop202:2181,hadoop203:2181";
	private int sessionTimeout = 2000;

	private ZooKeeper zooKeeper = null;

	private String parentNode = "/servers";

	public void getZKClient() throws IOException {
		zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
				System.out.println("get watcher,msg :");
				System.out.println("\t eventType : " + event.getType());
				System.out.println("\t eventPath : " + event.getPath());
				try {
					getServers();
				} catch (KeeperException | InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
	}

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

	}

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

	public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
		// 获取连接
		ZKClient zkClient = new ZKClient();
		zkClient.getZKClient();
		// 监听节点变化
		zkClient.getServers();
		// client业务
		zkClient.business();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值