1. 程式人生 > >zookeeper基礎及搭建環境

zookeeper基礎及搭建環境

zookeeper是一個分散式協調服務,主要為分散式程式提供協調服務。

  • zookeeper為分散式程式服務那麼自己本身也要高可用,所以zookeeper本身就是分散式程式,只要半數節點存活那麼zookeeper就可以提供正常服務,那麼推薦部署奇數臺機器
  • zookeeper基本提供主從協調、動態感知服務節點上下線,統一配置管理,分散式共享鎖等。這麼多功能總結起來就兩點,管理使用者程式提交過來的資料,並且監聽這些資料變化。

那麼先搭建系統,然後再來具體學習提供的這些功能。

一、準備安裝環境,3臺centos虛擬機器,安裝好jdk,儘量ip固定地址,關閉防火牆

配置環境變數配置檔案/etc/profile最後新增

export JAVA_HOME=路徑

export PATH=$PATH:$JAVA_HOME/bin

esc :wq 儲存

source  /etc/profile 讓修改的檔案生效

修改網絡卡配置檔案/etc/sysconfig/network-scripts

TYPE="Ethernet"
BOOTPROTO="static"
DEFROUTE="yes"
NAME="ens33"
DEVICE="ens33"
PEERROUTES="yes"
IPADDR=192.168.137.131
NTSMASK=255.255.255.0
GATEWAY=192.168.137.2
DNS1=192.168.137.2

 service network restart 儲存後重啟網絡卡或者重啟reboot

centos7防火牆設定(因為這些機器作為內部程式,便於操作關閉防火牆)

啟動: systemctl start firewalld 關閉: systemctl stop firewalld 檢視狀態: systemctl status firewalld  開機禁用  : systemctl disable firewalld 開機啟用  : systemctl enable firewalld

二、配置zookeeper叢集(便於操作我這直接使用root使用者)

解壓下載的zookeeper檔案 zookeeper-3.4.13.tar.gz

tar zxvf -C /root/apps 解壓到apps資料夾下

修改zookeeper的配置檔案:zookeeper-3.4.13/conf  複製一份zoo_sample.cfg        cp zoo_sample.cfg zoo.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=/root/apps/zookeeper-3.4.13/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1 
server.1=192.168.137.129:2888:3888
server.2=192.168.137.130:2888:3888
server.3=192.168.137.131:2888:3888

server.1=192.168.137.129:2888:3888  (主機名:心跳埠:資料埠)

建立資料資料夾/root/apps/zookeeper-3.4.13/data並且在此資料夾下建立檔名為myid 內容為server.1 中的1  根據配置不同機器修改為2,3

啟動zookeeper bin目錄下 執行 zkServer.sh start   檢視狀態 zkService.sh status

[[email protected] apps]# zkServer.sh status             ZooKeeper JMX enabled by default             Using config: /root/apps/zookeeper-3.4.13/bin/../conf/zoo.cfg             Mode: follower