(#)定义
Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群
管理、分布式应用配置项的管理

(#)使用
配置zk客户端我就不多说了,这个网上一找一堆,我主要说一下就是,自己玩玩的话,没必要搭建一个集群,用一台基本问题不大,在此假设集群已经搭建完成了
需要引入zk client的jar包

(#)code

  private Logger logger = LoggerFactory.getLogger(ServiceRepository.class);
@Value("${session.timeout}")
private int SESSION_TIMEOUT;
@Value("${registry.address}")
private String CONNECT_STRING;
@Value("${repository.path}")
private String RPC_PATH;
private ZooKeeper zooKeeper = null;
private CountDownLatch latch = new CountDownLatch(1); private void createConnect() {
releaseConnection();
try {
zooKeeper = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT,
new Watcher() {
public void process(WatchedEvent event) {
logger.info("receive message :{}" + event.getState());
latch.countDown();
}
});
latch.await();
} catch (IOException e) {
logger.error("create异常信息:{}", e);
} catch (InterruptedException e) {
logger.error("create异常信息:{}", e);
}
;
} private void releaseConnection() {
if (zooKeeper != null) {
try {
zooKeeper.close();
} catch (InterruptedException e) {
logger.error("release异常信息:{}", e);
}
}
} private boolean createPath(String path, String data) {
try {
zooKeeper.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch (KeeperException e) {
logger.error("create异常信息:{}", e);
} catch (InterruptedException e) {
logger.error("create异常信息:{}", e);
}
return true;
} public String readData(String path) {
try {
return new String(zooKeeper.getData(path, false, null));
} catch (KeeperException e) {
logger.error("read异常信息:{}", e);
return "";
} catch (InterruptedException e) {
logger.error("read异常信息:{}", e);
return "";
}
} public boolean writeData(String path, String data) {
try {
zooKeeper.setData(path, data.getBytes(), -1);
} catch (KeeperException e) {
logger.error("write异常信息:{}", e);
} catch (InterruptedException e) {
logger.error("write异常信息:{}", e);
}
return false;
} public void deleteNode(String path) {
try {
zooKeeper.delete(path, -1);
} catch (KeeperException e) {
logger.error("delete异常信息:{}", e);
} catch (InterruptedException e) {
logger.error("delete异常信息:{}", e);
}
}
}

  

最新文章

  1. iOS--NSBundle理解
  2. ANE 从入门到精通 --- 简单的Whatever
  3. html随记
  4. nodejs中的Crypto模块
  5. ofbiz进击 个人遇到的奇葩问题汇总。
  6. MFC中消息响应机制
  7. 暑假集训(2)第八弹 ----- Hero(hdu4310)
  8. find_cmd函数分析
  9. Java语言程序设计(基础篇) 第八章 多维数组
  10. 使用函数指针和多态代替冗长的if-else或者switch-case
  11. LeetCodeOJ. Maximum Depth of Binary Tree
  12. Oracle中忘记sys和System密码的办法
  13. visio UML用例里面找不到include关系
  14. OGG强制停止进程
  15. shell vim--处理二进制文本
  16. 【贪心】【堆】Gym -100956D - Greedy Game
  17. seo从业者发展方向
  18. Redishelp
  19. DNS污染
  20. SqlDataAdapter 对datagridview进行增删改(A)

热门文章

  1. ExtJs combobox模糊匹配
  2. 源代码编译安装Python3.5.2
  3. Dell DRAC的重启方法
  4. postgresql常用命令
  5. [课程设计]Scrum 3.7 多鱼点餐系统开发进度(留言板选择方案)
  6. Icon字体制作
  7. 使用maven搭建SpringMVC项目环境
  8. java性能监控常用命令
  9. js 中实现页面跳转的方法(window.location和window.open的区别)
  10. easyui 日期控件清空值