这是使用原始的api操作的
可以使用封装的api curator操作zookeeper
0、依赖
<!-- zookeeper--><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.12</version></dependency>
1、客户端操作测试
ZooKeeperClientTest
package com.lms.zookeeper;import org.apache.zookeeper.*;import org.apache.zookeeper.data.Stat;import org.junit.Before;import org.junit.Test;import java.io.IOException;import java.util.List;/*** @Author: 李孟帅* @CreateTime: 2020-08-20 22:07* @Description:*/public class ZooKeeperClientTest {private static final String CONNECTION_STRING = "192.168.189.100:2181,192.168.189.101:2181,192.168.189.102:2181";private static final int SESSION_TIMEOUT = 2000;private ZooKeeper zk = null;/*** @Author 李孟帅* @Description 获取客户端连接* @Date 2020/8/20**/@Beforepublic void getConnection() throws IOException {zk = new ZooKeeper(CONNECTION_STRING, SESSION_TIMEOUT, new Watcher() {@Overridepublic void process(WatchedEvent event) {System.out.println(event);try {zk.getChildren("/",true);} catch (KeeperException | InterruptedException e) {e.printStackTrace();}}});}/*** @Author 李孟帅* @Description 创建节点* @Date 2020/8/20**/@Testpublic void createNode() throws KeeperException, InterruptedException {String s = zk.create("/app1", "this is a test!".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);System.out.println(s);}/*** @Author 李孟帅* @Description 获取节点数据* @Date 2020/8/20**/@Testpublic void getData() throws KeeperException, InterruptedException {byte[] data = zk.getData("/app1", false, null);System.out.println(new String(data));}/*** @Author 李孟帅* @Description 获取子节点* @Date 2020/8/20**/@Testpublic void getChildren() throws KeeperException, InterruptedException {List<String> children = zk.getChildren("/app1", true);for (String child : children) {System.out.println(child);}Thread.sleep(Long.MAX_VALUE);}/*** @Author 李孟帅* @Description 修改节点数据* @Date 2020/8/20**/@Testpublic void setData() throws KeeperException, InterruptedException {Stat stat = zk.setData("/app1", "new data".getBytes(), -1);System.out.println(stat);}/*** @Author 李孟帅* @Description 删除节点* @Date 2020/8/20**/@Testpublic void deleteNode () throws KeeperException, InterruptedException {zk.delete("/app1",-1);}/*** @Author 李孟帅* @Description 判断节点是否存在* @Date 2020/8/21**/@Testpublic void exist() throws KeeperException, InterruptedException {Stat exists = zk.exists("/app2", true);System.out.println(exists);}}
2、服务器动态上线下线监听 服务端
DistributedServer
package com.lms.zookeeper;import org.apache.zookeeper.*;import java.io.IOException;/*** @Author: 李孟帅* @CreateTime: 2020-08-21 08:29* @Description:*/public class DistributedServer {private static final String CONNECTION_STRING = "192.168.189.100:2181,192.168.189.101:2181,192.168.189.102:2181";private static final int SESSION_TIMEOUT = 2000;private ZooKeeper zk = null;/*** @Author 李孟帅* @Description 获取客户端连接* @Date 2020/8/20**/public void getConnection() throws IOException {zk = new ZooKeeper(CONNECTION_STRING, SESSION_TIMEOUT, new Watcher() {@Overridepublic void process(WatchedEvent event) {System.out.println(event);}});}/*** @Author 李孟帅* @Description 注册* @Date 2020/8/21**/public void registryServer(String hostname) throws KeeperException, InterruptedException {String create = zk.create("/servers/"+hostname, hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);System.out.println(hostname + " is online ," + create);}/*** @Author 李孟帅* @Description 业务功能* @Date 2020/8/21**/public void handleBusiness(String hostname) throws InterruptedException {System.out.println(hostname+" start working...");Thread.sleep(Long.MAX_VALUE);}public static void main(String[] args) throws IOException, KeeperException, InterruptedException {//获取zk连接DistributedServer distributedServer = new DistributedServer();distributedServer.getConnection();//利用zk注册服务器信息distributedServer.registryServer(args[0]);//启动业务功能distributedServer.handleBusiness(args[0]);}}
3、服务器动态上线下线监听 客户端
DistributedClient
package com.lms.zookeeper;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;/*** @Author: 李孟帅* @CreateTime: 2020-08-21 08:48* @Description:*/public class DistributedClient {private static final String CONNECTION_STRING = "192.168.189.100:2181,192.168.189.101:2181,192.168.189.102:2181";private static final int SESSION_TIMEOUT = 2000;private ZooKeeper zk = null;private volatile ArrayList<String> serverList=new ArrayList<String>();/*** @Author 李孟帅* @Description 获取客户端连接* @Date 2020/8/20**/public void getConnection() throws IOException {zk = new ZooKeeper(CONNECTION_STRING, SESSION_TIMEOUT, new Watcher() {@Overridepublic void process(WatchedEvent event) {try {getServerList();} catch (KeeperException | InterruptedException e) {e.printStackTrace();}}});}/*** @Author 李孟帅* @Description 获取服务器列表* @Date 2020/8/21**/public void getServerList() throws KeeperException, InterruptedException {//获取子节点信息,并监听父节点List<String> children = zk.getChildren("/servers", true);ArrayList<String> servers = new ArrayList<>();for (String child : children) {byte[] data = zk.getData("/servers/" + child, false, null);servers.add(new String(data));}serverList=servers;System.out.println(serverList);}//业务功能public void handleBusiness() throws InterruptedException {System.out.println("client start working...");Thread.sleep(Long.MAX_VALUE);}public static void main(String[] args) throws IOException, KeeperException, InterruptedException {//获取zk连接DistributedClient distributedClient = new DistributedClient();distributedClient.getConnection();//获取/servers子节点信息(并监听),从中获取服务器列表distributedClient.getServerList();//业务功能distributedClient.handleBusiness();}}
