zookeeper学习笔记(一)

1、搭建一个zookeeper集群

  • 解压zookeeper
    tar -xvf zookeeper-3.4.14.tar.gz
    在这里插入图片描述

  • cd 到conf目录下
    cp zoo-sample.cfg zoo1.cfg cp zoo-sample.cfg zoo2.cfg、cp zoo-sample.cfg zoo3.cfg 分别对zoo1、2、3文件进行编辑

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/apps/servers/data/d_1
dataLogDir=/apps/servers/logs/logs_1
# the port at which the clients will connect
clientPort=2181 
#不同zoo.cfg修改自己的属性和端口号
# 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.1=localhost:2187:2887 
server.2=localhost:2188:2888
server.3=localhost:2189:2889

修改之后分别创建data目录和日志目录

mkdir /apps/servers/data/d_1
Mkdir /apps/servers/data/d_1
mkdir /apps/servers/data/d_1

mkdir /apps/servers/logs/logs_1
mkdir /apps/servers/logs/logs_1
mkdir /apps/servers/logs/logs_1

echo "1" > /apps/servers/data/d_1/myid
echo "2" >/apps/servers/data/d_2/myid
echo "3" >/apps/servers/data/d_3/myid

在这里插入图片描述

进入bin目录下输入命令 分别进行启动

./zkServer.sh start ../conf/zoo1.cfg
./zkServer.sh start ../conf/zoo2.cfg
./zkServer.sh start ../conf/zoo3.cfg

zkServer.sh status或者zkCli.sh -server localhost:2187; localhost:2188; localhost:2189 是否可以连接成功.如果不能连接上查看下错误日志.
在这里插入图片描述

2 . zookeeper的znode节点进行CRUD和监听操作

  • .创建节点
    在这里插入图片描述
  • 获取节点信息

在这里插入图片描述

  • 修改节点
    在这里插入图片描述

  • 删除节点
    -在这里插入图片描述
    3.用java api连接zookeeper并进行CRUD监听和操作。

 package zookeeper01;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.List;

public class ZooKeeperTest { 
	private final String connectString= "192.168.0.104:2187";
	   private ZooKeeper zookeeper;

	   public ZooKeeperTest() {
	      try {
	         this.zookeeper = new ZooKeeper(connectString,5000,null);
	      } catch (Exception e) {
	         e.printStackTrace();
	      }
	   }

	   /***
	    * 创建持久节点
	    * 配色整特
	    * @param path
	    * @param data
	    * @return
	    */
	   public String createPersistent(String path,String data){
	      try {
	         return  zookeeper.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	      } catch (KeeperException e) {
	         e.printStackTrace();
	      } catch (InterruptedException e) {
	         e.printStackTrace();
	      }
	      return  null;
	   }


	   /***
	    * 创建临时节点
	    * 因法美楼
	    * 银困受SEQUENTIAL
	    * @param path
	    * @param data
	    * @return
	    */
	   public String createEphemeral(String path,String data){
	      try {
	         return  zookeeper.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
	      } catch (KeeperException e) {
	         e.printStackTrace();
	      } catch (InterruptedException e) {
	         e.printStackTrace();
	      }
	      return  null;
	   }

	   /***
	    * 更新信息
	    * @param path
	    * @return
	    * @throws KeeperException
	    * @throws InterruptedException
	    */
	   public String getData(String path) throws KeeperException, InterruptedException {
	      byte data[] = zookeeper.getData(path,false,null);
	      data = (data == null)? "null".getBytes() : data;
	      return new String(data);
	   }


	   /***
	    * 更新信息
	    * @param path
	    * @param data
	    * @return
	    * @throws KeeperException
	    * @throws InterruptedException
	    */
	   public Stat setData(String path, String data) throws KeeperException, InterruptedException {
	      return zookeeper.setData(path, data.getBytes(), -1);
	   }

	   /***
	    * 是否存在
	    * @param path
	    * @return
	    * @throws KeeperException
	    * @throws InterruptedException
	    */
	   public Stat exists(String path) throws KeeperException, InterruptedException {
	      return zookeeper.exists(path,false);

	   }


	   /***
	    * 删除
	    * @param path
	    * @return
	    * @throws KeeperException
	    * @throws InterruptedException
	    */
	   public void delete(String path) throws KeeperException, InterruptedException {
	      zookeeper.delete(path,-1);
	   }
	   /***
	    * 删除
	    * @param path
	    * @return
	    * @throws KeeperException
	    * @throws InterruptedException
	    */
	   public void deleteRecursive(String path) throws KeeperException, InterruptedException {
	      ZKUtil.deleteRecursive(zookeeper, path);
	   }

	   public void close() throws InterruptedException {
	      zookeeper.close();
	   }
	   
	   public static void main(String[] args) {
		   ZooKeeperTest zooKeeperTest = new ZooKeeperTest();
		   zooKeeperTest.createPersistent("/yrz1","yrz1");
	}
}

.遇到的问题

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

可以看到防火墙已经关闭,端口连接也是这个,但是这个问题依然存在。有知道原因的小伙伴可以一起讨论。
其他解决方案基本就是防火墙和版本,但LK试过觉得都不行。
经过不懈努力,LK找到自己的问题主要是使用zookeeper的版本存在问题,之前是3.4.14,后来使用的是3.4.9。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值