本文目錄一覽:
zookeeper 集群 部署好後怎麼使用 java
很多使用Zookeeper的情景是需要我們嵌入Zookeeper作為自己的分散式應用系統的一部分來提供分散式服務,此時我們需要通過程序的方式來啟動Zookeeper。此時可以通過Zookeeper API的ZooKeeperServerMain類來啟動Zookeeper服務。
下面是一個集群模式下啟動Zookeeper服務的例子
這裡假定我們運行Zookeeper集群的三台機器名分別為fanbinx1,fanbinx2,fanbinx3
首先是zoo.cfg配置文件
[plain] view plain copy print?
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服務。
[java] view plain copy print?
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為集群中的任意一台機器
[java] view plain copy print?
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)));
ListString 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怎麼用java創建臨時節點
基本操作
下面給出基本的操作 ZooKeeper 的示例代碼,這樣你就能對 ZooKeeper 有直觀的認識了。下面的清單包括了創建與 ZooKeeper 伺服器的連接以及最基本的數據操作:
ZooKeeper 基本的操作示例
// 創建一個與伺服器的連接
ZooKeeper zk = new ZooKeeper(“localhost:” + CLIENT_PORT,
ClientBase.CONNECTION_TIMEOUT, new Watcher() {
// 監控所有被觸發的事件
public void process(WatchedEvent event) {
System.out.println(“已經觸發了” + event.getType() + “事件!”);
}
});
// 創建一個目錄節點
zk.create(“/testRootPath”, “testRootData”.getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
// 創建一個子目錄節點
zk.create(“/testRootPath/testChildPathOne”, “testChildDataOne”.getBytes(),
Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
System.out.println(new String(zk.getData(“/testRootPath”,false,null)));
// 取出子目錄節點列表
System.out.println(zk.getChildren(“/testRootPath”,true));
// 修改子目錄節點數據
zk.setData(“/testRootPath/testChildPathOne”,”modifyChildDataOne”.getBytes(),-1);
System.out.println(“目錄節點狀態:[“+zk.exists(“/testRootPath”,true)+”]”);
// 創建另外一個子目錄節點
zk.create(“/testRootPath/testChildPathTwo”, “testChildDataTwo”.getBytes(),
Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
System.out.println(new String(zk.getData(“/testRootPath/testChildPathTwo”,true,null)));
// 刪除子目錄節點
zk.delete(“/testRootPath/testChildPathTwo”,-1);
zk.delete(“/testRootPath/testChildPathOne”,-1);
// 刪除父目錄節點
zk.delete(“/testRootPath”,-1);
// 關閉連接
zk.close();
輸出的結果如下:
已經觸發了 None 事件!
testRootData
[testChildPathOne]
目錄節點狀態:[5,5,,,0,1,0,0,12,1,6]
已經觸發了 NodeChildrenChanged 事件!
testChildDataTwo
已經觸發了 NodeDeleted 事件!
已經觸發了 NodeDeleted 事件!
當對目錄節點監控狀態打開時,一旦目錄節點的狀態發生變化,Watcher 對象的 process 方法就會被調用。
安裝zookeeper需要先裝java嗎
ZooKeeper是用Java編寫的,運行在Java環境上,因此,在部署zk的機器上需要安裝Java運行環境。為了正常運行zk,我們需要JRE1.6或者以上的版本。
對於集群模式下的ZooKeeper部署,3個ZooKeeper服務進程是建議的最小進程數量,而且不同的服務進程建議部署在不同的物理機器上面,以減少機器宕機帶來的風險,以實現ZooKeeper集群的高可用。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/204350.html