zooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
zooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
在大型企业的开发中,服务的数量是十分庞大的。如果我们想要添加一个服务的话,那么就需要对文件进行重新覆盖,把整个容器重启。这样导致的结果是就是:波及影响太大,维护十分困难。
此时,便需要一个能够动态注册服务和获取服务信息的地方,来统一管理服务,这个地方便称为称为服务配置中心。而zookeeper不仅实现了对cusumer和provider的灵活管理,平滑过渡功能,而且还内置了负载均衡、主动通知等功能,使我们能够几乎实时的感应到服务的状态。能够很好的帮我们解决分布式相关的问题,至今仍是市面上主流的分布式服务注册中心技术之一。
IP | Hostname |
172.16.10.67 | 172.16.10.67 |
172.16.10.68 | 172.16.10.68 |
172.16.10.69 | 172.16.10.69 |
zookeeper 官网:Apache ZooKeeper
最新稳定版下载地址:Apache Downloads
3.1 三个节点一样操作,环境变量
[root@172 apache-zookeeper-3.6.3-bin]# cat /etc/profile
JAVA_HOME=/usr/java/jdk
PATH=$GRADLE_HOME/bin:$JAVA_HOME/bin:$NODE_HOME/bin:$NODE_HOME1/bin:$PATH
CLASSPATH=.:$JAVA_HOME/jre/lib/ext:$JAVA_HOME/jre/lib/ext/jar:$JAVA_HOME/lib/tools.jar
export PATH JAVA_HOME CLASSPATH
export HADOOP_HOME=/root/hadoop/server/hadoop
export HADOOP_INSTALL=$HADOOP_HOME
export HADOOP_MAPRED_HOME=$HADOOP_HOME
export HADOOP_HDFS_HOME=$HADOOP_HOME
export HADOOP_COMMON_HOME=$HADOOP_HOME
export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop
export PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin
export ZOOKEEPER_HOME=/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin
export PATH=$PATH:$ZOOKEEPER_HOME/bin
export HBASE_HOME=/root/hadoop/server/hbase/hbase-2.5.0
export PATH=$PATH:$HBASE_HOME/bin
3.2 配置文件:
[root@172 apache-zookeeper-3.6.3-bin]# vim conf/
configuration.xsl log4j.properties zoo.cfg zoo_sample.cfg
[root@172 apache-zookeeper-3.6.3-bin]# vim conf/zoo.cfg
[root@172 apache-zookeeper-3.6.3-bin]# cat conf/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/hadoop/server/zk/data
# the port at which the clients will connect
clientPort=2181
server.1=172.16.10.67:2888:3888
server.2=172.16.10.68:2888:3888
server.3=172.16.10.69:2888:3888
# 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
3.3 进入zkData中,修改myid文件,此处要与zoo.cfg中的修改相对应
[root@172 zk]# ls
apache-zookeeper-3.6.3-bin data
[root@172 zk]# cat data/myid
1
待三台虚拟机都配置完成后就可以开始开始测试了
启动各个服务器的zookeeper
[root@172 zk]# zkServer.sh start|status
ZooKeeper JMX enabled by default
Using config: /root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
[root@172 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
[root@172 zk]# zkServer.sh start|status
ZooKeeper JMX enabled by default
Using config: /root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
测试:
可以看到我们启动后的17565 即是zookeeper服务端
[root@172 ~]# jps
17565 QuorumPeerMain
回到zookeeper目录下,启动客户端
[root@172 ~]# zkCli.sh
Connecting to localhost:2181
2022-09-06 14:37:38,728 [myid:] - INFO [main:Environment@98] - Client environment:zookeeper.version=3.6.3--6401e4ad2087061bc6b9f80dec2d69f2e3c8660a, built on 04/08/2021 16:35 GMT
2022-09-06 14:37:38,732 [myid:] - INFO [main:Environment@98] - Client environment:host.name=172.16.10.68
2022-09-06 14:37:38,732 [myid:] - INFO [main:Environment@98] - Client environment:java.version=1.8.0_321
2022-09-06 14:37:38,735 [myid:] - INFO [main:Environment@98] - Client environment:java.vendor=Oracle Corporation
2022-09-06 14:37:38,735 [myid:] - INFO [main:Environment@98] - Client environment:java.home=/usr/java/jdk1.8.0_321/jre
2022-09-06 14:37:38,735 [myid:] - INFO [main:Environment@98] - Client environment:java.class.path=/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../zookeeper-server/target/classes:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../build/classes:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../zookeeper-server/target/lib/*.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../build/lib/*.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/zookeeper-prometheus-metrics-3.6.3.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/zookeeper-jute-3.6.3.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/zookeeper-3.6.3.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/snappy-java-1.1.7.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/slf4j-log4j12-1.7.25.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/slf4j-api-1.7.25.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/simpleclient_servlet-0.6.0.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/simpleclient_hotspot-0.6.0.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/simpleclient_common-0.6.0.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/simpleclient-0.6.0.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/netty-transport-native-unix-common-4.1.63.Final.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/netty-transport-native-epoll-4.1.63.Final.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/netty-transport-4.1.63.Final.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/netty-resolver-4.1.63.Final.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/netty-handler-4.1.63.Final.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/netty-common-4.1.63.Final.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/netty-codec-4.1.63.Final.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/netty-buffer-4.1.63.Final.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/metrics-core-3.2.5.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/log4j-1.2.17.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/json-simple-1.1.1.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/jline-2.14.6.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/jetty-util-ajax-9.4.39.v20210325.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/jetty-util-9.4.39.v20210325.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/jetty-servlet-9.4.39.v20210325.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/jetty-server-9.4.39.v20210325.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/jetty-security-9.4.39.v20210325.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/jetty-io-9.4.39.v20210325.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/jetty-http-9.4.39.v20210325.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/javax.servlet-api-3.1.0.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/jackson-databind-2.10.5.1.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/jackson-core-2.10.5.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/jackson-annotations-2.10.5.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/commons-cli-1.2.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../lib/audience-annotations-0.5.0.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../zookeeper-*.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../zookeeper-server/src/main/resources/lib/*.jar:/root/hadoop/server/zk/apache-zookeeper-3.6.3-bin/bin/../conf:.:/usr/java/jdk/jre/lib/ext:/usr/java/jdk/jre/lib/ext/jar:/usr/java/jdk/lib/tools.jar
2022-09-06 14:37:38,735 [myid:] - INFO [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2022-09-06 14:37:38,735 [myid:] - INFO [main:Environment@98] - Client environment:java.io.tmpdir=/tmp
2022-09-06 14:37:38,735 [myid:] - INFO [main:Environment@98] - Client environment:java.compiler=
2022-09-06 14:37:38,735 [myid:] - INFO [main:Environment@98] - Client environment:os.name=Linux
2022-09-06 14:37:38,735 [myid:] - INFO [main:Environment@98] - Client environment:os.arch=amd64
2022-09-06 14:37:38,735 [myid:] - INFO [main:Environment@98] - Client environment:os.version=3.10.0-862.el7.x86_64
2022-09-06 14:37:38,736 [myid:] - INFO [main:Environment@98] - Client environment:user.name=root
2022-09-06 14:37:38,736 [myid:] - INFO [main:Environment@98] - Client environment:user.home=/root
2022-09-06 14:37:38,736 [myid:] - INFO [main:Environment@98] - Client environment:user.dir=/root
2022-09-06 14:37:38,736 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.free=112MB
2022-09-06 14:37:38,737 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.max=228MB
2022-09-06 14:37:38,737 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.total=119MB
2022-09-06 14:37:38,741 [myid:] - INFO [main:ZooKeeper@1006] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@36aa7bc2
2022-09-06 14:37:38,745 [myid:] - INFO [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2022-09-06 14:37:38,751 [myid:] - INFO [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2022-09-06 14:37:38,758 [myid:] - INFO [main:ClientCnxn@1736] - zookeeper.request.timeout value is 0. feature enabled=false
Welcome to ZooKeeper!
JLine support is enabled
2022-09-06 14:37:38,816 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1181] - Opening socket connection to server localhost/127.0.0.1:2181.
2022-09-06 14:37:38,817 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1183] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2022-09-06 14:37:38,826 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1013] - Socket connection established, initiating session, client: /127.0.0.1:45364, server: localhost/127.0.0.1:2181
2022-09-06 14:37:38,841 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1448] - Session establishment complete on server localhost/127.0.0.1:2181, session id = 0x200f7ffe384000d, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
Linux环境下zookeeper的安装教程(超详细!!)_啊陈晓的博客-CSDN博客_linux zookeeper安装