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]# lltotal 0drwxr-xr-x、3 root root 63 Feb 24 08:43 zk1drwxr-xr-x、3 root root 63 Feb 24 08:43 zk2drwxr-xr-x、3 root root 63 Feb 24 08:43 zk3drwxr-xr-x、3 root root 63 Feb 24 08:43 zk4
修改配置文件几个要点:
# example sakes.持久化保持的文件目录,四个配置文件分别是zk1 zk2 zk3 zk4dataDir=/home/zookeeper/apache-zookeeper-3.7.0-bin/data/zk1
#给客户端访问的端口,四个配置文件分别是2181 2182 2183 2184clientPort=2181
#都是集群之间访问的端口,四个配置文件都一样#第一个端口是服务节点之间通讯用的#第二个端口是投票选举leader的端口server.1=192.168.106.129:2001:3001server.2=192.168.106.129:2002:3002server.3=192.168.106.129:2003:3003server.4=192.168.106.129:2004:3004:observer
完整的配置文件以zk1的为例:
# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial # synchronization phase can takeinitLimit=10# The number of ticks that can pass between # sending a request and getting an acknowledgementsyncLimit=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:3001server.2=192.168.106.129:2002:3002server.3=192.168.106.129:2003:3003server.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 defaultUsing config: ../conf/zoo4.cfgClient port found: 2184、Client address: localhost、Client SSL: false.Mode: observer[root@localhost bin]# ./zkServer.sh status ../conf/zoo3.cfg ZooKeeper JMX enabled by defaultUsing config: ../conf/zoo3.cfgClient port found: 2183、Client address: localhost、Client SSL: false.Mode: follower[root@localhost bin]# ./zkServer.sh status ../conf/zoo2.cfg ZooKeeper JMX enabled by defaultUsing config: ../conf/zoo2.cfgClient port found: 2182、Client address: localhost、Client SSL: false.Mode: leader[root@localhost bin]# ./zkServer.sh status ../conf/zoo1.cfg ZooKeeper JMX enabled by defaultUsing config: ../conf/zoo1.cfgClient 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:21842022-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 UTC2022-02-24 09:32:03,441 [myid:] - INFO [main:Environment@98] - Client environment:host.name=localhost2022-02-24 09:32:03,441 [myid:] - INFO [main:Environment@98] - Client environment:java.version=1.8.0_2912022-02-24 09:32:03,445 [myid:] - INFO [main:Environment@98] - Client environment:java.vendor=Oracle Corporation2022-02-24 09:32:03,445 [myid:] - INFO [main:Environment@98] - Client environment:java.home=/home/java/jdk1.8.0_291/jre2022-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.jar2022-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/lib2022-02-24 09:32:03,446 [myid:] - INFO [main:Environment@98] - Client environment:java.io.tmpdir=/tmp2022-02-24 09:32:03,446 [myid:] - INFO [main:Environment@98] - Client environment:java.compiler=