ZooKeeper是一個分佈式的,開源的分佈式應用程序協調服務,是Hadoop的子項目之一。它是一個爲分佈式應用提供一致性服務的軟件,提供的功能包括:配置維護、域名服務、分佈式同步、組服務等。html
操做系統 | 客戶端 | 服務端 | 原生客戶端 | 附加組件 |
---|---|---|---|---|
GNU/Linux | 開發/生產 | 開發/生產 | 開發/生產 | 開發/生產 |
Solaris | 開發/生產 | 開發/生產 | 不支持 | 不支持 |
FreeBSD | 開發/生產 | 開發/生產 | 不支持 | 不支持 |
Windows | 開發/生產 | 開發/生產 | 不支持 | 不支持 |
Mac OS X | 開發 | 開發 | 不支持 | 不支持 |
Java 8及Java 11以上版本(Java 9和10不支持)java
此硬件資源爲官網推薦的配置,實際開發過程當中不須要這麼大,筆者測試1核1G內存20G硬盤的虛擬機便可運行。linux
apache-zookeeper-3.6.1/conf/zoo_sample.cfg
複製一份並重命名爲zoo.cfg
,沒什麼特殊須要裏邊的配置項默認便可,筆者由於是在windows下使用,因此將datadir修改了。配置文件項說明以下:配置項 | 說明 |
---|---|
tickTime |
ZooKeeper使用的時間,單位毫秒,通常用於心跳檢測,而ZooKeeper中的最小session超時時間是此項的兩倍 |
dataDir |
保留內存數據庫快照的地址,若是不單獨指定,事務日誌也會記錄在此 |
clientPort |
服務端監聽的端口號 |
initLimit |
集羣中的follower服務器與leader服務器之間初始鏈接時的最大心跳數 |
syncLimit |
集羣中follower服務器與leader服務器之間通信時的最大心跳數 |
zkServer.bat
,Linux下爲zkServer.sh
。bin
目錄下自帶的客戶端進行訪問,Windows下爲zkCli.bat
,Linux下爲zkCli.sh
。localhost:2181
,若是有須要鏈接遠程或其餘端口的狀況,能夠以下添加參數:zkCli.sh -server IP:Port
進入客戶端後執行help
(此處是一個隨意的指令,只要不是zkCli支持的操做均可以)可查看其支持的操做,關於全部操做的介紹請參考官方頁面:https://zookeeper.apache.org/doc/current/zookeeperCLI.htmlgit
經常使用操做介紹:github
ls / ls /zookeeper
create /test create /test/testa
stat /test stat /test/testa
# 刪除單個空節點 delete /test/testa delete /test # 級聯刪除 deleteall /test
*退出客戶端數據庫
quit
由於筆者的第一開發語言是Java,這裏以Java爲例。經常使用的ZooKeeper Java客戶端用zkclient和Apache Curator兩種。zkclient是github上的一個開源項目,該項目在2018年10月2往後中止更新;Apache Curator是Apache基金會的開源項目,目前持續更新,推薦使用。經常使用的分佈式RPC框架DUBBO也在2019年1月份推出的2.7.0版本中將默認的ZooKeeper客戶端由zkclient切換爲Apache Curator,此文中的示例也使用Apache Curator。apache
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>apache-curator</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
src\test\java\
目錄建立com\aotian\curator\test\Tester.java
,文件基本框架以下,主要是建立一個空的測試類public class Tester { @Test public void testCurator() { } }
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy); curatorFramework.start();
Stat
對象,不存在則返回null
curatorFramework.checkExists().forPath("/localhost/aotian");
forPath
第二個參數能夠指定節點內容,不設置時建立空節點curatorFramework.create().creatingParentContainersIfNeeded().forPath("/localhost/aotian", message.getBytes());
curatorFramework.setData().forPath("/localhost/aotian", message.getBytes());
result
對象。Stat result = new Stat(); curatorFramework.getData().storingStatIn(result).forPath("/localhost/aotian");
byte[] results = curatorFramework.getData().forPath("/localhost/aotian");
zkCli
查看服務端中的內容。@Test public void testCurator() { // 建立客戶端 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy); curatorFramework.start(); // 定義節點內容 String message = "testCurator"; try { // 判斷節點是否存在不存在則建立,存在則設置指定值 Stat a = curatorFramework.checkExists().forPath("/localhost/aotian"); if (a == null){ curatorFramework.create() .creatingParentContainersIfNeeded() .forPath("/localhost/aotian", message.getBytes()); }else{ curatorFramework.setData().forPath("/localhost/aotian", message.getBytes()); } // 獲取節點信息 Stat result = new Stat(); curatorFramework.getData().storingStatIn(result).forPath("/localhost/aotian"); System.out.println(result.getCtime()); // 獲取節點內容 byte[] results = curatorFramework.getData().forPath("/localhost/aoitan"); System.out.println(new String(results)); // 線程睡10S,這段時間內能夠經過客戶端查看節點內的信息,結束後只能查看到空節點 Thread.sleep(100000); } catch (Exception e) { e.printStackTrace(); }finally { curatorFramework.close(); } }
ZooKeeper集羣中包含兩種角色:Leader和Follower,由於ZooKeeper集羣是半數節以上節點正常時纔會正常提供服務,因此通常ZooKeeper集羣中節點數量均爲奇數。咱們按照最小數量算,準備三臺zookeeper服務器。windows
datadir
屬性對應的目錄下建立一個myid
文件。而後在文件內寫上當前服務對應的ID,筆者規劃的是0、一、2,因此我須要添加的配置文件以下:IP地址 | 文件路徑 | 文件內容 |
---|---|---|
192.168.142.7 | /tmp/zookeeper/myid | 0 |
192.168.142.8 | /tmp/zookeeper/myid | 1 |
192.168.142.9 | /tmp/zookeeper/myid | 2 |
datadir
屬性默認在/tmp
目錄下,此目錄會被按期清理掉,生產環境不要使用。centos
三、配置完以上文件後,須要配置以前的zoo.cfg
,在最後添加如下內容,其中server.*
對應myid
文件中的ID號,192.168.142.7
是IP地址,2888
是ZooKeeper集羣的通信端口,3888
是集羣選取Leader使用的端口。服務器
server.0=192.168.142.7:2888:3888 server.1=192.168.142.8:2888:3888 server.2=192.168.142.9:2888:3888
四、最後檢查防火牆是否開放了218一、288八、3888端口,確認開放後啓動ZooKeeper便可。經過執行zkServer.sh status
命令能夠查看當前機器的狀態。
[root@centos-server-01 bin]# ./zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/apache-zookeeper-3.6.0/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: follower [root@centos-server-02 bin]# ./zkServer.sh status /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/apache-zookeeper-3.6.0/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: leader