Windows环境下搭建ZooKeeper伪集群

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/carson0408/article/details/84203495

        在生产环境和开发环境ZooKeeper服务的搭建,我们一般常用集群和单机两种模式。但是在自己学习过程中,如果想要了解集群模式的性能,则可以使用伪集群来了解ZooKeeper集群模式下的一些工作机制。

1.下载与配置

        下载了ZooKeeper压缩包之后,并将复制成三份文件,对应三个服务器。如下图所示:

        并且为每个服务器conf/文件夹下添加一个zoo.cfg 文件,分别如下:

server1

# 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=D:/ZooKeeper/zookeeper/server1/zookeeper-3.4.12/data 
dataLogDir=D:/ZooKeeper/zookeeper/server1/zookeeper-3.4.12/log  

# 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.1=127.0.0.1:2887:3887 
server.2=127.0.0.1:2888:3888 
server.3=127.0.0.1:2889:3889 

server2

# 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=D:/ZooKeeper/zookeeper/server2/zookeeper-3.4.12/data 
dataLogDir=D:/ZooKeeper/zookeeper/server2/zookeeper-3.4.12/log  

# the port at which the clients will connect
clientPort=2182
# 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=127.0.0.1:2887:3887 
server.2=127.0.0.1:2888:3888 
server.3=127.0.0.1:2889:3889 

server3

# 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=D:/ZooKeeper/zookeeper/server3/zookeeper-3.4.12/data 
dataLogDir=D:/ZooKeeper/zookeeper/server3/zookeeper-3.4.12/log  

# the port at which the clients will connect
clientPort=2183
# 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=127.0.0.1:2887:3887 
server.2=127.0.0.1:2888:3888 
server.3=127.0.0.1:2889:3889 

        上面三个配置文件大致相同,就部分不同:

1.clientPort不同,clientPort即为服务器对外的端口,由于在单机上,所以这三个端口不同,避免引起端口冲突。

2.dataDir和dataLogDir的值不同,具体路径则跟三个软件包的路径相关。

        针对上面三个服务器每个服务器data文件夹都对应一个myid文件,其文件内容只有上面对应server.ID的id值。

        另外,这里有几个端口的概念,clientPort是对外提供服务的访问端口。而server.ID的值为IP:port1:port2.这里的port1是指用于指定Follower服务器与Leader进行运行时通信和数据同步时所使用的端口,第二个端口则专门用于进行Leader选举过程中的投票通信。

2.启动服务器

        逐个启动上面三个服务器,通过cmd命令窗口通过jps命令,可以看到,有三个zookeeper服务器进程。

        根据以下两张图可以看出server2是Leader服务器

   3.客户端请求

        一般客户端请求,分为事务请求与非事务请求。ZooKeeper中事务请求即为会应用到内存数据库的请求,比如插入节点,改节点数据,删除节点等操作都叫作事务请求。而那些不会应用到数据库的请求就叫作非事务请求。

        在集群中,为了保证事务请求被顺序执行,从而确保ZooKeeper集群的数据一致性,所有的事务请求必须由Leader服务器来处理。因此,会涉及到请求事务请求转发的问题,加入Follower服务器接收到一个请求,首先要判断是不是事务请求,如果是需要转发给Leader,需要Leader服务器来处理,如果是非事务请求,Follower服务器可以自己处理。以下通过实际操作来验证,已知当前搭建的伪集群中server2为Leader服务器,剩余两个为Follower服务器。因此先用客户端与server1服务器相连。

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;

public class ZooKeeperTest implements Watcher {
	private static CountDownLatch countDownLatch = new CountDownLatch(1);
	public static void main(String[] args) throws Exception {
		ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 5000, new ZooKeeperTest());
		//String path = "/zk_test/zk";
		//zk.create(path, "123".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

		System.out.println(zk.getSessionId());
		//System.out.println(new String(zk.getData(path, false, null)));
		try {
			countDownLatch.await();
		}catch(InterruptedException e) {
			e.printStackTrace();
			}
		System.out.println("ZooKeeper session established");
	}
	public void process(WatchedEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("Watchedevent is:"+arg0);
		if(KeeperState.SyncConnected==arg0.getState()) {
			
			countDownLatch.countDown();
			
		}

	}

        运行后,观察下图server1的状态,server1自行创建了一个会话,因为尚未初始化之前,创建连接属于非事务请求,所以Follower服务器可以自行处理,创建了会话,并给会话分配了sessionID

        接下来观察事务请求的例子,比如如下代码,插入一个节点:

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;

public class ZooKeeperTest implements Watcher {
	private static CountDownLatch countDownLatch = new CountDownLatch(1);
	public static void main(String[] args) throws Exception {
		ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 5000, new ZooKeeperTest());
		String path = "/zktest1";
		zk.create(path, "123".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

		System.out.println(zk.getSessionId());
		//System.out.println(new String(zk.getData(path, false, null)));
		try {
			countDownLatch.await();
		}catch(InterruptedException e) {
			e.printStackTrace();
			}
		System.out.println("ZooKeeper session established");
	}
	public void process(WatchedEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("Watchedevent is:"+arg0);
		if(KeeperState.SyncConnected==arg0.getState()) {
			
			countDownLatch.countDown();
			
		}

	}

}

        以下分别为server1和server2的状态。

        第一张图说明server1并未处理这个事务请求,观察第二张图,说明server2处理了该请求,说明server1进行了事务请求转发。 

猜你喜欢

转载自blog.csdn.net/carson0408/article/details/84203495