1. 程式人生 > >zookeeper的偽叢集搭建模式

zookeeper的偽叢集搭建模式

所謂偽叢集就是在單機模擬叢集模式zookeeper的執行。

配置檔案

下面是我配置的偽叢集分佈模式,分別通過zoo1.cfg、zoo2.cfg、zoo3.cfg來模擬三臺機器的zookeeper叢集:

zoo1.cfg內容如下:

# The number of milliseconds of each tick
tickTime=2000

# The number of ticks that the initial
# synchronization phase can take
initLimit=10

# The number of ticks that can pass between
# sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/usr/local/zookeeper-3.4.9/data/data_1 dataLogDir=/usr/local/zookeeper-3.4.9/dataLog/dataLog_1 # the port at which the clients will connect
clientPort=2181 server.0=localhost:2287:3387 server.1=localhost:2288:3388 server.2=localhost:2289:3389

zoo2.cfg內容如下

# The number of milliseconds of each tick
tickTime=2000

# The number of ticks that the initial
# synchronization phase can take
initLimit=10

# The number of ticks that can pass between
# sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/usr/local/zookeeper-3.4.9/data/data_2 dataLogDir=/usr/local/zookeeper-3.4.9/dataLog/dataLog_2 # the port at which the clients will connect clientPort=2182 server.0=localhost:2287:3387 server.1=localhost:2288:3388 server.2=localhost:2289:3389

zoo3.cfg內容如下:

# The number of milliseconds of each tick
tickTime=2000

# The number of ticks that the initial
# synchronization phase can take
initLimit=10

# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5

# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper-3.4.9/data/data_3
dataLogDir=/usr/local/zookeeper-3.4.9/dataLog/dataLog_3

# the port at which the clients will connect
clientPort=2183

server.0=localhost:2287:3387
server.1=localhost:2288:3388
server.2=localhost:2289:3389

配置完成上述三個檔案後,還需在dataDir對應路徑下新增myid檔案,內容對應server.n的n值:

 echo 1 > data_1/myid
 echo 2 > data_2/myid
 echo 0 > data_3/myid

啟動

在叢集為分散式下,我們只有一臺機器,但是要執行三個Zookeeper例項。此時,如果在使用單機模式的啟動命令是行不通的。此時,只要通過下面三條命令就能執行前面所配置的Zookeeper服務。如下所示:

./zkServer.sh start zoo1.cfg
./zkServer.sh start zoo2.cfg
./zkServer.sh start zoo3.cfg

在執行完第一條指令之後,會出現一些錯誤異常,產生異常資訊的原因是由於Zookeeper 服務的每個例項都擁有全域性配置資訊,他們在啟動的時候會隨時隨地的進行Leader選舉操作。此時,第一個啟動的Zookeeper需要和另外兩個 Zookeeper例項進行通訊。但是,另外兩個Zookeeper例項還沒有啟動起來,因此就產生了這的異樣資訊。我們直接將其忽略即可,待把圖中“2 號”和“3號”Zookeeper例項啟動起來之後,相應的異常資訊自然會消失。此時,可以通過下面三條命令,來查詢:

./zkServer.sh status zoo1.cfg
./zkServer.sh status zoo2.cfg
./zkServer.sh status zoo3.cfg

結果如下:
這裡寫圖片描述