Zookeeper实战之嵌入式执行Zookeeper集群模式

2023-06-07,,

非常多使用Zookeeper的情景是须要我们嵌入Zookeeper作为自己的分布式应用系统的一部分来提供分布式服务。此时我们须要通过程序的方式来启动Zookeeper。此时能够通过Zookeeper API的ZooKeeperServerMain类来启动Zookeeper服务。
 
以下是一个集群模式下启动Zookeeper服务的样例
 
这里假定我们执行Zookeeper集群的三台机器名分别为fanbinx1,fanbinx2,fanbinx3
 

首先是zoo.cfg配置文件

tickTime=2000
dataDir=/tmp/zookeeper/data
clientPort=2181
initLimit=10
syncLimit=5
server.1=fanbinx1:2888:3888
server.2=fanbinx2:2888:3888
server.3=fanbinx3:2888:3888

启动Zookeeper集群服务的类。例如以下 
* 这个类同一时候使用同一个zoo.cfg配置文件来启动Zookeeper服务。

* 在每台机器上启动Zookeeper服务的时候推断当前机器是不是定义在zoo.cfg文件中,假设是获取当中的ID号,然后生成myid文件并将ID写入当中。
* 最后启动Zookeeper服务。

package my.zookeeperstudy.server;

import org.apache.commons.io.FileUtils;
import org.apache.zookeeper.server.ServerConfig;
import org.apache.zookeeper.server.ZooKeeperServerMain;
import org.apache.zookeeper.server.quorum.QuorumPeerConfig; import java.io.File;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class ClusteredZKServer { public static void main(String[] args) throws Exception {
InputStream is = ClusteredZKServer.class.getResourceAsStream("/my/zookeeperstudy/server/zoo.cfg");
Properties props = new Properties();
try {
props.load(is);
} finally {
is.close();
} for (String key : props.stringPropertyNames()) {
Pattern pKey = Pattern.compile("^server\\.(\\d)");
Pattern pValue = Pattern.compile("([\\w|.]*):\\d*:\\d*");
Matcher mKey = pKey.matcher(key);
Matcher mValue = pValue.matcher(props.getProperty(key));
if (mKey.find() && mValue.find()) {
String id = mKey.group(1);
String host = mValue.group(1);
String thisHostName = InetAddress.getLocalHost().getHostName();
String thisHostAddress = InetAddress.getLocalHost().getHostAddress();
if (host.equals(thisHostName) || host.equals(thisHostAddress)) {
//System.out.println(new File(props.getProperty("dataDir"), "myid").getAbsolutePath());
FileUtils.write(new File(props.getProperty("dataDir"), "myid"), id);
QuorumPeerConfig quorumConfig = new QuorumPeerConfig();
quorumConfig.parseProperties(props); final ZooKeeperServerMain zkServer = new ZooKeeperServerMain();
final ServerConfig config = new ServerConfig();
config.readFrom(quorumConfig);
zkServer.runFromConfig(config);
}
}
}
}
}

客户端測试代码例如以下,这里能够改动hostname为集群中的随意一台机器

package my.zookeeperstudy.server;

import org.apache.zookeeper.*;

import java.util.List;

public class Client {
public static void main(String[] args) throws Exception {
ZooKeeper zk = new ZooKeeper("fanbinx1:2181,fanbinx2:2181,fanbinx3:2181", 10000,
new Watcher() {
public void process(WatchedEvent event) {
System.out.println("event: " + event.getType());
}
}); System.out.println(zk.getState()); zk.create("/myApps", "myAppsData".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/myApps/App1", "App1Data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/myApps/App2", "App2Data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/myApps/App3", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.setData("/myApps/App3","App3Data".getBytes(), -1); System.out.println(zk.exists("/myApps", true));
System.out.println(new String(zk.getData("/myApps", true, null))); List<String> children = zk.getChildren("/myApps", true);
for (String child : children) {
System.out.println(new String(zk.getData("/myApps/" + child, true, null)));
zk.delete("/myApps/" + child,-1);
} zk.delete("/myApps",-1); zk.close();
}
}

測试
* 在集群中的各个机器上分别执行ClusteredZKServer类来启动Zookeeper服务。
* 然后在随意一台机器上执行Client类来连接Zookeeper并操作数据。

原文链接:Zookeeper实战嵌入式执行Zookeeper集群模式

Zookeeper实战之嵌入式执行Zookeeper集群模式的相关教程结束。

《Zookeeper实战之嵌入式执行Zookeeper集群模式.doc》

下载本文的Word格式文档,以方便收藏与打印。