RedHat6.5安装kafka单机
RedHat6.5安装kafka单机
我是王者鑫 发表于6个月前
RedHat6.5安装kafka单机
  • 发表于 6个月前
  • 阅读 3
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: 版本号:Redhat6.5    JDK1.8     zookeeper-3.4.6   kafka_2.11-0.8.2.1

版本号:

Redhat6.5    JDK1.8     zookeeper-3.4.6   kafka_2.11-0.8.2.1

1、软件环境

已经搭建好的zookeeper:  RedHat6.5安装zookeeper单机

软件版本kafka_2.11-0.8.2.1.tgz

官网下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/0.8.2.1/kafka_2.11-0.8.2.1.tgz

百度云盘下载地址:链接:http://pan.baidu.com/s/1qYdl3ys 密码:d9ti

2、创建目录并上传kafka压缩包

 
  1. #创建目录 
  2. mkdir /usr/local/kafka
  3. #创建kafka消息目录,主要存放kafka消息
  4. mkdir /usr/local/kafka/kafka-logs

把下载好的kafka_2.11-0.8.2.1.tgz压缩包上传到/usr/local/kafka目录下,并执行以下解压命令:

tar -zvxf  /usr/local/kafka/kafka_2.11-0.8.2.1.tgz

如图:

3、修改配置文件

3.1修改config/server.properties

进入到config目录

cd /usr/local/kafka/kafka_2.11-0.8.2.1/config

ls

如图:

主要关注:server.properties 这个文件即可,我们可以发现在目录下:

有很多文件,这里可以发现有Zookeeper文件,我们可以根据Kafka内带的zk集群来启动,但是建议使用独立的zk集群

 server.properties,参数的解释:

 
  1. broker.id=0  #当前机器在kafka机器里唯一标识,与zookeeper的myid一个意思,由于我使用独立zookeeper这里可以注释掉
  2. port=9092 #这个参数默认是关闭的,当前kafka对外提供服务的端口默认是9092
  3. #host.name=localhost #broker绑定的IP
  4. num.network.threads=3 #这个是broker进行网络处理的线程数
  5. num.io.threads=8 #这个是broker进行I/O处理的线程数 
  6. log.dirs=/tmp/kafka-logs #消息存放的目录,这个目录可以配置为“,”逗号分割的表达式,上面的num.io##3.threads要大于这个目录的个数这个目录,如果配置多个目录,新创建的topic他把消息持久化的地方是,当前以逗号分割的目录中,那个分区数最少就放那一个 
  7. socket.send.buffer.bytes=102400 #发送缓冲区buffer大小,数据不是一下子就发送的,先回存储到缓冲区了到达一定的大小后在发送,能提高性能
  8. socket.receive.buffer.bytes=102400 #kafka接收缓冲区大小,当数据到达一定大小后在序列化到磁盘
  9. socket.request.max.bytes=104857600 #这个参数是向kafka请求消息或者向kafka发送消息的请请求的最大数,这个值不能超过java的堆栈大小
  10. num.partitions=1 #默认的分区数,一个topic默认1个分区数
  11. log.retention.hours=168 #默认消息的最大持久化时间,168小时,7天 message.max.byte=5242880  #消息保存的最大值5M
  12. default.replication.factor=2  #kafka保存消息的副本数,如果一个副本失效了,另一个还可以继续提供服务
  13. replica.fetch.max.bytes=5242880  #取消息的最大直接数 log.segment.bytes=1073741824 #这个参数是:因为kafka的消息是以追加的形式落地到文件,当超过这个值的时候,kafka会新起一个文件
  14. log.retention.check.interval.ms=300000 #每隔300000毫秒去检查上面配置的log失效时间(log.retention.hours=168 ),到目录查看是否有过期的消息如果有,删除
  15. log.cleaner.enable=false #是否启用log压缩,一般不用启用,启用的话可以提高性能
  16. zookeeper.connect=localhost:2181 #设置zookeeper的连接端口

上面是参数的解释,master机器实际的修改项为: 

 
  1. host.name=192.168.168.200 #broker绑定的IP,要将其释放出来
  2. log.dirs=/usr/local/kafka/kafka-logs
  3. zookeeper.connect=192.168.168.200:2181

修改之后的完整的server.properties内容为:

 
  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=0
  21.  
  22. ############################# Socket Server Settings #############################
  23.  
  24. # The port the socket server listens on
  25. port=9092
  26.  
  27. # Hostname the broker will bind to. If not set, the server will bind to all interfaces
  28. host.name=192.168.168.200
  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=/usr/local/kafka/kafka-logs
  59.  
  60. # The default number of log partitions per topic. More partitions allow greater
  61. # parallelism for consumption, but this will also result in more files across
  62. # the brokers.
  63. num.partitions=1
  64.  
  65. # The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
  66. # This value is recommended to be increased for installations with data dirs located in RAID array.
  67. num.recovery.threads.per.data.dir=1
  68.  
  69. ############################# Log Flush Policy #############################
  70.  
  71. # Messages are immediately written to the filesystem but by default we only fsync() to sync
  72. # the OS cache lazily. The following configurations control the flush of data to disk.
  73. # There are a few important trade-offs here:
  74. # 1. Durability: Unflushed data may be lost if you are not using replication.
  75. # 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.
  76. # 3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to exceessive seeks.
  77. # The settings below allow one to configure the flush policy to flush data after a period of time or
  78. # every N messages (or both). This can be done globally and overridden on a per-topic basis.
  79.  
  80. # The number of messages to accept before forcing a flush of data to disk
  81. #log.flush.interval.messages=10000
  82.  
  83. # The maximum amount of time a message can sit in a log before we force a flush
  84. #log.flush.interval.ms=1000
  85.  
  86. ############################# Log Retention Policy #############################
  87.  
  88. # The following configurations control the disposal of log segments. The policy can
  89. # be set to delete segments after a period of time, or after a given size has accumulated.
  90. # A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
  91. # from the end of the log.
  92.  
  93. # The minimum age of a log file to be eligible for deletion
  94. log.retention.hours=168
  95.  
  96. # A size-based retention policy for logs. Segments are pruned from the log as long as the remaining
  97. # segments don't drop below log.retention.bytes.
  98. #log.retention.bytes=1073741824
  99.  
  100. # The maximum size of a log segment file. When this size is reached a new log segment will be created.
  101. log.segment.bytes=1073741824
  102.  
  103. # The interval at which log segments are checked to see if they can be deleted according
  104. # to the retention policies
  105. log.retention.check.interval.ms=300000
  106.  
  107. # By default the log cleaner is disabled and the log retention policy will default to just delete segments after their retention expires.
  108. # If log.cleaner.enable=true is set the cleaner will be enabled and individual logs can then be marked for log compaction.
  109. log.cleaner.enable=false
  110.  
  111. ############################# Zookeeper #############################
  112.  
  113. # Zookeeper connection string (see zookeeper docs for details).
  114. # This is a comma separated host:port pairs, each corresponding to a zk
  115. # server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
  116. # You can also append an optional chroot string to the urls to specify the
  117. # root directory for all kafka znodes.
  118. zookeeper.connect=192.168.168.200:2181
  119.  
  120. # Timeout in ms for connecting to zookeeper
  121. zookeeper.connection.timeout.ms=6000

3.2配置/etc/profile

sudo gedit /etc/profile

添加如下配置:

 
  1. #set kafka environment
  2. export KAFKA_HOME=/usr/local/kafka/kafka_2.11-0.8.2.1
  3. export PATH=$KAFKA_HOME/bin:$PATH

source /etc/profile

4、启动Kafka并测试

4.1启动zookeeper服务

 
  1. [root@master]# /usr/local/zookeeper/zookeeper-3.4.6/bin/zkServer.sh start

4.2启动Kafka服务

从后台启动Kafka

 
  1. #进入到kafka的bin目录
  2. cd /usr/local/kafka/kafka_2.11-0.8.2.1
  3. #启动kafka
  4. bin/kafka-server-start.sh config/server.properties &

 

 
  1. [root@master local]# jps
  2. 3584 Jps
  3. 3299 QuorumPeerMain
  4. 3519 Kafka

4.3 创建一个Topic实例

4.3.1 创建一个主题test

kafka-topics.sh --create --zookeeper 192.168.168.200:2181 --replication-factor 1 --partitions 1 --topic test

 
  1. [root@master 桌面]# kafka-topics.sh --create --zookeeper 192.168.168.200:2181 --replication-factor 1 --partitions 1 --topic test
  2. Created topic "test".

4.3.2 创建一个生产者

kafka-console-producer.sh --broker-list 192.168.168.200:9092 --topic test

此时控制台会捕获键盘值,当有换行键被按下表示一条消息被发送出去

 
  1. [root@master local]# kafka-console-producer.sh --broker-list 192.168.168.200:9092 --topic test
  2. [2017-07-11 20:54:43,465] WARN Property topic is not valid (kafka.utils.VerifiableProperties)
  3. test
  4. 6666
  5. success
  6. nice

在使用时提示:WARN Property topic is not valid (kafka.utils.VerifiableProperties),不影响正常使用,可忽略。

4.3.3 创建一个消费者

kafka-console-consumer.sh --zookeeper 192.168.168.200:2181 --topic test  --from-beginning

此时控制台会处于接收状态, 在生产者上输入信息回车之后,消费者上会同步出现发送过来的消息。

 
  1. [root@master local]# kafka-console-consumer.sh --zookeeper 192.168.168.200:2181 --topic test --from-beginning
  2. test
  3. 6666
  4. success
  5. nice

5关闭kafka命令

kafka-server-stop.sh

 

搭建完毕!!!

 

参考自:http://blog.csdn.net/sand_clock/article/details/67633433

标签: RedHat6.5 Kafka 单机
共有 人打赏支持
粉丝 0
博文 51
码字总数 50778
×
我是王者鑫
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: