Zookeeper 監控服務上下線
阿新 • • 發佈:2019-01-30
package nue.edu.ls; 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; /** * 服務端 * 1:建立連結 * 2:註冊 * 3:server端的業務處理 * @author root * */ public class DistributedServer { private static final String connectString = "jiqun01:2181,jiqun02:2181,jiqun03:2181"; private static final int sessionTimeout = 2000; private static final String parentNode = "/servers"; ZooKeeper zkClient = null; /** * 獲取zk連線 * @throws Exception */ public void getConnect() throws Exception{ zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() { @Override public void process(WatchedEvent event) { //收到事件通知後呼叫的回撥函式 System.out.println(event.getPath()+""+event.getType()); try { zkClient.getChildren("/", true); } catch (KeeperException | InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } /** * 向zk註冊伺服器的資訊 * @throws InterruptedException * @throws KeeperException */ public void registerServer(String hostName) throws KeeperException, InterruptedException{ String create = zkClient.create(parentNode+"/server", hostName.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); System.out.println(hostName+" is online "+ create); } /** * 處理業務邏輯 * @throws InterruptedException */ public void handleBussiness(String hostName) throws InterruptedException{ System.out.println(hostName + " start working ......"); Thread.sleep(Long.MAX_VALUE); } public static void main(String[] args) throws Exception { // 獲取zk連結 DistributedServer server = new DistributedServer(); server.getConnect(); //利用zk連結向zk進行註冊 server.registerServer("alibaba"); // 啟動業務功能 server.handleBussiness("alibaba"); } }
package nue.edu.ls; 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; /** * 服務端 * 1:建立連結 * 2:註冊 * 3:server端的業務處理 * @author root * */ public class DistributedServer { private static final String connectString = "jiqun01:2181,jiqun02:2181,jiqun03:2181"; private static final int sessionTimeout = 2000; private static final String parentNode = "/servers"; ZooKeeper zkClient = null; /** * 獲取zk連線 * @throws Exception */ public void getConnect() throws Exception{ zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() { @Override public void process(WatchedEvent event) { //收到事件通知後呼叫的回撥函式 System.out.println(event.getPath()+""+event.getType()); try { zkClient.getChildren("/", true); } catch (KeeperException | InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } /** * 向zk註冊伺服器的資訊 * @throws InterruptedException * @throws KeeperException */ public void registerServer(String hostName) throws KeeperException, InterruptedException{ String create = zkClient.create(parentNode+"/server", hostName.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); System.out.println(hostName+" is online "+ create); } /** * 處理業務邏輯 * @throws InterruptedException */ public void handleBussiness(String hostName) throws InterruptedException{ System.out.println(hostName + " start working ......"); Thread.sleep(Long.MAX_VALUE); } public static void main(String[] args) throws Exception { // 獲取zk連結 DistributedServer server = new DistributedServer(); server.getConnect(); //利用zk連結向zk進行註冊 server.registerServer("alibaba"); // 啟動業務功能 server.handleBussiness("alibaba"); } }
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>nuc.edu</groupId> <artifactId>Zookeeper</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <!-- <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> <scope>test</scope> </dependency> --> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </project>
結果:
關閉服務端後: