`
lshh83
  • 浏览: 159442 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Zookeeper的集群配置

阅读更多

一 集群的作用

为了获得可靠的 ZooKeeper 服务,用户可以在一个集群上部署 ZooKeeper 。只要集群上大多数的ZooKeeper 服务启动了,那么总的 ZooKeeper 服务将是可用的。另外,最好使用奇数台机器。 如果 zookeeper拥有 5 台机器,那么它就能处理 2 台机器的故障了。

二 zookeeper伪分布式集群配置 

所谓 "伪分布式集群" 就是在,在一台PC中,启动多个ZooKeeper的实例。"完全分布式集群" 是每台PC,启动一个ZooKeeper实例。下面介绍一下windows下的配置方法。
1.解压三个zookeeper目录

D:\zookeeper\zookeeper-1

D:\zookeeper\zookeeper-2

D:\zookeeper\zookeeper-3


2.在这3个目录下面建立data和log目录,并在data目录下面新建myid文件  myid的文件内容分别为:1、2、3 ,这个文件中仅含有一行的内容,指定的是自身的 id 值。比如,服务器" 1 "应该在 myid 文件中写入" 1 "。这个 id 值必须是 ensemble 中唯一的,且大小在 1 到 255 之间。
Ps:windows下可建立myid.txt文件,修改完内容后将后缀删除即可。
 
3.修改每个服务端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. 
dataDir=D:\\zookeeper\\zookeeper-1data 
# the directory where the log 
dataLogDir=D:\\zookeeper\\zookeeper-1log
# the port at which the clients will connect  clientPort=2181 
#Clusters  server.1=127.0.0.1:2888:3888  server.2=127.0.0.1:2889:3889 
server.3=127.0.0.1:2890:3890


其中下面clusters的配置里," server.id=host:port:port. "指示了不同的 ZooKeeper 服务器的自身标识,作为集群的一部分的机器应该知道 集群中的其它机器。用户可以从" server.id=host:port:port. "中读取相关的信息。 其中的id就是我们在上一步在data( dataDir 参数所指定的目录)目录下创建的 myid 的文件中设置的id。这一行配置中,第一个端口用来集群成员的信息交换,第二个端口是在leader挂掉时专门用来进行选举leader所用。需要注意的是clientPort这个端口如果你是在1台机器上部署多个server,那么每台机器都要不同的clientPort,比如我server1是2181,server2是2182,server3是2183,dataDir和dataLogDir也需要区分下。因为在同一台机器上,后面连着的2个端口3个server都不要一样,否则端口冲突。

4. 启动ZooKeeper伪机群的所有服务器
分别进入三个服务器的zookeeper/bin目录下,启动服务

三 完全分布式集群

完全分布式集群跟伪集群配置是一样的,只是将三个zookeeper分布在不同的机器上面,所以不存在端口冲突的问题,clientport和下面clusters里配置的各个服务器的端口是可以一样的。
 

四 项目连接集群配置

将集群的zookeeper地址配置到项目中的spring-service.xml配置文件里即可。
如:

spring-service.xml
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics