一、本机搭建zookeeper伪集群

1、下载安装包,复制三份

2、每个安装包目录下面新建一个data文件夹,用于存放数据目录

3、安装包的conf目录下,修改zoo.cfg配置文件

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=5
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=2
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#这里要修改成刚才创建的目录
dataDir=D:/tools/zookeeper/zookeeper-3.4.6_1/data
# the port at which the clients will connect
#每个安装包的启动端口要不一样
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1 #server后面的两个端口也必须不同
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:4888:5888
server.3=127.0.0.1:6888:7888

4、上面创建的data目录,新建myid文件,分别写入1,2,3,与配置文件中的server点后面的数字一致

5、进入bin目录,分别启动三个zk

6、在节点1,创建一个znode,并设置初始内容,如下

7、登录其他节点,查看这个znode的内容,如下,可以看到,不同节点的zk已经同步了znode的内容,这就是zk的核心特性,基于这个特性,可以对分布式应用程序实现服务不同、统一配置管理、统一服务命名,服务注册等功能。

二、使用java集成zk,实现znode的增删改查

package net.Eleven.demo.OtherTest;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.util.List;
import java.util.concurrent.CountDownLatch; public class ZookeeperClient implements Watcher {
private ZooKeeper zookeeper;
private static final int SESSION_TIME_OUT=2000; //超时时间
private CountDownLatch countDownLatch = new CountDownLatch(1); @Override
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState()== Event.KeeperState.SyncConnected){
System.out.println("Watch received event");
countDownLatch.countDown();
}
} /**
* 连接zk
* @param host
* @throws Exception
*/
public void connectZookeeper(String host) throws Exception{
zookeeper = new ZooKeeper(host,SESSION_TIME_OUT,this);
countDownLatch.await();
System.out.println("zookeeper.java connection success");
} //创建节点
public String createNode(String path,String data) throws Exception{
return this.zookeeper.create(path,data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} //获取所有节点
public List<String> getChildren(String path) throws KeeperException,InterruptedException{
List<String> children = zookeeper.getChildren(path,false);
return children;
} //获取节点上的数据
public String getData(String path) throws KeeperException,InterruptedException{
byte[] data = zookeeper.getData(path,false,null);
if (data==null){
return "";
}
return new String(data);
} //设置节点信息
public Stat setData(String path,String data) throws KeeperException, InterruptedException{
Stat stat = zookeeper.setData(path, data.getBytes(), -1);
return stat;
} //删除节点
public void deleteNode(String path) throws InterruptedException, KeeperException{
zookeeper.delete(path, -1);
} //关闭连接
public void closeConnection() throws InterruptedException {
if (zookeeper != null) {
zookeeper.close();
}
} public boolean isConnected(){
return zookeeper.getState() == ZooKeeper.States.CONNECTED;
} public static void main(String[] args) throws Exception {
ZookeeperClient zookeeper = new ZookeeperClient();
zookeeper.connectZookeeper("127.0.0.1:2181");
List<String> children = zookeeper.getChildren("/");
System.out.println(children);
zookeeper.createNode("/Eleven4","create node by java");
System.out.println(zookeeper.getData("/Eleven4"));
}
}

最新文章

  1. 【KMP算法】字符串匹配
  2. mac os 下的sublime --- 快捷键
  3. es6中添加块级作用域的目的
  4. UTF-8 BOM头
  5. Introduction to Windows 8: The Definitive Guide for Developer
  6. VB.NET转C#代码的工具
  7. CentOS下命令行和桌面模式的切换方法(转载)
  8. iOS 中Window优先级的问题
  9. 仍需&quot;敬请期待&quot;的微信沃卡
  10. Swift - 类初始化和反初始化方法(init与deinit)
  11. 基于SQLite日志记录工具--Log4W
  12. JAVA设计模式:模板设计模式
  13. 【推荐】开源项目minapp-重新定义微信小程序的开发
  14. IOS11 底部输入框被手机输入法遮住
  15. 关于XMLHttpRequest状态的讨论及处理方法
  16. JAVA回文
  17. UI设计师经常去的五个网站
  18. Android 数据存储04之Content Provider
  19. Eclipse已经安装了SVN插件,但是在获取SVN代码时,一直处于progress....
  20. Java学习(正则表达式、Date类、DateFormat类、Calendar类)

热门文章

  1. 一个简单的利用 WebClient 异步下载的示例(四)
  2. 【杂文】CSP2019蒟蒻AFO(假)记
  3. CD 基金会、Jenkins、Jenkins X、Spinnaker 和 Tekton 的常问问题
  4. C# 重载 overload,重写override覆盖
  5. 「白帽挖洞技能提升」ThinkPHP5 远程代码执行漏洞-动态分析
  6. ubuntu下编译android jni到so库的mk文件配置
  7. javascript 和oc交互
  8. Factorization Machine算法
  9. ubuntu 16.04下node和pm2安装
  10. 笔记5:Django知识一