写点什么

Zookeeper 配置管理自动更新

用户头像
binfirechen
关注
发布于: 1 小时前

一、服务端配置 Znode,包括重新初始化

package Zookeeper;

import org.apache.zookeeper.*;import org.junit.Test;
import java.io.IOException;
public class Server {
private static String url="192.168.186.128:2181"; private static String root="/mysql"; private static String userNameNode=root+"/username"; private static String passwordNode=root+"/password";
private static String auth_type="digest"; private static String auth_password="123456";
@Test public void init() throws IOException, InterruptedException, KeeperException { ZooKeeper zooKeeper=new ZooKeeper(url, 3000, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { System.out.println("触发了事件:"+watchedEvent.getType()); } });
while (zooKeeper.getState()!=ZooKeeper.States.CONNECTED){ Thread.sleep(3000); } zooKeeper.addAuthInfo(auth_type,auth_password.getBytes()); zooKeeper.delete(userNameNode,-1); zooKeeper.delete(passwordNode,-1); zooKeeper.delete(root,-1);

if(zooKeeper.exists(root,true)==null){ zooKeeper.create(root,"mysql".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL,CreateMode.PERSISTENT); } if(zooKeeper.exists(userNameNode,true)==null){ zooKeeper.create(userNameNode,"root".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL,CreateMode.PERSISTENT); } if(zooKeeper.exists(passwordNode,true)==null){ zooKeeper.create(passwordNode,"Qingfeng@2020".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL,CreateMode.PERSISTENT); }
zooKeeper.close();
System.out.println("finished"); }

}
复制代码


问题:

1、ls 目录无权限

addauth digest 123456 /mysql


digest 和 123456 对应



二、Client 读取和 watch znode 值变更


package Zookeeper;
import lombok.Data;import lombok.SneakyThrows;import org.apache.zookeeper.*;import org.junit.Test;
import java.io.IOException;
@Datapublic class MyZKClient implements Watcher { private String url="192.168.186.128:2181"; private String rootNode ="/mysql"; private String userNameNode= rootNode +"/username"; private String passwordNode= rootNode +"/password";
private String auth_type="digest"; private String auth_password="123456";
private String rootString; private String userNameString; private String passwordString; private ZooKeeper zooKeeper=null;
public void initConnectString() throws KeeperException, InterruptedException { this.rootString=new String(zooKeeper.getData(rootNode,true,null)); this.userNameString=new String(zooKeeper.getData(userNameNode,true,null)); this.passwordString=new String(zooKeeper.getData(passwordNode,true,null)); }
public ZooKeeper init() throws IOException, InterruptedException, KeeperException { zooKeeper=new ZooKeeper(url, 3000, this); zooKeeper.addAuthInfo(auth_type,auth_password.getBytes()); while (zooKeeper.getState()!=ZooKeeper.States.CONNECTED){ Thread.sleep(3000); } System.out.println("connect server success."); return zooKeeper; }
@Test public void test() throws InterruptedException, IOException, KeeperException { MyZKClient client=new MyZKClient(); ZooKeeper zooKeeper = client.init(); int i=0; while (true){ client.initConnectString();
System.out.println("root:"+client.getRootString()); System.out.println("userName:"+client.getUserNameString()); System.out.println("password:"+client.getPasswordString());
System.out.println("------------------------------------------"); i++; Thread.sleep(10000);
if(i==100){ break; } } zooKeeper.close(); }
@SneakyThrows @Override public void process(WatchedEvent watchedEvent) {
if(watchedEvent.getType()== Event.EventType.None){ System.out.println("连接成功"); } if(watchedEvent.getType()== Event.EventType.NodeCreated){ System.out.println("节点创建成功"); } if(watchedEvent.getType()== Event.EventType.NodeDeleted){ System.out.println("节点删除成功"); } if(watchedEvent.getType()== Event.EventType.NodeDataChanged){ System.out.println("节点更新成功"); this.initConnectString(); } if(watchedEvent.getType()== Event.EventType.NodeChildrenChanged){ System.out.println("子节点创建成功"); this.initConnectString(); } }}
复制代码




用户头像

binfirechen

关注

还未添加个人签名 2020.11.27 加入

熟悉程度决定应用高度

评论

发布
暂无评论
Zookeeper配置管理自动更新