Zookeeper使用范例1 统一配置管理
该Demo主要是用来实现多个Thread线程来模拟集群中的多台机器,统一使用某一个Zookeeper来读取相关的配置信息,当Zookeeper相关的节点信息做出了改变的时候相应的每一个Thread线程的配置信息也作出改变。
类1 MyZookeeper(该类主要负责连接Zookeeper,初始化Watcher中的相关信息,以及设置初次的Watcher)
package org.com.hugu.bookdinner.zookeeper;
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
/**
*
* @author Administrator
* Zookeeper连接类,主要负责Zookeeper的连接,Watcher相关信息的初始化以及设置第一次Watcher。
*
*/
public class MyZookeeper
{
private ZooKeeper zk;
private MyWatcher myWatcher = new MyWatcher();
String address1 = "192.168.199.129:21815";
String address2 = "127.0.0.1:2181";
public void connect()
{
System.out.println("Begin to Connect the Zookeeper!");
try {
//创建连接
zk = new ZooKeeper(address1,50000,myWatcher);
//MyZookeeper里面的ZK创建连接之后使用该连接
myWatcher.setMyZK(this);
//通过查询操作来对目标节点设置Watcher
myWatcher.setTimeOut(Integer.parseInt(getNodeValue("/testconfig/timeout")));
myWatcher.setWorking(Boolean.parseBoolean(getNodeValue("/testconfig/isworking")));
myWatcher.setSleepTime(Integer.parseInt(getNodeValue("/testconfig/sleeptime")));
}
catch (IOException e)
{
System.out.println("Connect To The Zookeeper Fail!");
}
}
public MyZookeeper(MyWatcher myWatcher)
{
this.myWatcher = myWatcher;
}
/**
*
* @param path
* @param data
* 创建Node节点
*/
public void createNode(String path,String data)
{
if(path != null && path != "")
{
try {
zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
*
* @param path
* @param value
* @param version
* 对Node节点进行值设定
*
*/
public void setNodeValue(String path,String value,int version)
{
if(path != null && path != "")
{
try {
zk.setData(path, value.getBytes(), version);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
*
* @param path
* @return
* 对Node节点的值获取
*/
public String getNodeValue(String path)
{
if(path != null && path != "")
{
try {
String nodeValue = new String(zk.getData(path, true, null));
System.out.println("NodeValue : " + nodeValue);
return nodeValue;
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
}
类2 MyWatcher 自定义的一个Watcher,实现了Watcher接口的Process方法,用于进行Watcher触发之后的相关处理(读取配置,设置下一次Watcher等等操作)
package org.com.hugu.bookdinner.zookeeper;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
/**
*
* @author Administrator
* 该类实现了Zookeeper的Watcher接口中的Process方法
* 负责当监视的相关Node节点发生了变化做出相关的操作(更改配置,并设置下一个Watcher)
*/
public class MyWatcher implements Watcher
{
private int timeOut;
private boolean isWorking;
private int sleepTime;
private MyZookeeper myZK;
public void process(WatchedEvent event)
{
if(event.getPath().equals("/testconfig/timeout"))
{
timeOut = Integer.parseInt(myZK.getNodeValue("/testconfig/timeout"));
}
if(event.getPath().equals("/testconfig/isworking"))
{
isWorking = Boolean.parseBoolean(myZK.getNodeValue("/testconfig/isworking"));
}
if(event.getPath().equals("/testconfig/sleeptime"))
{
sleepTime = Integer.parseInt(myZK.getNodeValue("/testconfig/sleeptime"));
}
}
public int getTimeOut() {
return timeOut;
}
public void setTimeOut(int timeOut) {
this.timeOut = timeOut;
}
public boolean isWorking() {
return isWorking;
}
public void setWorking(boolean isWorking) {
this.isWorking = isWorking;
}
public int getSleepTime() {
return sleepTime;
}
public void setSleepTime(int sleepTime) {
this.sleepTime = sleepTime;
}
public MyZookeeper getMyZK() {
return myZK;
}
public void setMyZK(MyZookeeper myZK) {
this.myZK = myZK;
}
}
类3 ,WorkThread 用来模拟集群中的一台机器进行读取并使用配置的操作
package org.com.hugu.bookdinner.zookeeper;
/**
*
* @author Administrator
* 工作线程,用于读取相关的配置并执行相关的操作。
*/
public class WorkThread implements Runnable
{
int threadId;
MyWatcher myWatcher;
public WorkThread(MyWatcher myWatcher,int threadId)
{
this.threadId = threadId;
this.myWatcher = myWatcher;
}
public void run()
{
while(myWatcher.isWorking())
{
System.out.println(threadId + ":" + myWatcher.getTimeOut());
try {Thread.sleep(myWatcher.getSleepTime());} catch (InterruptedException e) {e.printStackTrace();}
}
}
}
类4 ZKConnetcTest测试类 定义了三个线程来模拟三台机器,使用同一配置,及时响应远端Zookeeper上面的配置的改变
package org.com.hugu.bookdinner.zookeeper;
public class ZKConnetcTest
{
public static void main(String [] args)
{
//创建一个Watcher
MyWatcher myWatcher = new MyWatcher();
//Zookeeper开始连接,并初始化Watcher中的MyZookeeper实例
MyZookeeper myZK = new MyZookeeper(myWatcher);
myZK.connect();
//工作线程开始启动
WorkThread wt1 = new WorkThread(myWatcher,1);
WorkThread wt2 = new WorkThread(myWatcher,2);
WorkThread wt3 = new WorkThread(myWatcher,3);
Thread t1 = new Thread(wt1);
Thread t2 = new Thread(wt2);
Thread t3 = new Thread(wt3);
t1.start();
t2.start();
t3.start();
}
}