zookeeper学习(二)

zookeeper单台linux部署集群

一、安装步骤

  1.下载zookeeper并在根目录创建3个文件夹

      zk1,zk2,zk3,用于存放每个集群的数据文件,并分别在3个目录中创建data和logs目录,如下实例:

  

    

   2、配置配置文件

    将conf目录下的zoo-sample.cfg 更改为zoo1.cfg , zoo2.cfg ,zoo3.cfg :

    

       文件配置如下:  

 1 # The number of milliseconds of each tick
 2 # 心跳检测时间
 3 tickTime=2000
 4 # The number of ticks that the initial 
 5 # synchronization phase can take
 6 # 心跳检测失败最大次数
 7 initLimit=10
 8 # The number of ticks that can pass between 
 9 # sending a request and getting an acknowledgement
10 syncLimit=5
11 # the directory where the snapshot is stored.
12 # do not use /tmp for storage, /tmp here is just 
13 # example sakes.
14 # 内存数据库保存的模糊快照的目录,如果某个服务器为集群中的一台,则id文件也保存在该目录下
15 #  快照异步写入,不会阻塞
16 dataDir= ../zk1/data
17 dataLogDir= ../zk1/logs
18 # the port at which the clients will connect
19 # TCP监听端口
20 clientPort=2181
21 # the maximum number of client connections.
22 # increase this if you need to handle more clients
23 #maxClientCnxns=60
24 #
25 # Be sure to read the maintenance section of the 
26 # administrator guide before turning on autopurge.
27 #
28 # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
29 #
30 # The number of snapshots to retain in dataDir
31 #autopurge.snapRetainCount=3
32 # Purge task interval in hours
33 # Set to "0" to disable auto purge feature
34 #autopurge.purgeInterval=1
35 # server.myid=IP:PORT1;PORT2
36 #PORT1:服务器与集群中的LEADER服务器交换信息的端口
37 #PORT2:万一集群中的LEADER挂了,需要一个端口来重新进行宣讲 选出新的LEADER 
38 server.1=127.0.0.1:2881:3881
39 server.2=127.0.0.1:2882:3882
40 server.3=127.0.0.1:2883:3883

  3.三个文件中的差异

  zoo1.cfg 

1 dataDir= ../zk1/data
2 dataLogDir= ../zk1/logs
3 clientPort=2181

  zoo2.cfg

dataDir= ../zk2/data
dataLogDir= ../zk2/logs
clientPort=2182

  zoo3.cfg

1 dataDir= ../zk3/data
2 dataLogDir= ../zk3/logs
3 clientPort=2183
  • tickTime: 基本事件单元,以毫秒为单位。这个时间是作为zookeeper服务器之间或客户端与服务器间维持心跳的时间。也就是每隔tickTime时间就会发送一个心跳
  • dataDir: 存储内存中数据库快照的位置,就是zookeeper保存数据的目录,默认情况下,zookeeper将数据的日志问也保存在这个目录里
  • clientPort: 客户端连接zookeeper服务器的端口,默认是2181,zookeeper会监听这个端口,接收客户端的访问请求
  • initLimit: 这个配置项是用来配置zookeeper接收客户端初始化连接能忍受多少个心跳时间间隔数。当已经超过10个心跳的时间(tickTime)长度后,zookeeper服务器还没有接收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 10*2000 = 20 秒
  • syncLimit: 这个配置项标识Leader和Follower之间发送消息,请求和应答的长度,最长不能超过多少个tickTime的时间长度,总的时间长度就是 5 * 2000 = 10秒
  • server.myid=IP:Port1:Port2, myid是服务器的编号,一个正整数,一般是0、1、2、3等待,port1表示的是服务器与集群中的Leader服务器交换信息的端口,Port2表示的是万一集群中的Leader服务器宕机了,需要一个端口来重新进行宣讲,选出一个新的Leader,如果是不同的机器,可以选用相同的端口,由于本次测试是在单机上进行,因此选用不同的端口。

  4、创建myid文件

  

   5、启动服务常看相关状态

   

    可以看到,ZK2选举成为了leader,另外两个为follower。

  6、链接客户端 

1 ./zkCli.sh  -server 127.0.0.1:2181
2 
3 
4 ./zkCli.sh  -server 127.0.0.1:2182
5 
6 
7 ./zkCli.sh  -server 127.0.0.1:2183

  分别连接三个集群节点,在任何一个集群节点进行操作,其他集群也会同步更新;在其中一个创造节点,可在其他的服务中看到,如下图:

  

   

  7、宕机测试

  

参考文献:https://www.cnblogs.com/lgjlife/p/10546014.html

     

    

  

kafka rabbitMq
原文地址:https://www.cnblogs.com/stt101517/p/14917324.html