Zookeeper實戰之嵌入式運行Zookeeper叢集模式,zookeeper叢集
很多使用Zookeeper的情景是需要我們嵌入Zookeeper作為自己的分布式應用系統的一部分來提供分布式服務,此時我們需要通過程式的方式來啟動Zookeeper。此時可以通過Zookeeper API的ZooKeeperServerMain類來啟動Zookeeper服務。
下面是一個叢集模式下啟動Zookeeper服務的例子
這裡假定我們運行Zookeeper叢集的三台機器名分別為fanbinx1,fanbinx2,fanbinx3
首先是zoo.cfg設定檔
tickTime=2000dataDir=/tmp/zookeeper/dataclientPort=2181initLimit=10syncLimit=5server.1=fanbinx1:2888:3888server.2=fanbinx2:2888:3888server.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叢集模式