1. 程式人生 > >zookeeper-3.4.8單機與主從安裝與配置

zookeeper-3.4.8單機與主從安裝與配置

ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them ,which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed.

Reference Link:  http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html

下載:

從官網下載最新的最穩定版本http://www.apache.org/dyn/closer.cgi/zookeeper/

在Linux下載的命令:wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz

規劃目錄:

計劃要安裝主從,但先從單機開始,目錄要規劃好,可以在同一臺機上部署三臺,目錄規劃如下:

/usr/local/redis下zoo1,zoo2,zoo3

單機部署

在zoo1下解壓下載的壓縮包,並建立data,datalog,logs資料夾。

配置檔案cd /usr/local/redis/zoo1/zookeeper-3.4.8/conf

cp zoo_sample.cfg  zoo.cfg

vi zoo.cfg新增或修改data資料夾目錄

dataDir=/usr/local/redis/zoo1/data
dataLogDir=/usr/local/redis/zoo1/datalog

啟動zookeeper

zookeeper-3.4.8下執行命令,啟動zookeeper

[[email protected] /usr/local/redis/zoo1/zookeeper-3.4.8]#bin/zkServer.sh start


ZooKeeper JMX enabled by default
Using config: /usr/local/redis/zoo1/zookeeper-3.4.8/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

使用命令客戶端連線至zookeeper

[[email protected] /usr/local/redis/zoo1/zookeeper-3.4.8]#bin/zkCli.sh -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181

[zk: 127.0.0.1:2181(CONNECTING) 0] 2016-03-29 11:53:08,156 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):[email protected]] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x153c07ff35f0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
看到這個就已連線上了,回車後出現下面的命令提示行:

[zk: 127.0.0.1:2181(CONNECTED) 0]

可以執行一些命令進行測試檢視

[zk: 127.0.0.1:2181(CONNECTED) 0]help

[zk: 127.0.0.1:2181(CONNECTED) 1]ls /
[zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 2] create /zk_test my_data
Created /zk_test
[zk: 127.0.0.1:2181(CONNECTED) 3] ls /                   
[zookeeper, zk_test]
[zk: 127.0.0.1:2181(CONNECTED) 4] get /zk_test
my_data
cZxid = 0x2
ctime = Tue Mar 29 11:55:03 CST 2016
mZxid = 0x2
mtime = Tue Mar 29 11:55:03 CST 2016
pZxid = 0x2
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 5] set /zk_test junk
cZxid = 0x2
ctime = Tue Mar 29 11:55:03 CST 2016
mZxid = 0x3
mtime = Tue Mar 29 11:55:39 CST 2016
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 6] get /zk_test     
junk
cZxid = 0x2
ctime = Tue Mar 29 11:55:03 CST 2016
mZxid = 0x3
mtime = Tue Mar 29 11:55:39 CST 2016
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 7] delete /zk_test
[zk: 127.0.0.1:2181(CONNECTED) 8] get /zk_test   
Node does not exist: /zk_test
[zk: 127.0.0.1:2181(CONNECTED) 9] ls /
[zookeeper]

單機zookeeper就安裝並啟動好了。

Running Replicated ZooKeeper

Running ZooKeeper in standalone mode is convenient for evaluation, some development, and testing. But in production, you should run ZooKeeper in replicated mode. A replicated group of servers in the same application is called a quorum, and in replicated mode, all servers in the quorum have copies of the same configuration file.

For replicated mode, a minimum of three servers are required, and it is strongly recommended that you have an odd number of servers. If you only have two servers, then you are in a situation where if one of them fails, there are not enough machines to form a majority quorum. Two servers is inherently less stable than a single server, because there are two single points of failure.

其他兩臺,按照單機那臺先部署。

進入data目錄,建立一個myid的檔案,裡面寫入一個數字,比如我這個是zoo1,那麼就寫一個1,zoo2對應myid檔案就寫入2,zoo3對應myid檔案就寫個3。

然後修改配置檔案,The required conf/zoo.cfg file for replicated mode is similar to the one used in standalone mode, but with a few differences. Here is an example:

tickTime=2000
initLimit=5
syncLimit=2
dataDir=/usr/local/redis/zoo1/data       #zoo1 zoo2 zoo3分別三臺的目錄
clientPort=2183                          #2181 2182 2183 三臺的埠
dataLogDir=/usr/local/redis/zoo1/datalog #zoo1 zoo2 zoo3分別三臺的目錄
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890

最後幾行需要注意的地方就是 server.X 這個數字就是對應 data/myid中的數字。你在3個server的myid檔案中分別寫入了1,2,3,那麼每個server中的zoo.cfg都配server.1,server.2,server.3就OK了。因為在同一臺機器上,後面連著的2個埠3個server都不要一樣,否則埠衝突,其中第一個埠用來叢集成員的資訊交換,第二個埠是在leader掛掉時專門用來進行選舉leader所用。

進入zookeeper/bin目錄中,./zkServer.sh start啟動一個server,這時會報大量錯誤?其實沒什麼關係,因為現在叢集只起了1臺server,zookeeper伺服器端起來會根據zoo.cfg的伺服器列表發起選舉leader的請求,因為連不上其他機器而報錯,那麼當我們起第二個zookeeper例項後,leader將會被選出,從而一致性服務開始可以使用,這是因為3臺機器只要有2臺可用就可以選出leader並且對外提供服務(2n+1臺機器,可以容n臺機器掛掉)。

接下來就可以使用了,我們可以先通過 zookeeper自帶的客戶端互動程式來簡單感受下zookeeper到底做一些什麼事情。進入zookeeper/bin(3個server中任意一個)下,./zkCli.sh –server 127.0.0.1:2182,我連的是開著2182埠的機器。輸入help命令,檢視結果:

help

ls(檢視當前節點資料),
ls2(檢視當前節點資料並能看到更新次數等資料) ,
create(建立一個節點) ,
get(得到一個節點,包含資料和更新次數等資料),
set(修改節點)
delete(刪除一個節點)
通過上述命令實踐,我們可以發現,zookeeper使用了一個類似檔案系統的樹結構,資料可以掛在某個節點上,可以對這個節點進行刪改。另外我們還發現,當改動一個節點的時候,叢集中活著的機器都會更新到一致的資料。 

通過java程式碼使用zookeeper 

Zookeeper的使用主要是通過建立其jar包下的Zookeeper例項,並且呼叫其介面方法進行的,主要的操作就是對znode的增刪改操作,監聽znode的變化以及處理。

package com.jh.sms.test;

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

public class ZookeeperTest {

	public static void zooTest()throws Exception{
		//建立一個Zookeeper例項,第一個引數為目標伺服器地址和埠,第二個引數為Session超時時間,第三個為節點變化時的回撥方法
		ZooKeeper zk = new ZooKeeper("192.168.0.149:2181", 500000,new Watcher() {
		           // 監控所有被觸發的事件
		             public void process(WatchedEvent event) {
		           //dosomething
		            	 System.out.println(event.getState().name()+" int: "+event.getState().getIntValue());
		           }
		 });
		//建立一個節點root,資料是mydata,不進行ACL許可權控制,節點為永久性的(即客戶端shutdown了也不會消失)
		zk.create("/root", "mydata".getBytes(),Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

		//在root下面建立一個childone znode,資料為childone,不進行ACL許可權控制,節點為永久性的
		zk.create("/root/childone","childone".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

		//取得/root節點下的子節點名稱,返回List<String>
		zk.getChildren("/root",true);

		//取得/root/childone節點下的資料,返回byte[]
		zk.getData("/root/childone", true, null);

		//修改節點/root/childone下的資料,第三個引數為版本,如果是-1,那會無視被修改的資料版本,直接改掉
		zk.setData("/root/childone","childonemodify".getBytes(), -1);

		//刪除/root/childone這個節點,第二個引數為版本,-1的話直接刪除,無視版本
		zk.delete("/root/childone", -1);		      
		//關閉session
		zk.close();
	}
	
	public static void main(String[] args)throws Exception {
		ZookeeperTest.zooTest();
	}
}
執行後,可以在zookeeper叢集上看到相應的資料:

[zk: 127.0.0.1:2182(CONNECTED) 1] ls /
[root, zookeeper, zk_test]
[zk: 127.0.0.1:2182(CONNECTED) 2] get /root
mydata

相關推薦

zookeeper-3.4.8單機主從安裝配置

ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providin

Linux下圖示安裝Zookeeper-3.4.12(單機版)

一:Zookeeper下載(版本是3.4.12)。        Zookeeper是JAVA開發的需要安裝JDK,這個就不說了。       1.  官網介紹。      

Zabbix 3.4.10 服務端的安裝設定

  實驗驗目的:  Zabbix 3.x 服務端的安裝 實驗主機:  m01  (centos 7.4)  IP 10.0.0.61/172.16.1.61 1) 配置yum源, 並用wget命令把相關的軟體包下載到本地, 然後再進行安裝, 如下所示

Zookeeper-3.4.8叢集搭建

老專案中的客戶端使用了ZK的3.4.8版本,所以服務端依舊使用該版本進行搭建。選擇三臺伺服器搭建叢集:172.16.10.70,172.16.10.71,172.16.10.72。根據zk的選舉原理,叢集選擇奇數個節點,保證其高可用性(原理不在此處標明)。1、增加主機名解析(

安裝 Dubbo 註冊中心(Zookeeper-3.4.6)

store port -s 端口 作者 tin 內容 send 輸出信息 安裝 Dubbo 註冊中心(Zookeeper-3.4.6) Dubbo 建議使用 Zookeeper 作為服務的註冊中心。 註冊中心服務器(192.168.3.71)配置,安裝 Zookeep

linux安裝配置zookeeper-3.4.10

mirror http ima mkdir ech cli comm 利用 .com 安裝zookeeper: 下載地址:http://mirror.bit.edu.cn/apache/zookeeper/ zk節點最好是奇數個,這樣子方便在節點中選取leader節點; 上

在 CentOS7 上安裝 Zookeeper-3.4.9 服務

sin cal 永久 int mit apache small www cti 在 CentOS7 上安裝 zookeeper-3.4.9 服務 1、創建 /usr/local/services/zookeeper 文件夾: mkdir -p /usr/local

Zookeeper 3.4.10 安裝部署

部署 zkserver dir zookeeper nbsp pos start 地址 class 安裝Zookeeper cd /opt/bigdata tar -zvxf zookeeper-3.4.10.tar.gz vi /etc/profile 添加: #zook

CentOS6.4安裝Zookeeper-3.4.12圖解教程

attr dcb 同時 圖片 遠程 iso root用戶 網絡 系統盤 安裝工具 VMware_workstation_full_12.5.2 CentOS6-Base-163 CentOS-6.4-x86_64-bin-DVD1.iso VM運行配置 雙擊運行VM 運行

Linux CentOS7下安裝Zookeeper-3.4.10服務(最新)

pre exp 路徑 datadir detail count 3.4 repr 數據文件 Linux CentOS7下安裝Zookeeper-3.4.10服務(最新) 2017年10月27日 01:25:26 極速-蝸牛 閱讀數:1933 版權聲明:

zookeeper-3.4.10 安裝配置

  準備安裝包     zookeeper-3.4.10(連結:http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.10/)     解壓 tar -zxvf zookeeper-3.4.10        配置     進入zooke

分散式ZooKeeper-3.4.10叢集安裝

ZooKeeper是一個為分散式應用所設計的開源協調服務,其設計目的是為了減輕分散式應用程式所承擔的協調任務。它可以為使用者提供同步、配置管理、分組和命名等服務。 jstorm需要zk配合使用,記錄安裝文件。 一、基礎環境: JDK    :1.8.0_65 (要求

Ubuntu 16.04.1 LTS zookeeper-3.4.11安裝啟動失敗Error contacting service. It is probably not running.

騰訊雲伺服器Ubuntu 16.04.1 LTS zookeeper-3.4.11安裝啟動失敗 zookeeper安裝後啟動報許可權不足: [email protected]:/opt/zookeeper-3.4.8/bin$ ./zkServer.sh start ZooKeep

CentOS7下安裝Zookeeper-3.4.12

一、下載Zookeeper(需要jdk的支援) 下載好之後上傳至Linux系統中,如下: 二、解壓 tar -zxvf zookeeper-3.4.12.tar.gz 三、在解壓後的資料夾中,建立data目錄 解壓後的資料夾如下:  建立data目錄:

CentOS 7不使用RPM安裝RabbitMQ 3.7.8單機

RabbitMQ是目前非常熱門的訊息中介軟體,憑藉其高可靠、高擴充套件、高可用及豐富的功能特性:TTL、死信佇列、延遲佇列、優先順序佇列、訊息持久化、映象佇列。 訊息中介軟體是指利用高效可靠的訊息傳遞機制進行與平臺無關的資料交流,並基於資料通訊來進行分散式系統的整合,能在不同平臺之間進行通訊,常被用來遮蔽各

zookeeper-3.4.9叢集安裝

下載 Centos7 Jdk-1.7 or 1.8 zookeeper3.4.9   主機 cancer03、cancer04、cancer05   上傳zookeeper-3.4.9.tar.gz到cancer03、cancer04、canc

Redis單機多實體安裝主從配置

上一篇文章講解了Centos上的redis安裝。 現在我們來說一下redis單機多例項的安裝 首先關閉redis 根據上一篇redis的安裝的配置檔案位置 首先我們複製redis 的配置檔案 cp/etc/redis/6379.conf /etc/redis

Linux-7.2 下 Solr4.10.4 單機模式的安裝部署圖文詳解

《 Linux下Solr4.10.4搜尋引擎的安裝與部署 》 瞭解Solr: Solr是來自Apache Lucene專案的流行的,快速的,開源的NoSQL搜尋平臺。它的主要功能包括強大

ZooKeeper-3.4.6叢集安裝配置

ZooKeeper是一個分散式開源框架,提供了協調分散式應用的基本服務,它向外部應用暴露一組通用服務——分散式同步(Distributed Synchronization)、命名服務(Naming Service)、叢集維護(Group Maintenance)等,簡化分

[Hadoop] CentOS7安裝Zookeeper-3.4.5-cdh5.7.0(單節點)

  1. Zookeeper下載安裝 我的環境使用的套件版本是cdh5.7,所以zookeeper也選擇對應的版本下載。 下載解壓 [[email protected] software]$ wget http://archive.cloudera.com/c