window环境搭建zookeeper,kafka集群

本文是对在window环境下如何搭建zookeeper集群和kafka集群的研究,如果你正在查找相关此类资料,希望本文能对你有所帮助。

网上很多资料关于"kafka集群"几乎都是一个模样,写得东西很类似,而且很多细节没有注明,需要在不断的测试过程中推敲才能得到理想的结果。本篇文章力求能尽量说明每一个细节每一个步骤,达到只要认真看过本文者都能自己搭建一个基于window的kafka集群环境,当然这里仅代表学习心得,不具备任何商业用途的说明。

话不多说,实践才是硬道理。在进行搭建之前需要说明几点,为了能更好的了解本文的相关内容,推荐您先认真查阅kafka入门这篇文章,这能作为基础能让你更顺利看懂本篇文章。为了演示集群的效果,这里准备了一台虚拟机(window server 2008),在虚拟机中搭建了单IP多节点的zookeeper集群(多IP节点的也是同理的),并且在本机(win 10)和虚拟机中都安装了kafka。简言之需要准备的东西如下:

1.三台zookeeper服务器,都安装在同一台虚拟机中。

2.本机win10,和虚拟机分别安装一台kafka服务器。

一:虚拟机和本机网络环境

1.将虚拟机的网络模式调整为桥接模式,将虚拟机的防火墙功能关闭;

2.将主机的防火墙也关闭。

3.互相ping,测试是否能正常连接.

1)查看主机或者虚拟机中的ip:

   在cmd中输入ipconfig,找到IP4地址。这就是机子的ip地址。

   主机win10:

   

   虚拟机winserver2008:

   

2)使用ping ip地址  测试网络是否连接

   在两台机子的cmd中相互输入ping一下对方的ip:

   主机win10:

   

  winserver2008:

   

二 虚拟机中搭建zookeeper集群

  相对来说,zookeeper集群的搭建是很容易的,只需要注意一下配置文件。

  首先准备三份zookeeper程序,这里命名为server1,server2,server3.三台zookeeper服务器不同点在于它们各自的配置文件:

  

  server1的zoo.cfg文件:

[html]  view plain  copy
  1. # The number of milliseconds of each tick  
  2. tickTime=2000  
  3. # The number of ticks that the initial   
  4. # synchronization phase can take  
  5. initLimit=10  
  6. # The number of ticks that can pass between   
  7. # sending a request and getting an acknowledgement  
  8. syncLimit=5  
  9. # the directory where the snapshot is stored.  
  10. # do not use /tmp for storage, /tmp here is just   
  11. # example sakes.  
  12. #dataDir=/tmp/zookeeper  
  13. dataDir=C:\\Users\\Administrator\\Desktop\\server1\\zookeeper-3.4.8\\zkdata  
  14. dataLogDir=C:\\Users\\Administrator\\Desktop\\server1\\zookeeper-3.4.8\\zkdatalog  
  15. # the port at which the clients will connect  
  16. clientPort=3181  
  17. # the maximum number of client connections.  
  18. # increase this if you need to handle more clients  
  19. #maxClientCnxns=60  
  20. #  
  21. # Be sure to read the maintenance section of the   
  22. # administrator guide before turning on autopurge.  
  23. #  
  24. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance  
  25. #  
  26. # The number of snapshots to retain in dataDir  
  27. #autopurge.snapRetainCount=3  
  28. # Purge task interval in hours  
  29. # Set to "0" to disable auto purge feature  
  30. #autopurge.purgeInterval=1  
  31. server.1=192.168.0.102:4181:5181  
  32. server.2=192.168.0.102:4182:5182  
  33. server.3=192.168.0.102:4183:5183  
server2的zoo.cfg文件:

[html]  view plain  copy
  1. # The number of milliseconds of each tick  
  2. tickTime=2000  
  3. # The number of ticks that the initial   
  4. # synchronization phase can take  
  5. initLimit=10  
  6. # The number of ticks that can pass between   
  7. # sending a request and getting an acknowledgement  
  8. syncLimit=5  
  9. # the directory where the snapshot is stored.  
  10. # do not use /tmp for storage, /tmp here is just   
  11. # example sakes.  
  12. #dataDir=/tmp/zookeeper  
  13. dataDir=C:\\Users\\Administrator\\Desktop\\server2\\zookeeper-3.4.8\\zkdata  
  14. dataLogDir=C:\\Users\\Administrator\\Desktop\\server2\\zookeeper-3.4.8\\zkdatalog  
  15. # the port at which the clients will connect  
  16. clientPort=3182  
  17. # the maximum number of client connections.  
  18. # increase this if you need to handle more clients  
  19. #maxClientCnxns=60  
  20. #  
  21. # Be sure to read the maintenance section of the   
  22. # administrator guide before turning on autopurge.  
  23. #  
  24. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance  
  25. #  
  26. # The number of snapshots to retain in dataDir  
  27. #autopurge.snapRetainCount=3  
  28. # Purge task interval in hours  
  29. # Set to "0" to disable auto purge feature  
  30. #autopurge.purgeInterval=1  
  31. server.1=192.168.0.102:4181:5181  
  32. server.2=192.168.0.102:4182:5182  
  33. server.3=192.168.0.102:4183:5183  
server3的zoo.cfg文件:
[html]  view plain  copy
  1. # The number of milliseconds of each tick  
  2. tickTime=2000  
  3. # The number of ticks that the initial   
  4. # synchronization phase can take  
  5. initLimit=10  
  6. # The number of ticks that can pass between   
  7. # sending a request and getting an acknowledgement  
  8. syncLimit=5  
  9. # the directory where the snapshot is stored.  
  10. # do not use /tmp for storage, /tmp here is just   
  11. # example sakes.  
  12. #dataDir=/tmp/zookeeper  
  13. dataDir=C:\\Users\\Administrator\\Desktop\\server3\\zookeeper-3.4.8\\zkdata  
  14. dataLogDir=C:\\Users\\Administrator\\Desktop\\server3\\zookeeper-3.4.8\\zkdatalog  
  15. # the port at which the clients will connect  
  16. clientPort=3183  
  17. # the maximum number of client connections.  
  18. # increase this if you need to handle more clients  
  19. #maxClientCnxns=60  
  20. #  
  21. # Be sure to read the maintenance section of the   
  22. # administrator guide before turning on autopurge.  
  23. #  
  24. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance  
  25. #  
  26. # The number of snapshots to retain in dataDir  
  27. #autopurge.snapRetainCount=3  
  28. # Purge task interval in hours  
  29. # Set to "0" to disable auto purge feature  
  30. #autopurge.purgeInterval=1  
  31. server.1=192.168.18.97:4181:5181  
  32. server.2=192.168.18.97:4182:5182  
  33. server.3=192.168.18.97:4183:5183  

注意:每一个server下的zoo.cfg内容中主要关注几个点,dataDir是zookeeper存放数据的地方,自己手动新建一个位置,比如这里是zkdata,将路径赋值给dataDir即可。同理dataLogDir。对于clientPort是客户端端口,在单IP多节点的这种方式每个clientPort不能一样。在最后server.1,server.2,server.3这是指定zookeeper服务器,第一个端口比如4181是指各个zookeeper之间的通信端口,5181是各个zookeeper的选举leader端口。还有最重要的一点是这个server.1还要在zkdata文件夹下新建一个没有后缀名的myid文件,里面填写1.同理,server.2是在zkdata文件夹下新建一个myid,里面填写2.以此类推,比如server.3的话,请看下图:


启动zookeeper集群:一个一个启动每一个zookeeper服务器:
先后进入每一个zookeeper服务器的bin目录下执行zkserver.cmd,在先启动的zookeeper会打印其他zookeeper服务器未启动的错误信息,直至到最后一个启动的时候就没有异常信息了。

启动:



三 kafka集群

1.配置server.properties文件:

因为模板里面内容挺多的,我就贴一个出来作为演示说明:

[html]  view plain  copy
  1. # Licensed to the Apache Software Foundation (ASF) under one or more  
  2. # contributor license agreements.  See the NOTICE file distributed with  
  3. # this work for additional information regarding copyright ownership.  
  4. # The ASF licenses this file to You under the Apache License, Version 2.0  
  5. # (the "License"); you may not use this file except in compliance with  
  6. # the License.  You may obtain a copy of the License at  
  7. #   
  8. #    http://www.apache.org/licenses/LICENSE-2.0  
  9. #   
  10. # Unless required by applicable law or agreed to in writing, software  
  11. # distributed under the License is distributed on an "AS IS" BASIS,  
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13. # See the License for the specific language governing permissions and  
  14. # limitations under the License.  
  15. # see kafka.server.KafkaConfig for additional details and defaults  
  16.   
  17. ############################# Server Basics #############################  
  18.   
  19. # The id of the broker. This must be set to a unique integer for each broker.  
  20. broker.id=1  
  21. broker.list=192.168.0.101:9093,192.168.0.102:9092  
  22. ############################# Socket Server Settings #############################  
  23.   
  24. # The port the socket server listens on  
  25. port=9093  
  26.   
  27. # Hostname the broker will bind to. If not set, the server will bind to all interfaces  
  28. #host.name=localhost  
  29.   
  30. # Hostname the broker will advertise to producers and consumers. If not set, it uses the  
  31. # value for "host.name" if configured.  Otherwise, it will use the value returned from  
  32. # java.net.InetAddress.getCanonicalHostName().  
  33. #advertised.host.name=<hostname routable by clients>  
  34.   
  35. # The port to publish to ZooKeeper for clients to use. If this is not set,  
  36. # it will publish the same port that the broker binds to.  
  37. #advertised.port=<port accessible by clients>  
  38.   
  39. # The number of threads handling network requests  
  40. num.network.threads=3  
  41.    
  42. # The number of threads doing disk I/O  
  43. num.io.threads=8  
  44.   
  45. # The send buffer (SO_SNDBUF) used by the socket server  
  46. socket.send.buffer.bytes=102400  
  47.   
  48. # The receive buffer (SO_RCVBUF) used by the socket server  
  49. socket.receive.buffer.bytes=102400  
  50.   
  51. # The maximum size of a request that the socket server will accept (protection against OOM)  
  52. socket.request.max.bytes=104857600  
  53.   
  54.   
  55. ############################# Log Basics #############################  
  56.   
  57. # A comma seperated list of directories under which to store log files  
  58. #log.dirs=/tmp/kafka-logs  
  59. log.dirs=G:\Tools\JAVA\Kafka\kafka_2.11-0.8.2.2\kafka_2.11-0.8.2.2\kafka-logs  
  60.   
  61. # The default number of log partitions per topic. More partitions allow greater  
  62. # parallelism for consumption, but this will also result in more files across  
  63. # the brokers.  
  64. num.partitions=3  
  65.   
  66. # The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.  
  67. # This value is recommended to be increased for installations with data dirs located in RAID array.  
  68. num.recovery.threads.per.data.dir=1  
  69.   
  70. ############################# Log Flush Policy #############################  
  71.   
  72. # Messages are immediately written to the filesystem but by default we only fsync() to sync  
  73. # the OS cache lazily. The following configurations control the flush of data to disk.   
  74. # There are a few important trade-offs here:  
  75. #    1. Durability: Unflushed data may be lost if you are not using replication.  
  76. #    2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.  
  77. #    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to exceessive seeks.   
  78. # The settings below allow one to configure the flush policy to flush data after a period of time or  
  79. # every N messages (or both). This can be done globally and overridden on a per-topic basis.  
  80.   
  81. # The number of messages to accept before forcing a flush of data to disk  
  82. #log.flush.interval.messages=10000  
  83.   
  84. # The maximum amount of time a message can sit in a log before we force a flush  
  85. #log.flush.interval.ms=1000  
  86.   
  87. ############################# Log Retention Policy #############################  
  88.   
  89. # The following configurations control the disposal of log segments. The policy can  
  90. # be set to delete segments after a period of time, or after a given size has accumulated.  
  91. # A segment will be deleted whenever *either* of these criteria are met. Deletion always happens  
  92. # from the end of the log.  
  93.   
  94. # The minimum age of a log file to be eligible for deletion  
  95. log.retention.hours=168  
  96.   
  97. # A size-based retention policy for logs. Segments are pruned from the log as long as the remaining  
  98. # segments don't drop below log.retention.bytes.  
  99. #log.retention.bytes=1073741824  
  100.   
  101. # The maximum size of a log segment file. When this size is reached a new log segment will be created.  
  102. log.segment.bytes=1073741824  
  103.   
  104. # The interval at which log segments are checked to see if they can be deleted according   
  105. # to the retention policies  
  106. log.retention.check.interval.ms=300000  
  107.   
  108. # By default the log cleaner is disabled and the log retention policy will default to just delete segments after their retention expires.  
  109. # If log.cleaner.enable=true is set the cleaner will be enabled and individual logs can then be marked for log compaction.  
  110. log.cleaner.enable=false  
  111.   
  112. ############################# Zookeeper #############################  
  113.   
  114. # Zookeeper connection string (see zookeeper docs for details).  
  115. # This is a comma separated host:port pairs, each corresponding to a zk  
  116. # server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".  
  117. # You can also append an optional chroot string to the urls to specify the  
  118. # root directory for all kafka znodes.  
  119. zookeeper.connect=192.168.0.102:3181,192.168.0.102:3182,192.168.0.102:3183  
  120.   
  121. # Timeout in ms for connecting to zookeeper  
  122. zookeeper.connection.timeout.ms=6000  

需要修改的几个地方:

broker.id=1
broker.list=192.168.0.101:9093,192.168.0.102:9092

port=9093

log.dirs=G:\Tools\JAVA\Kafka\kafka_2.11-0.8.2.2\kafka_2.11-0.8.2.2\kafka-logs

zookeeper.connect=192.168.0.102:3181,192.168.0.102:3182,192.168.0.102:3183

每一个kafka服务器都需要有一个唯一的broker.id,这个broker.list是kafka集群的地址。


2.启动kafka集群,创建topics

主机win10启动kafka:


虚拟机启动kafka并且创建一个带2个备份的topic(test_yzr_2017_03_29)



三 测试集群

1.消息提供者和消费者之间的消息流通测试

比如在虚拟机上创建一个消息提供者:

kafka-console-producer.bat --broker-list 192.168.0.102:9092,192.168.0.101:9093 --topic test_yzr_2017_03_29

在虚拟机和win10主机上都创建一个消息消费者:

kafka-console-consumer.bat --zookeeper 192.168.0.102:3181 --topic test_yzr_2017_03_29


2.集群的容错性:

首先查看topic的信息

kafka-topics.bat --list --zookeeper 192.168.0.102:3181

或是改成(kafka-topics.bat --list --zookeeper 192.168.0.102:3182  和kafka-topics.bat --list --zookeeper 192.168.0.102:3183都是可以的,因为zookeeper集群共享数据)

查看指定的topic的详细信息:

kafka-topics.bat --describe --zookeeper --192.168.0.102:3181 --topic test_yzr_2017_03_29


可以看到此时选举的leader是0,即就是虚拟机中的kafka服务器,现在把虚拟机的kafka服务器给干掉。

此时leader为变为1,消费者能继续消费。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值