`
sungang_1120
  • 浏览: 310949 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
收藏列表
标题 标签 来源
java 创建 和 读取 zookeeper 节点 zookeeper
package zookeeper.study1;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class SetConfig {

	public static String url = "192.168.1.101:2181";
	private final static String root = "/myConf";
	private final static String urlNode = root + "/url";
	private final static String userNameNode = root + "/userName";
	private final static String passwordNode = root + "/password";

	private final static String auth_type = "digest";
	private final static String auth_passwd = "password";

	private final static String DB_URL = "192.168.1.101";
	private final static String DB_USERNAME = "root";
	private final static String DB_PASSWD = "root123";

	public static void main(String[] args) throws Exception {
		ZooKeeper zk = new ZooKeeper(url, 3000, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
				System.out.println("触发了事件...");
			}
		});

		while (ZooKeeper.States.CONNECTED != zk.getState()) {
			Thread.sleep(3000);
		}

		zk.addAuthInfo(auth_type, auth_passwd.getBytes());
		//zk.delete(urlNode, -1);
		//zk.delete(userNameNode, -1);
		//zk.delete(passwordNode, -1);
		//zk.delete(root, -1);

		if (zk.exists(root, true) == null) {
			zk.create(root, "root".getBytes(), Ids.CREATOR_ALL_ACL,
					CreateMode.PERSISTENT);
		}
		if (zk.exists(urlNode, true) == null) {
			zk.create(urlNode, DB_URL.getBytes(), Ids.CREATOR_ALL_ACL,
					CreateMode.PERSISTENT);
		}
		if (zk.exists(userNameNode, true) == null) {
			zk.create(userNameNode, DB_USERNAME.getBytes(),
					Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
		}
		if (zk.exists(passwordNode, true) == null) {
			zk.create(passwordNode, DB_PASSWD.getBytes(), Ids.CREATOR_ALL_ACL,
					CreateMode.PERSISTENT);
		}
		zk.close();
	}
}


//=============================


package zookeeper.study1;

import java.io.IOException;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

public class Client implements Watcher{
	
	public static String url = "192.168.1.101:2181";
	
	private final static String root = "/myConf";
	private final static String urlNode = root + "/url";
	private final static String userNameNode = root + "/userName";
	private final static String passwordNode = root + "/password";
	
	private final static String auth_type = "digest";
	private final static String auth_passwd = "password";
	
	private String urlString ;
	private String userName;
	private String passwd;
	
	private ZooKeeper zk = null;
	
	
	public void initValue() throws KeeperException, InterruptedException{
		//zk = new ZooKeeper(url, 3000, watcher)
		urlString = new String(zk.getData(urlNode, true, null)); 
		userName = new String(zk.getData(userNameNode, true, null)); 
		passwd = new String(zk.getData(passwordNode, true, null)); 
	}
	
	public ZooKeeper getZK() throws Exception{
		zk =  new ZooKeeper(url,3000,this);
		//权限认证
		zk.addAuthInfo(auth_type,auth_passwd.getBytes());
		while (zk.getState() != ZooKeeper.States.CONNECTED) {
			Thread.sleep(3000);
		}
		System.out.println("");
		return zk;
	}
	
	public static void main(String[] args) throws Exception {
		Client client = new Client();
		ZooKeeper zooKeeper = client.getZK();
		client.initValue();
		int i = 0;
		while (true) {
			System.out.println("userString : " + client.getUrlString());
			System.out.println("userName : " + client.getUserName());
			System.out.println("passwd : " + client.getPasswd());
			System.out.println("======================================");
			Thread.sleep(5000);
			i++;
			if (i == 5) {
				break;
			}
		}
	}
	
	@Override
	public void process(WatchedEvent event) {
		if (event.getType() == Watcher.Event.EventType.None) {
			System.out.println("连接服务器成功");
		}
		if (event.getType() == Watcher.Event.EventType.NodeCreated) {
			System.out.println("节点创建成功");
		}
		if(event.getType() == Watcher.Event.EventType.NodeChildrenChanged){
			System.out.println("子节点更新成功");
			//更新读取新的配置
			try {
				initValue();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (event.getType() == Watcher.Event.EventType.NodeDataChanged) {
			System.out.println("节点更新成功");
			//更新读取新的配置
			try {
				initValue();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (event.getType() == Watcher.Event.EventType.NodeDeleted) {
			System.out.println("节点删除成功");
		}
	}


	public String getUrlString() {
		return urlString;
	}


	public void setUrlString(String urlString) {
		this.urlString = urlString;
	}


	public String getUserName() {
		return userName;
	}


	public void setUserName(String userName) {
		this.userName = userName;
	}


	public String getPasswd() {
		return passwd;
	}


	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

}
Global site tag (gtag.js) - Google Analytics