ZKClientTest
package com.lv.zookeeper;
import com.lv.redis.StringUtil;
import com.lv.redis.User;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lvyanghui
* 2018/6/28 12:16
*/
public class ZKClientTest {
public ZkClient createSession(){
ZkClient zkClient = new ZkClient("192.168.84.128:2181,192.168.84.131:2181,192.168.84.132:2181",10000);
System.out.println("create Session succeed " + zkClient);
return zkClient;
}
public void createNode(ZkClient zkClient){
User user = new User();
user.setId("user1");
user.setName("zknode");
user.setAddress("深圳");
String usrStr = StringUtil.BeanToString(user);
zkClient.createPersistent("/zk",usrStr);
String data = zkClient.readData("/zk");
User dataUsr = StringUtil.StringToBean(data);
System.out.println(dataUsr.getAddress() + dataUsr.getId() + dataUsr.getName());
}
public void deleteNode(ZkClient zkClient){
zkClient.deleteRecursive("/zk");
}
public void getChildNode(ZkClient zkClient){
List<String> list = zkClient.getChildren("/zk");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public void childChangeListener(ZkClient zkClient) throws InterruptedException {
String path = "/zk-listener";
zkClient.subscribeChildChanges(path, new IZkChildListener() {
@Override
public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
System.out.println(parentPath + "下面的子节点有变化,当前子节点" + currentChilds);
}
});
zkClient.createPersistent(path);
Thread.sleep(1000);
System.out.println(zkClient.getChildren(path));
Thread.sleep(1000);
zkClient.createPersistent(path + "/c1");
Thread.sleep(1000);
zkClient.delete(path + "/c1");
Thread.sleep(1000);
zkClient.delete(path);
Thread.sleep(10000);
}
/*public void getNodeData(ZkClient zkClient){
zkClient.createPersistent("/zk-data","datatest");
System.out.println(zkClient.readData("/zk-data"));
}
public void updateNodeData(ZkClient zkClient){
System.out.println(zkClient.readData("/zk-data"));
zkClient.writeData("/zk-data",1234);
System.out.println(zkClient.readData("/zk-data"));
}*/
public void dataChangeListener(ZkClient zkClient) throws InterruptedException {
String path = "/zk-datachange";
zkClient.createPersistent(path,"data");
zkClient.subscribeDataChanges(path, new IZkDataListener() {
@Override
public void handleDataChange(String dataPath, Object data) throws Exception {
System.out.println("节点" + dataPath + "数据更新为" + data);
}
@Override
public void handleDataDeleted(String dataPath) throws Exception {
System.out.println("节点" + dataPath + "数据删除了");
}
});
zkClient.writeData(path,99999);
Thread.sleep(1000);
zkClient.delete(path);
Thread.sleep(10000);
}
public void existNode(ZkClient zkClient){
System.out.println("节点/existNode是否存在:" + zkClient.exists("/existNode"));
}
public void acl(ZkClient zkClient)throws Exception{
List<ACL> acl = new ArrayList<ACL>();
Id adminId = new Id("digest", DigestAuthenticationProvider.generateDigest("root:admin"));
ACL adminAcl = new ACL(ZooDefs.Perms.CREATE|ZooDefs.Perms.READ,adminId);
acl.add(adminAcl);
zkClient.createPersistent("/zk-acl","aclkkk",acl);
System.out.println(zkClient.getAcl("/zk-acl"));
zkClient.addAuthInfo("digest","root:admin".getBytes());
//System.out.println(zkClient.readData("/zk-acl"));
}
public void getAclData(ZkClient zkClient){
zkClient.addAuthInfo("digest","root:admin".getBytes());
//System.out.println(zkClient.readData("/zk-acl"));
zkClient.writeData("/zk-acl",000000);
zkClient.deleteRecursive("/zk-acl");
}
public void setAcl(ZkClient zkClient) throws NoSuchAlgorithmException {
zkClient.addAuthInfo("digest","root:admin".getBytes());
List<ACL> acl = new ArrayList<ACL>();
Id adminId = new Id("digest", DigestAuthenticationProvider.generateDigest("root:admin"));
ACL adminAcl = new ACL(ZooDefs.Perms.ALL,adminId);
acl.add(adminAcl);
zkClient.setAcl("/zk-acl",acl);
}
public static void main(String[] args) throws Exception {
ZKClientTest test = new ZKClientTest();
ZkClient zkClient = test.createSession();
//test.deleteNode(zkClient);
test.createNode(zkClient);
zkClient.createPersistent("/zk/test1");
zkClient.createPersistent("/zk/test2");
test.getChildNode(zkClient);
//test.childChangeListener(zkClient);
//test.getNodeData(zkClient);
//test.updateNodeData(zkClient);
//test.dataChangeListener(zkClient);
//test.existNode(zkClient);
//test.acl(zkClient);
//test.getAclData(zkClient);
//test.setAcl(zkClient);
//test.getAclData(zkClient);
System.out.println(zkClient.exists("/zk"));
}
}
MapBeanUtil
package com.lv.util;
import org.springframework.cglib.beans.BeanMap;
import java.util.*;
/**
* Created by LocalUser on 2018/3/27.
*/
public class MapBeanUtil {
/**
* 将对象装换为map
* @param bean
* @return
*/
public static <T> Map<String, Object> beanToMap(T bean) {
Map<String, Object> map = new HashMap<String, Object>();
if (bean != null) {
BeanMap beanMap = BeanMap.create(bean);
Iterator<Map.Entry<String,Object>> iterator = beanMap.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String,Object> entry = iterator.next();
map.put(entry.getKey()+"", entry.getValue());
}
}
return map;
}
/**
* 将map装换为javabean对象
* @param map
* @param clazz
* @return
*/
public static <T> T mapToBean(Map<String, Object> map,Class<T> clazz) throws InstantiationException, IllegalAccessException{
T bean = clazz.newInstance();
BeanMap beanMap = BeanMap.create(bean);
beanMap.putAll(map);
return bean;
}
/**
* 将List<T>转换为List<Map<String, Object>>
* @param objList
* @return
*/
public static <T> List<Map<String, Object>> objectsToMaps(List<T> objList) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if (objList != null && objList.size() > 0) {
Map<String, Object> map = null;
T bean = null;
for (int i = 0,size = objList.size(); i < size; i++) {
bean = objList.get(i);
map = beanToMap(bean);
list.add(map);
}
}
return list;
}
/**
* 将List<Map<String,Object>>转换为List<T>
* @param maps
* @param clazz
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static <T> List<T> mapsToObjects(List<Map<String, Object>> maps,Class<T> clazz) throws InstantiationException, IllegalAccessException {
List<T> list = new ArrayList<T>();
if (maps != null && maps.size() > 0) {
Map<String, Object> map = null;
T bean = null;
for (int i = 0,size = maps.size(); i < size; i++) {
map = maps.get(i);
mapToBean(map, clazz);
list.add(bean);
}
}
return list;
}
}