MiddleWare ❀ Zookeeper基础概述
Posted 无糖可乐没有灵魂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MiddleWare ❀ Zookeeper基础概述相关的知识,希望对你有一定的参考价值。
文章目录
1、认识Zookeeper
Zookeeper的数据结构类似于unix文件系统,等同于一棵树,每个节点称之为Znode,每个节点可以通过路径来进行标识,结构如下:
1.1 数据类型
节点数据类型主要分为两类:
- 短暂/临时 Ephemeral:当客户端和服务端端开连接后,所创建的Znode节点会自动删除;
- 持久 Persistent:当客户端和服务端端开连接后,所创建的Znode节点不会自动删除;
Zookeeper与Redis一样,分为客户端与服务端,典型的C/S结构。
1.2 监听器
Zookeeper配合了监听器,常见的监听器有以下两种:
- 监听Znode的数据变化;
- 监听子节点的增减变化。
1.3 基本功能
Zookeeper主要服务于分布式系统,可以用Zookeeper来做:统一配置管理、统一命名服务、分布式锁、集群管理;
1.3.1 统一配置管理
假设当前环境下有三个系统A、B、C,有三个配置,分别是A_system.yml、B_system.yml、C_system.yml,这三份配置非常类似,很多配置基本保持一致。
此时若是修改了其中一份配置项信息,另外两份也需要被破修改,改变了配置项的信息可能需要重启系统系统A、B、C来应用此配置。因此把A_system.yml、B_system.yml、C_system.yml相同的配置抽取成一份公用的配置common.yml,并且common.yml进行修改,不需要重启系统系统A、B、C;
可以将common.yml配置放在Zookeeper的Znode节点中,系统A、B、C监听这个Znode节点是否发生变更即可。
1.3.2 统一命名服务
统一命名服务的理解与域名相同,为某个部分资源取一个类似的名称,通过这个名称寻找对应的资源。
1.3.3 分布式锁
假设系统A、B、C都访问/locks节点,访问的时候会创建带顺序序号的临时节点,如:系统A创建了id_00000的节点,系统B创建了id_00002的节点,系统C创建了id_00001的节点,接着拿到/locks节点下的所有子节点(id_00000、id_00001、id_00002)判断自己创建的是不是最小的节点,如果是则拿到锁,当系统A执行完操作后,端开连接会删除临时节点id_00000,因此id_00001会成为最小的节点,系统C拿到锁,进行相关操作,直到系统B拿到锁执行完端开连接。
1.3.4 集群状态
Zookeeper Leader选举参数:
- server-id:服务器id,编号为1、2、3…,编号越大在选择算法中权重越大;
- Zxid:数据id,服务器中存放的最大数据id,数值越大说明数据越新,在选举算法中越新权重越大;
在选举中,若某台Zookeeper获得超过半数的选票,则被选为Leader,Leader一旦确认,非异常不可更改。以系统A、B、C为例,在Zookeeper创建临时节点,系统A挂了,则临时节点被删除,通过监听器可以感知到临时节点的子节点是否存在判断系统A的存活状态。
2、下载安装包
下载地址:https://zookeeper.apache.org/
,下载最新版本即可,非必须要求版本一致性。(注意:下载apache-zookeeper-3.8.0-bin.tar.gz,而不是apache-zookeeper-3.8.0.tar.gz)
3、安装服务
下载完成后,将安装包传入Linux后台,并在对应目录下解压安装包
[root@centos7 download]# tar -zxvf apache-zookeeper-3.8.0-bin.tar.gz
修改目录名称
[root@centos7 download]# mv apache-zookeeper-3.8.0-bin zookeeper
[root@centos7 zookeeper]# ll
total 36
drwxr-xr-x. 2 admin ftp 4096 Feb 25 2022 bin # 可执行文件目录
drwxr-xr-x. 2 admin ftp 72 Feb 25 2022 conf # 配置文件目录
drwxr-xr-x. 5 admin ftp 4096 Feb 25 2022 docs
drwxr-xr-x. 2 root root 4096 Aug 30 02:43 lib
-rw-r--r--. 1 admin ftp 11358 Feb 25 2022 LICENSE.txt
-rw-r--r--. 1 admin ftp 2084 Feb 25 2022 NOTICE.txt
-rw-r--r--. 1 admin ftp 2335 Feb 25 2022 README.md
-rw-r--r--. 1 admin ftp 3570 Feb 25 2022 README_packaging.md
配置文件参数介绍
[root@centos7 zookeeper]# cd conf/
[root@centos7 conf]# ll
total 16
-rw-r--r--. 1 admin ftp 535 Feb 25 2022 configuration.xsl
-rw-r--r--. 1 admin ftp 4559 Feb 25 2022 logback.xml
-rw-r--r--. 1 admin ftp 1183 Feb 25 2022 zoo_sample.cfg
[root@centos7 conf]# cat zoo_sample.cfg
# The number of milliseconds of each tick
tickTime=2000 # Leader与Follower之间的心跳时间间隔,单位ms
# The number of ticks that the initial
# synchronization phase can take
initLimit=10 # 最大心跳次数,10*2000ms=20s
# 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=/tmp/zookeeper # 数据存储路径,默认为临时目录,建议修改
# the port at which the clients will connect
clientPort=2181 # 客户端连接端口,默认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.
#
# https://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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpHost=0.0.0.0
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
复制配置文件并进行修改
[root@centos7 zookeeper]# cd conf/
[root@centos7 conf]# ll
total 16
-rw-r--r--. 1 admin ftp 535 Feb 25 2022 configuration.xsl
-rw-r--r--. 1 admin ftp 4559 Feb 25 2022 logback.xml
-rw-r--r--. 1 admin ftp 1183 Feb 25 2022 zoo_sample.cfg # 配置文件
[root@centos7 conf]# cp zoo_sample.cfg zoo.cfg
[root@centos7 conf]# vim zoo.cfg
# 修改数据文件路径为自定义路径
dataDir=/root/download/zookeeper/data/tmp
# 切记需要创建对应目录路径
[root@centos7 conf]# cd ../
[root@centos7 zookeeper]# mkdir ./data
问题解答:Zookeeper为什么必须要复制配置文件为zoo.cfg
# 查看zkServer.sh启动服务脚本,发现调用zkEnv.sh
[root@centos7 conf]# cat ../bin/zkServer.sh | grep 'zkEnv.sh' -C 3
ZOOBIN="$(dirname "$ZOOBIN")"
ZOOBINDIR="$(cd "$ZOOBIN"; pwd)"
if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then
. "$ZOOBINDIR"/../libexec/zkEnv.sh
else
. "$ZOOBINDIR"/zkEnv.sh
fi
# See the following page for extensive details on setting
# 查看zkEnv.sh脚本发现启动配置文件名称为zoo.cfg
[root@centos7 conf]# cat ../bin/zkEnv.sh | grep 'zoo.cfg' -C 3
if [ "x$ZOOCFG" = "x" ]
then
ZOOCFG="zoo.cfg"
fi
ZOOCFG="$ZOOCFGDIR/$ZOOCFG"
# 可以得到zkServer.sh最终的调用配置文件名称为zoo.cfg,因此必须进行复制配置文件操作或修改zkEnv.sh内的脚本名称(不建议后者)。
启动服务
[root@centos7 conf]# cd ../bin/
[root@centos7 bin]# ll
total 72
-rw-r--r--. 1 admin ftp 232 Feb 25 2022 README.txt
-rwxr-xr-x. 1 admin ftp 1978 Feb 25 2022 zkCleanup.sh
-rw-r--r--. 1 admin ftp 1115 Feb 25 2022 zkCli.cmd
-rwxr-xr-x. 1 admin ftp 1576 Feb 25 2022 zkCli.sh # 客户端脚本
-rw-r--r--. 1 admin ftp 1810 Feb 25 2022 zkEnv.cmd
-rwxr-xr-x. 1 admin ftp 3613 Feb 25 2022 zkEnv.sh
-rw-r--r--. 1 admin ftp 1243 Feb 25 2022 zkServer.cmd
-rwxr-xr-x. 1 admin ftp 4559 Feb 25 2022 zkServer-initialize.sh
-rwxr-xr-x. 1 admin ftp 11616 Feb 25 2022 zkServer.sh # 服务端脚本
-rwxr-xr-x. 1 admin ftp 987 Feb 25 2022 zkSnapshotComparer.cmd
-rwxr-xr-x. 1 admin ftp 1374 Feb 25 2022 zkSnapshotComparer.sh
-rwxr-xr-x. 1 admin ftp 988 Feb 25 2022 zkSnapShotToolkit.cmd
-rwxr-xr-x. 1 admin ftp 1377 Feb 25 2022 zkSnapShotToolkit.sh
-rwxr-xr-x. 1 admin ftp 996 Feb 25 2022 zkTxnLogToolkit.cmd
-rwxr-xr-x. 1 admin ftp 1385 Feb 25 2022 zkTxnLogToolkit.sh
[root@centos7 bin]# ./zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
3.1 服务端命令
# 查看/停止/重启 服务状态
[root@centos7 bin]# ./zkServer.sh status/stop/restart
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Error contacting service. It is probably not running.
# 服务端口
[root@centos7 bin]# netstat -anop | grep 2181
tcp6 0 0 :::2181 :::* LISTEN 4700/java off (0.00/0/0)
3.2 客户端命令
# 连接命令
[root@centos7 bin]# ./zkCli.sh -server localhost:2181
/usr/bin/java
Connecting to localhost:2181
2022-08-30 02:50:49,009 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:zookeeper.version=3.8.0-5a02a05eddb59aee6ac762f7ea82e92a68eb9c0f, built on 2022-02-25 08:49 UTC
2022-08-30 02:50:49,013 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:host.name=centos7.3-1
2022-08-30 02:50:49,013 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:java.version=1.8.0_102
2022-08-30 02:50:49,013 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:java.vendor=Oracle Corporation
2022-08-30 02:50:49,013 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64/jre
2022-08-30 02:50:49,013 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:java.class.path=/root/download/zookeeper/bin/../zookeeper-server/target/classes:/root/download/zookeeper/bin/../build/classes:/root/download/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/root/download/zookeeper/bin/../build/lib/*.jar:/root/download/zookeeper/bin/../lib/zookeeper-prometheus-metrics-3.8.0.jar:/root/download/zookeeper/bin/../lib/zookeeper-jute-3.8.0.jar:/root/download/zookeeper/bin/../lib/zookeeper-3.8.0.jar:/root/download/zookeeper/bin/../lib/snappy-java-1.1.7.7.jar:/root/download/zookeeper/bin/../lib/slf4j-api-1.7.30.jar:/root/download/zookeeper/bin/../lib/simpleclient_servlet-0.9.0.jar:/root/download/zookeeper/bin/../lib/simpleclient_hotspot-0.9.0.jar:/root/download/zookeeper/bin/../lib/simpleclient_common-0.9.0.jar:/root/download/zookeeper/bin/../lib/simpleclient-0.9.0.jar:/root/download/zookeeper/bin/../lib/netty-transport-native-unix-common-4.1.73.Final.jar:/root/download/zookeeper/bin/../lib/netty-transport-native-epoll-4.1.73.Final.jar:/root/download/zookeeper/bin/../lib/netty-transport-classes-epoll-4.1.73.Final.jar:/root/download/zookeeper/bin/../lib/netty-transport-4.1.73.Final.jar:/root/download/zookeeper/bin/../lib/netty-tcnative-classes-2.0.48.Final.jar:/root/download/zookeeper/bin/../lib/netty-tcnative-2.0.48.Final.jar:/root/download/zookeeper/bin/../lib/netty-resolver-4.1.73.Final.jar:/root/download/zookeeper/bin/../lib/netty-handler-4.1.73.Final.jar:/root/download/zookeeper/bin/../lib/netty-common-4.1.73.Final.jar:/root/download/zookeeper/bin/../lib/netty-codec-4.1.73.Final.jar:/root/download/zookeeper/bin/../lib/netty-buffer-4.1.73.Final.jar:/root/download/zookeeper/bin/../lib/metrics-core-4.1.12.1.jar:/root/download/zookeeper/bin/../lib/logback-core-1.2.10.jar:/root/download/zookeeper/bin/../lib/logback-classic-1.2.10.jar:/root/download/zookeeper/bin/../lib/jline-2.14.6.jar:/root/download/zookeeper/bin/../lib/jetty-util-ajax-9.4.43.v20210629.jar:/root/download/zookeeper/bin/../lib/jetty-util-9.4.43.v20210629.jar:/root/download/zookeeper/bin/../lib/jetty-servlet-9.4.43.v20210629.jar:/root/download/zookeeper/bin/../lib/jetty-server-9.4.43.v20210629.jar:/root/download/zookeeper/bin/../lib/jetty-security-9.4.43.v20210629.jar:/root/download/zookeeper/bin/../lib/jetty-io-9.4.43.v20210629.jar:/root/download/zookeeper/bin/../lib/jetty-http-9.4.43.v20210629.jar:/root/download/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/root/download/zookeeper/bin/../lib/jackson-databind-2.13.1.jar:/root/download/zookeeper/bin/../lib/jackson-core-2.13.1.jar:/root/download/zookeeper/bin/../lib/jackson-annotations-2.13.1.jar:/root/download/zookeeper/bin/../lib/commons-io-2.11.0.jar:/root/download/zookeeper/bin/../lib/commons-cli-1.4.jar:/root/download/zookeeper/bin/../lib/audience-annotations-0.12.0.jar:/root/download/zookeeper/bin/../zookeeper-*.jar:/root/download/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/root/download/zookeeper/bin/../conf:
2022-08-30 02:50:49,013 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2022-08-30 02:50:49,013 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:java.io.tmpdir=/tmp
2022-08-30 02:50:49,014 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:java.compiler=<NA>
2022-08-30 02:50:49,014 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:os.name=Linux
2022-08-30 02:50:49,014 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:os.arch=amd64
2022-08-30 02:50:49,014 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:os.version=3.10.0-514.el7.x86_64
2022-08-30 02:50:49,014 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:user.name=root
2022-08-30 02:50:49,014 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:user.home=/root
2022-08-30 02:50:49,014 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:user.dir=/root/download/zookeeper/bin
2022-08-30 02:50:49,014 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:os.memory.free=10MB
2022-08-30 02:50:49,014 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:os.memory.max=247MB
2022-08-30 02:50:49,015 [myid:] - INFO [main:o.a.z.Environment@98] - Client environment:os.memory.total=15MB
2022-08-30 02:50:49,025 [myid:] - INFO [main:o.a.z.ZooKeeper@637] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@1b604f19
2022-08-30 02:50:49,030 [myid:] - INFO [main:o.a.z.c.X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2022-08-30 02:50:49,042 [myid:] - INFO [main:o.a.z.ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2022-08-30 02:50:49,050 [myid:] - INFO [main:o.a.z.ClientCnxn@1732] - zookeeper.request.timeout value is 0. feature enabled=false
Welcome to ZooKeeper!
JLine support is enabled
2022-08-30 02:50:49,121 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1171] - Opening socket connection to server localhost/127.0.0.1:2181.
2022-08-30 02:50:49,121 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1173] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2022-08-30 02:50:49,130 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1005] - Socket connection established, initiating session, client: /127.0.0.1:36080, server: localhost/127.0.0.1:2181
2022-08-30 02:50:49,172 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1444] - Session establishment complete on server localhost/127.0.0.1:2181, session id = 0x100001fa4000000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
# 查看当前节点信息
[zk: localhost:2181(CONNECTED) 1] ls /zookeeper
[config, quota]
[zk: localhost:2181(CONNECTED) 2] ls /zookeeper/config
[]
[zk: localhost:2181(CONNECTED) 3] ls /zookeeper/quota
[]
[zk: localhost:2181(CONNECTED) 4] ls -R /zookeeper
/zookeeper
/zookeeper/config
/zookeeper/quota
# 创建节点
[zk: localhost:2181(CONNECTED) 6] create /app1 data_test
Created /app1
[zk: localhost:2181(CONNECTED) 8] create /app2
Created /app2
[zk: localhost:2181(CONNECTED) 9] get /app1
data_test
[zk: localhost:2181(CONNECTED) 10] get /app2
null
[zk: localhost:2181(CONNECTED) 13] create /app2/app21
Created /app2/app21
[zk: localhost:2181(CONNECTED) 14] create /app2/app22 date_test_22
Created /app2/app22
[zk: localhost:2181(CONNECTED) 15] ls /app2
[app21, app22]
# 修改节点
[zk: localhost:2181(CONNECTED) 11] set /app2 data_test222
[zk: localhost:2181(CONNECTED) 12] get /app2
data_test222
# 删除节点
[zk: localhost:2181(CONNECTED) 16] delete /app2 # 若节点下存在子节点,则需要删除子节点,后删除父节点
Node not empty: /app2
[zk: localhost:2181(CONNECTED) 17] delete
delete deleteall
[zk: localhost:2181(CONNECTED) 17] deleteall /app2 # deleteall 可以强行删除父节点
[zk: localhost:2181(CONNECTED) 18] ls /
[app1, zookeeper]
# 帮助
[zk: localhost:2181(CONNECTED) 19] help
ZooKeeper -server host:port -client-configuration properties-file cmd args
addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
addauth scheme auth
close
config [-c] [-w] [-s]
connect host:port
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
delete [-v version] path
deleteall path [-b batch size]
delquota [-n|-b|-N|-B] path
get [-s] [-w] path
getAcl [-s] path
getAllChildrenNumber path
getEphemerals path
history
listquota path
ls [-s] [-w] [-R] path
printwatches on|off
quit
reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
redo cmdno
removewatches path [-c|-d|-a] [-l]
set [-s] [-v version] path data
setAcl [-s] [-v version] [-R] path acl
setquota -n|-b|-N|-B val path
stat [-w] path
sync path
version
whoami
Command not found: Command not found help
# 退出
[zk: localhost:2181(CONNECTED) 20] quit
2022-08-30 03:12:45,303 [myid:localhost:2181] - WARN [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1286] - An exception was thrown while closing send thread for session 0x100001fa4000001.
org.apache.zookeeper.ClientCnxn$EndOfStreamException: Unable to read additional data from server sessionid 0x100001fa4000001, likely server has closed socket
at org.apache.zookeeper.ClientCnxnSocketNIO.doIO(ClientCnxnSocketNIO.java:77)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:350)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1282)
WATCHER::
WatchedEvent state:Closed type:None path:null
2022-08-30 03:12:45,410 [myid:] - INFO [main:o.a.z.ZooKeeper@1232] - Session: 0x100001fa4000001 closed
2022-08-30 03:12:45,412 [myid:] - ERROR [main:o.a.z.u.ServiceUtils@42] - Exiting JVM with code 127
2022-08-30 03:12:45,415 [myid:] - INFO [main-EventThread:o.a.z.ClientCnxn$EventThread@568] - EventThread shut down for session: 0x100001fa4000001
3、集群部署
真实的集群是需要部署在不同的服务器上,但是在测试环境中这种行为需要多数的服务器支持,相对浪费资源,因此可以将不同的服务使用端口进行区分,以此构成伪集群。
# 安装包上传并进行解压
[root@centos7 download]# ll
总用量 12880
drwxr-xr-x. 6 root root 133 8月 31 20:02 apache-zookeeper-3.8.0-bin
-rw-r--r--. 1 root root 13185104 8月 30 02:39 apache-zookeeper-3.8.0-bin.tar.gz
[root@centos7 download]# cp -r apache-zookeeper-3.8.0-bin zookeeper_1
[root@centos7 download]# cp -r apache-zookeeper-3.8.0-bin zookeeper_2
[root@centos7 download]# cp -r apache-zookeeper-3.8.0-bin zookeeper_3
# 创建数据存储路径,并复制配置文件
[root@centos7 download]# mkdir ./zookeeper_1/data
[root@centos7 download]# mkdir ./zookeeper_2/data
[root@centos7 download]# mkdir ./zookeeper_3/data
[root@centos7 download]#
[root@centos7 download]# cp -a ./zookeeper_1/conf/zoo_sample.cfg ./zookeeper_1/conf/zoo.cfg
[root@centos7 download]# cp -a ./zookeeper_2/conf/zoo_sample.cfg ./zookeeper_2/conf/zoo.cfg
[root@centos7 download]# cp -a ./zookeeper_3/conf/zoo_sample.cfg ./zookeeper_3/conf/zoo.cfg
# 修改配置文件中对应的数据存储路径
[root@centos7 download]# sed -i '12s@dataDir\\=\\/tmp\\/zookeeper@dataDir\\=\\/root\\/download\\/zookeeper\\_1\\/data\\/tmp@' /root/download//zookeeper_1/conf/zoo.cfg
[root@centos7 download]# sed -i '12s@dataDir\\=\\/tmp\\/zookeeper@dataDir\\=\\/root\\/download\\/zookeeper\\_2\\/data\\/tmp@' /root/download//zookeeper_2/conf/zoo.cfg
[root@centos7 download]# sed -i '12s@dataDir\\=\\/tmp\\/zookeeper@dataDir\\=\\/root\\/download\\/zookeeper\\_3\\/data\\/tmp@' /root/download//zookeeper_3/conf/zoo.cfg
# 修改服务端口(伪集群端口不可冲突)
[root@centos7 download]# sed -i '14s@clientPort=2181@clientPort=2182@' /root/download//zookeeper_2/conf/zoo.cfg
[root@centos7 download]# sed -i '14s@clientPort=2181@clientPort=2183@' /root/download//zookeeper_3/conf/zoo.cfg
# 配置集群IP列表,配置格式:server.id=服务器IP地址:服务器之间的通信端口:
# zookeeper_2与zookeeper_3配置相同
[root@centos7 download]# sed -i '$i server.1=192.168.160.128:2881:3881\\nserver.2=192.168.160.128:2882:3882\\nserver.3=192.168.160.128:2883:3883' /root/download/zookeeper_1/conf/zoo.cfg
# 在tmp目录(配置文件中自定义的数据存储路径)下创建myid文件,id需要对应配置的IP地址
[root@centos7 download]# echo '1' > /root/download/zookeeper_1/data/tmp/myid
[root@centos7 download]# echo '2' > /root/download/zookeeper_2/data/tmp/myid
[root@centos7 download]# echo '3' > /root/download/zookeeper_3/data/tmp/myid
# 启动集群服务
[root@centos7 download]# /root/download/zookeeper_1/bin/zkServer.sh start
[root@centos7 download]# /root/download/zookeeper_2/bin/zkServer.sh start
[root@centos7 download]# /root/download/zookeeper_3/bin/zkServer.sh start
# 服务启动验证
[root@centos7 conf]# netstat -atunolp | grep -E '2181|2182|2183'
tcp6 0 0 :::2181 :::* LISTEN 4176/java off (0.00/0/0)
tcp6 0 0 :::2182 :::* LISTEN 5267/java off (0.00/0/0)
tcp6 0 0 :::2183 :::* LISTEN 5495/java off (0.00/0/0)
# 查看节点Mode状态
[root@centos7 download]# /root/download/zookeeper_1/bin/zkServer.sh status | grep Mode
Mode: follower
[root@centos7 download]# /root/download/zookeeper_2/bin/zkServer.sh status | grep Mode
Mode: leader
[root@centos7 download]# /root/download/zookeeper_3/bin/zkServer.sh status | grep Mode
Mode: follower
以上是关于MiddleWare ❀ Zookeeper基础概述的主要内容,如果未能解决你的问题,请参考以下文章
Shell ❀ Zookeeper和Kafka集群环境启停脚本