某分佈式系統中,主節點能夠有多臺,能夠動態上下線,任意一臺客戶端都能實時感知到主節點服務器的上下線。html
pom.xml:java
<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:spring
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
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(); } } }); } }
先運行客戶端shell
在xshell上登陸客戶端(bin/zkCli.sh),進行一些操做,能夠看到eclipse的控制檯會實時輸出有哪些節點。apache
而後運行服務器端,記得先在run configuration裏設置一下參數,好比設成hadoop103,那麼運行後,客戶端的控制檯會實時顯示當前的節點。服務器
點上面那個紅框能夠切換控制檯。session