ZooKeeper之服务器动态上下线案例
需求
某分布式系统中,主节点可以有多台,可以动态上下线,任意一台客户端都能实时感知到主节点服务器的上下线。
需求分析
具体实现
先在集群上创建/servers节点
create /servers "servers"
一些依赖
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mcq</groupId> <artifactId>zookeeper1026</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> </dependencies> </project>
在src/main/resources下添加一个file log4j.properties:
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=target/spring.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
服务器端向 Zookeeper 注册代码
package com.mcq.zookeeper1026; 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 DistributeServer { private String connectString = "hadoop103:2181,hadoop104:2181,hadoop105:2181"; private int sessionTimeOut = 2000; private ZooKeeper zk = null; private String parentNode = "/servers"; public void getConnect() throws IOException { zk = new ZooKeeper(connectString, sessionTimeOut, new Watcher() { @Override public void process(WatchedEvent event) { // TODO Auto-generated method stub } }); } // 注册服务器 public void registServer(String hostname) throws Exception { System.out.println(zk); String path = zk.create(parentNode + "/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); System.out.println(hostname + " is online " + path); } // 业务功能 public void business(String hostname) throws InterruptedException { 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]); } }
客户端代码
package com.mcq.zookeeper1026; 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 DistributeClient { public static void main(String[] args) throws IOException, KeeperException, InterruptedException { //1.获取zk连接 DistributeClient client=new DistributeClient(); client.getConnect(); //2.获取servers的子节点信息,从中获取服务器信息列表 client.getServerList(); //3.业务进程启动 client.business(); } private void business() throws InterruptedException { // TODO Auto-generated method stub System.out.println("client is working ……"); Thread.sleep(Long.MAX_VALUE); } private String parentNode="/servers"; private void getServerList() throws KeeperException, InterruptedException { // TODO Auto-generated method stub //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); } private ZooKeeper zk=null; private String connectString="hadoop103:2181,hadoop104:2181,hadoop105:2181"; private int sessionTimeOut=2000; private void getConnect() throws IOException { // TODO Auto-generated method stub //创建到zk的客户端连接 zk=new ZooKeeper(connectString,sessionTimeOut,new Watcher() { @Override public void process(WatchedEvent event) { // TODO Auto-generated method stub try { getServerList(); }catch(Exception e) { e.printStackTrace(); } } }); } }
运行程序
先运行客户端
在xshell上登录客户端(bin/zkCli.sh),进行一些操作,可以看到eclipse的控制台会实时输出有哪些节点。
然后运行服务器端,记得先在run configuration里设置一下参数,比如设成hadoop103,那么运行后,客户端的控制台会实时显示当前的节点。
点上面那个红框可以切换控制台。