zookeeper中角色分
三
种:
领导者leader
:服务器为客户端提供读服务和写服务。
跟随者follower
:服务器为客户端提供读服务,参与Leader选举过程,参与写操作“过半写成功”策略。
观察者observer
:服务器为客户端提供读服务,不参与Leader选举过程,不参与写操作“过半写成功”策略。用于在不影响写性能的前提下提升集群的读性能。
集群搭建步骤:
基于之前的单机zk,搭建伪集群,复制4份配置文件出来,分别启动四次
。
在data下新增四个存储数据的文件夹并新建myid
文件,内容分别是1 2 3 4
echo >> myid 1
效果如下
/home/zookeeper/apache-zookeeper-3.7.0-bin/data
[root@localhost data]# ll
total 0
drwxr-xr-x. 3 root root 63 Feb 24 08:43 zk1
drwxr-xr-x. 3 root root 63 Feb 24 08:43 zk2
drwxr-xr-x. 3 root root 63 Feb 24 08:43 zk3
drwxr-xr-x. 3 root root 63 Feb 24 08:43 zk4
修改配置文件几个要点:
# example sakes.持久化保持的文件目录,四个配置文件分别是zk1 zk2 zk3 zk4
dataDir=/home/zookeeper/apache-zookeeper-3.7.0-bin/data/zk1
#给客户端访问的端口,四个配置文件分别是2181 2182 2183 2184
clientPort=2181
#都是集群之间访问的端口,四个配置文件都一样
#第一个端口是服务节点之间通讯用的
#第二个端口是投票选举leader的端口
server.1=192.168.106.129:2001:3001
server.2=192.168.106.129:2002:3002
server.3=192.168.106.129:2003:3003
server.4=192.168.106.129:2004:3004:observer
完整的配置文件以zk1
的为例:
# 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=/home/zookeeper/apache-zookeeper-3.7.0-bin/data/zk1
# 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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
#都是集群之间访问的端口
#第一个端口是服务节点之间通讯用的
#第二个端口是投票选举leader的端口
server.1=192.168.106.129:2001:3001
server.2=192.168.106.129:2002:3002
server.3=192.168.106.129:2003:3003
server.4=192.168.106.129:2004:3004:observer
至此配置修改完成
启动四个zk服务
#在bin目录下分别执行四次,每次要修改conf
./zkServer.sh status ../conf/zoo1.cfg
查看四个服务的角色,2是leader
[root@localhost bin]# ./zkServer.sh status ../conf/zoo4.cfg
ZooKeeper JMX enabled by default
Using config: ../conf/zoo4.cfg
Client port found: 2184. Client address: localhost. Client SSL: false.
Mode: observer
[root@localhost bin]# ./zkServer.sh status ../conf/zoo3.cfg
ZooKeeper JMX enabled by default
Using config: ../conf/zoo3.cfg
Client port found: 2183. Client address: localhost. Client SSL: false.
Mode: follower
[root@localhost bin]# ./zkServer.sh status ../conf/zoo2.cfg
ZooKeeper JMX enabled by default
Using config: ../conf/zoo2.cfg
Client port found: 2182. Client address: localhost. Client SSL: false.
Mode: leader
[root@localhost bin]# ./zkServer.sh status ../conf/zoo1.cfg
ZooKeeper JMX enabled by default
Using config: ../conf/zoo1.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
连接集群
[root@localhost bin]# ./zkCli.sh -server 192.168.106.129:2181,192.168.106.129:2182,192.168.106.129:2183,192.168.106.129:2184
Connecting to 192.168.106.129:2181,192.168.106.129:2182,192.168.106.129:2183,192.168.106.129:2184
2022-02-24 09:32:03,435 [myid:] - INFO [main:Environment@98] - Client environment:zookeeper.version=3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC
2022-02-24 09:32:03,441 [myid:] - INFO [main:Environment@98] - Client environment:host.name=localhost
2022-02-24 09:32:03,441 [myid:] - INFO [main:Environment@98] - Client environment:java.version=1.8.0_291
2022-02-24 09:32:03,445 [myid:] - INFO [main:Environment@98] - Client environment:java.vendor=Oracle Corporation
2022-02-24 09:32:03,445 [myid:] - INFO [main:Environment@98] - Client environment:java.home=/home/java/jdk1.8.0_291/jre
2022-02-24 09:32:03,445 [myid:] - INFO [main:Environment@98] - Client environment:java.class.path=/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../zookeeper-server/target/classes:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../build/classes:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../zookeeper-server/target/lib/*.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../build/lib/*.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/zookeeper-prometheus-metrics-3.7.0.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/zookeeper-jute-3.7.0.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/zookeeper-3.7.0.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/snappy-java-1.1.7.7.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/slf4j-log4j12-1.7.30.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/slf4j-api-1.7.30.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/simpleclient_servlet-0.9.0.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/simpleclient_hotspot-0.9.0.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/simpleclient_common-0.9.0.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/simpleclient-0.9.0.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/netty-transport-native-unix-common-4.1.59.Final.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/netty-transport-native-epoll-4.1.59.Final.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/netty-transport-4.1.59.Final.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/netty-resolver-4.1.59.Final.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/netty-handler-4.1.59.Final.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/netty-common-4.1.59.Final.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/netty-codec-4.1.59.Final.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/netty-buffer-4.1.59.Final.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/metrics-core-4.1.12.1.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/log4j-1.2.17.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/jline-2.14.6.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-util-ajax-9.4.38.v20210224.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-util-9.4.38.v20210224.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-servlet-9.4.38.v20210224.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-server-9.4.38.v20210224.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-security-9.4.38.v20210224.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-io-9.4.38.v20210224.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-http-9.4.38.v20210224.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/javax.servlet-api-3.1.0.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/jackson-databind-2.10.5.1.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/jackson-core-2.10.5.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/jackson-annotations-2.10.5.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/commons-cli-1.4.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../lib/audience-annotations-0.12.0.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../zookeeper-*.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../zookeeper-server/src/main/resources/lib/*.jar:/home/zookeeper/apache-zookeeper-3.7.0-bin/bin/../conf:.:/home/java/jdk1.8.0_291/lib/dt.jar:/home/java/jdk1.8.0_291/lib/tools.jar
2022-02-24 09:32:03,446 [myid:] - INFO [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2022-02-24 09:32:03,446 [myid:] - INFO [main:Environment@98] - Client environment:java.io.tmpdir=/tmp
2022-02-24 09:32:03,446 [myid:] - INFO [main:Environment@98] - Client environment:java.compiler=<NA>
2022-02-24 09:32:03,447 [myid:] - INFO [main:Environment@98] - Client environment:os.name=Linux
2022-02-24 09:32:03,447 [myid:] - INFO [main:Environment@98] - Client environment:os.arch=amd64
2022-02-24 09:32:03,447 [myid:] - INFO [main:Environment@98] - Client environment:os.version=3.10.0-1160.el7.x86_64
2022-02-24 09:32:03,447 [myid:] - INFO [main:Environment@98] - Client environment:user.name=root
2022-02-24 09:32:03,447 [myid:] - INFO [main:Environment@98] - Client environment:user.home=/root
2022-02-24 09:32:03,447 [myid:] - INFO [main:Environment@98] - Client environment:user.dir=/home/zookeeper/apache-zookeeper-3.7.0-bin/bin
2022-02-24 09:32:03,447 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.free=11MB
2022-02-24 09:32:03,464 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.max=247MB
2022-02-24 09:32:03,464 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.total=15MB
2022-02-24 09:32:03,476 [myid:] - INFO [main:ZooKeeper@637] - Initiating client connection, connectString=192.168.106.129:2181,192.168.106.129:2182,192.168.106.129:2183,192.168.106.129:2184 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@4ca8195f
2022-02-24 09:32:03,485 [myid:] - INFO [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2022-02-24 09:32:03,498 [myid:] - INFO [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2022-02-24 09:32:03,534 [myid:] - INFO [main:ClientCnxn@1726] - zookeeper.request.timeout value is 0. feature enabled=false
Welcome to ZooKeeper!
JLine support is enabled
2022-02-24 09:32:03,600 [myid:192.168.106.129:2184] - INFO [main-SendThread(192.168.106.129:2184):ClientCnxn$SendThread@1171] - Opening socket connection to server 192.168.106.129/192.168.106.129:2184.
2022-02-24 09:32:03,600 [myid:192.168.106.129:2184] - INFO [main-SendThread(192.168.106.129:2184):ClientCnxn$SendThread@1173] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2022-02-24 09:32:03,637 [myid:192.168.106.129:2184] - INFO [main-SendThread(192.168.106.129:2184):ClientCnxn$SendThread@1005] - Socket connection established, initiating session, client: /192.168.106.129:43126, server: 192.168.106.129/192.168.106.129:2184
2022-02-24 09:32:03,966 [myid:192.168.106.129:2184] - INFO [main-SendThread(192.168.106.129:2184):ClientCnxn$SendThread@1438] - Session establishment complete on server 192.168.106.129/192.168.106.129:2184, session id = 0x4000019866d0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 192.168.106.129:2181,192.168.106.129:2182,192.168.106.129:2183,192.168.106.129:2184(CONNECTED) 0]