Zookeeper集群搭建

13 篇文章 0 订阅

Zookeeper集群搭建

环境

我搞了3台cenos6.4的装的mini版(最小安装版)放在vmware中

然后centos配置如下

server1:192.168.25.141

server2:192.168.25.142

server2:192.168.25.142

jdk

zookeeper3.4.5

服务器之间可以相互访问

将zookeeper解压后我就直接放在 /root/apps/ 下,绝对路径为 /root/apps/zookeeper-3.4.5/

为了方面3台服务器都解压后的路径都一样

配置

搞完上面那些后搞配置

jdk 我是这样配置的 直接在 /etc/profile 文件配置

直接在 /etc/profile 文件末尾追加
#this is jdk home
export JAVA_HOME=/home/apps/jdk1.7
export PATH=$PATH:$JAVA_HOME/bin

然后再搞zookeeper的配置文件

cd /root/apps/zookeeper-3.4.5/conf/ 进入配置目录下他最初有一个 zoo_sample.cfg 你要复制一份并命名为zoo.cfg这是我们要改写的文件, 使用 cp zoo_sample.cfg zoo.cfg 后大概是这个样子的
在这里插入图片描述
然后就搞zoo.cfg 文件 vi zoo.cfg 后 搞成这个样子的就可以了

# 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=/root/zkdata
# the port at which the clients will connect
clientPort=2181
#
# 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=192.168.25.141:2888:3888 
server.2=192.168.25.142:2888:3888
server.3=192.168.25.143:2888:3888
然后还要在 /root/ 目录下建立一个 zookeeper的数据文件 zkdata 

执行 mkdir /root/zkdata

并在 zkdata目录下建立一个 myid 文件;假设当前是你的第一台服务器那么文件内容为 1,如果是第二台那么为2,以此类推。

之后每台服务器都这么干,就可以了。

假设第一台那么建立向下面这样的,记住每一台都要建立
在这里插入图片描述
再看一下第二台服务器配置
在这里插入图片描述
记住每一台都要配置上述步骤

测试

然后每个服务器切换到 cd /root/apps/zookeeper-3.4.5/bin/ 目录下 大概如下
在这里插入图片描述
然后就每台服务器都运行这个 ./zkServer.sh start 就OK
在这里插入图片描述或者
说明成功了。

Java客户端连接

参考 https://www.cnblogs.com/zjiacun/p/7654894.html
客户端连接
import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.junit.Before;
import org.junit.Test;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class SimpleZkClient {
	private static final String connectString = "192.168.25.141:2181,192.168.25.142:2181,192.168.25.143:2181";
	private static final int sessionTimeout = 2000;

	ZooKeeper zkClient = null;

public static void main(String[] args) throws Exception {
	ZooKeeper zk = new ZooKeeper("192.168.25.141:2181", 2000, null);
	
	System.out.println("o1k");
	Thread.sleep(10000);
	System.out.println("ok");
	
	System.out.println(zk.getClass());
	if (zk.exists("/test", false) == null) {
		zk.create("/test", "znode1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	}
	System.out.println(new String(zk.getData("/test", false, null)));
}

@Before()
public void init() throws Exception {
	zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {

		@Override
		public void process(WatchedEvent event) {
			// TODO Auto-generated method stub
			System.out.println(event.getType() + "------------" + event.getPath());
			try {
				zkClient.getChildren("/", true);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	});
	System.out.println("连接成功");
	Thread.sleep(10000);//等待10s 不然就报错,其他人报不报错我不知道,反正我的不停救报错
}

/**
 * 创建数据节点到zk中
 */
@Test
public void testCreate() throws Exception {
	String nodeCreated = zkClient.create("/eclipse", "hellozk".getBytes(), Ids.OPEN_ACL_UNSAFE,
			CreateMode.PERSISTENT);
	//上传的数据可以是任意类型,但都要转换成byte []
	
	// System.out.println("nodeCreated = "+nodeCreated);
}

/**
 * 测试某数据是否存在
 * @throws Exception
 */
@Test
public void testExsit() throws Exception {
	Stat stat = zkClient.exists("/eclipse", false);
	System.out.println(stat==null?"not exsit":"exsit");
}

/**
 * 获取子节点
 * @throws Exception
 */
@Test
public void getChildren() throws Exception {
	List<String> children = zkClient.getChildren("/", true);
	for (String child : children) {
		System.out.println(child);
	}
	Thread.sleep(Long.MAX_VALUE);
}

/**
 * 获取 znode 数据
 * @throws Exception
 */
@Test
public void getData() throws Exception {
	byte [] data = zkClient.getData("/eclipse", false, null);
	System.out.println(new String(data));
}

/**
 * 删除znode数据
 * @throws Exception
 */
@Test
public void deleteZnode() throws Exception {
	//参数2 => 表示要参数的版本, -1表示删除所有版本
	zkClient.delete("/eclipse", -1);
}

/**
 * 修改znode数据
 * @throws Exception
 */
@Test
public void setZnode() throws Exception {
	zkClient.setData("/eclipse", "change data".getBytes(), -1);
}
}

编程群聊:726688057


作者:yangmingsen1999
来源:CSDN
原文:https://blog.csdn.net/yangmingsen1999/article/details/86499585
版权声明:本文为博主原创文章,转载请附上博文链接!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值