ZooKeeper之服务器动态上下线案例

 公司新闻     |      2019-11-13 12:59

某分布式系统中,主节点可以有多台,可以动态上下线,任意一台客户端都能实时感知到主节点服务器的上下线。

 

 

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

 

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 {
 @override
 public void process {
 // todo auto-generated method stub
 // 注册服务器
 public void registserver throws exception {
 system.out.println;
 string path = zk.create, ids.open_acl_unsafe,
 createmode.ephemeral_sequential);
 system.out.println;
 // 业务功能
 public void business throws interruptedexception {
 system.out.println;
 thread.sleep;
 public static void main throws exception {
 // 1.获取zk连接
 distributeserver server = new distributeserver;
 server.getconnect;
 // 2.利用zk连接注册服务器信息
 server.registserver;
 // 3.启动业务功能
 server.business;

 

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 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;
 thread.sleep;
 private string parentnode="/servers";
 private void getserverlist throws keeperexception, interruptedexception {
 // todo auto-generated method stub
 //1.获取服务器子节点信息,并且对父节点进行监听
 list string children = zk.getchildren;
 //2.存储服务器信息列表
 arraylist string servers=new arraylist ;
 //3.遍历所有节点,获取节点中的主机名称信息
 for {
 byte[] data=zk.getdata;
 servers.add);
 //4.打印服务器列表信息
 system.out.println;
 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 {
 @override
 public void process {
 // todo auto-generated method stub
 try {
 getserverlist;
 }catch {
 e.printstacktrace;

 

先运行客户端

在xshell上登录客户端,进行一些操作,可以看到eclipse的控制台会实时输出有哪些节点。

 

然后运行服务器端,记得先在run configuration里设置一下参数,比如设成hadoop103,那么运行后,客户端的控制台会实时显示当前的节点。

 

 点上面那个红框可以切换控制台。