需求 某分散式系統中,主節點可以有多台,可以動態上下線,任意一臺客戶端都能實時感知到主節點伺服器的上下線。 需求分析 具體實現 先在集群上創建/servers節點 create /servers "servers" 一些依賴 pom.xml: 在src/main/resources下添加一個file ...
需求
某分散式系統中,主節點可以有多台,可以動態上下線,任意一臺客戶端都能實時感知到主節點伺服器的上下線。
需求分析
具體實現
先在集群上創建/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,那麼運行後,客戶端的控制台會實時顯示當前的節點。
點上面那個紅框可以切換控制台。