Apache Kafka 分布式消息队列中间件安装与配置 转载

时间:2023-03-09 21:43:01
Apache Kafka 分布式消息队列中间件安装与配置   转载

bin/zkServer.sh start /home/guym/down/kafka_2.8.0-0.8.0/config/zookeeper.properties&

bin/kafka-server-start.sh config/server.properties

bin/kafka-create-topic.sh --zookeeper localhost:2181 --replica 1 --partition 1 --topic mykafka

bin/kafka-list-topic.sh --zookeeper localhost:2181

bin/kafka-console-producer.sh --zookeeper 127.0.0.1:2181 --topic mykafka

bin/kafka-list-topic.sh --zookeeper localhost:2181

bin/kafka-console-producer.sh --zookeeper localhost:2181 --topic mykafka

本文地址: http://blog.csdn.net/wangjia184/article/details/37921183

本文演示从1个zookeeper+1个kafka broker到3个zookeeper+2个kafka broker集群的配置过程。

kafka依赖于zookeeper, 首先下载zookeeper和kafka

  1. $ wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
  2. $ gzip -d zookeeper-3.4.6.tar.gz
  3. $ tar -xvf zookeeper-3.4.6.tar
  4. $ wget http://apache.fayea.com/apache-mirror/kafka/0.8.1.1/kafka_2.8.0-0.8.1.1.tgz
  5. $ gtar xvzf kafka_2.8.0-0.8.1.1.tgz

对于CentOS来说在,在本地试验可能会遇到莫名其妙的问题,这一般是由于主机名不能正确识别导致。为了避免可能遇到的问题,首先查询本机主机名,

  1. $ hostname
  2. HOME

然后加入一条本地解析到/etc/hosts文件中

  1. 127.0.0.1      HOME

一个zookeeper  + 一个kafka broker的配置

将zookeeper/conf/下的zoo_sample.cfg改名成zoo.cfg。 zookeeper默认会读取此配置文件。配置文件暂时不用改,默认即可

  1. $mv zookeeper-3.4.6/conf/zoo_sample.cfg zookeeper-3.4.6/conf/zoo.cfg

启动Zookeeper服务, Zookeeper启动成功后在2181端口监听

  1. $ zookeeper-3.4.6/bin/zkServer.sh start
  2. JMX enabled by default
  3. Using config: /home/wj/event/zookeeper-3.4.6/bin/../conf/zoo.cfg
  4. Starting zookeeper ... STARTED

启动Kafka服务,启动成功后在9092端口监听

  1. $ kafka_2.8.0-0.8.1.1/bin/kafka-server-start.sh kafka_2.8.0-0.8.1.1/config/server.properties

开始测试

  1. # 连接zookeeper, 创建一个名为test的topic, replication-factor 和 partitions 后面会解释,先设置为1
  2. $ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
  3. Created topic "test".
  4. # 查看已经创建的topic列表
  5. $ bin/kafka-topics.sh --list --zookeeper localhost:2181
  6. test
  7. # 查看此topic的属性
  8. $ bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
  9. Topic:test  PartitionCount:1    ReplicationFactor:1 Configs:
  10. Topic: test Partition: 0    Leader: 0   Replicas: 0 Isr: 0
  11. # 生产者连接Kafka Broker发布一个消息
  12. $ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
  13. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  14. SLF4J: Defaulting to no-operation (NOP) logger implementation
  15. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  16. Hello World

消费者连接Zookeeper获取消息

  1. $ bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
  2. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  3. SLF4J: Defaulting to no-operation (NOP) logger implementation
  4. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  5. Hello World

一个zookeeper  + 两个kafka broker的配置



避免Kafka Broker的Singal-Point-Failure(单点失败), 需要建立多个Kafka
Broker。先将kakfa目录中的/config/server.properties复制为/config/server-
2.properties然后编辑它的内容, 具体见注释
  1. # The id of the broker. This must be set to a unique integer for each broker.
  2. broker.id=1  #每个Kafka Broker应该配置一个唯一的ID
  3. ############################# Socket Server Settings #############################
  4. # The port the socket server listens on
  5. port=19092   #因为是在同一台机器上开多个Broker,所以使用不同的端口号区分
  6. # Hostname the broker will bind to. If not set, the server will bind to all interfaces
  7. #host.name=localhost   #如果有多个网卡地址,也可以将不同的Broker绑定到不同的网卡
  8. ############################# Log Basics #############################
  9. # A comma seperated list of directories under which to store log files
  10. log.dirs=/tmp/kafka-logs-2  #因为是在同一台机器上开多个Broker,需要确保使用不同的日志目录来避免冲突

现在就可以用新建的配置文件启动这个一个Broker了

  1. $ kafka_2.8.0-0.8.1.1/bin/kafka-server-start.sh kafka_2.8.0-0.8.1.1/config/server-2.properties

现在新创建一个topic, replication-factor表示该topic需要在不同的broker中保存几份,这里replication-factor设置为2, 表示在两个broker中保存。

  1. $ kafka_2.8.0-0.8.1.1/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 1 --topic test2

然后查看此topic的属性。

  1. $ kafka_2.8.0-0.8.1.1/bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test2
  2. Topic:test2 PartitionCount:1    ReplicationFactor:2 Configs:
  3. Topic: test2    Partition: 0    Leader: 0   Replicas: 0,1   Isr: 0,1
  • Leader: 如果有多个brokerBroker保存同一个topic,那么同时只能有一个Broker负责该topic的读写,其它的Broker作为实时备份。负责读写的Broker称为Leader.
  • Replicas : 表示该topic的0分区在0号和1号broker中保存
  • Isr : 表示当前有效的broker, Isr是Replicas的子集

在test2这个topic下发布新的消息验证工作正常

  1. $ kafka_2.8.0-0.8.1.1/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test2
  2. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  3. SLF4J: Defaulting to no-operation (NOP) logger implementation
  4. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  5. HHH
  6. $ kafka_2.8.0-0.8.1.1/bin/kafka-console-consumer.sh --zookeeper lochost:2181 --from-beginning --topic test2
  7. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  8. SLF4J: Defaulting to no-operation (NOP) logger implementation
  9. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  10. HHH

现在杀掉第一个Broker,模拟此点的崩溃

  1. $ ps aux | grep server.properties
  2. user        2620  1.5  5.6 2082704 192424 pts/1  Sl+  08:57   0:25 java
  3. $ kill 2620

重新查询此topic的属性,会发现Leader已经进行了切换,而0号Broker也从Isr中消失了。

  1. $ kafka_2.8.0-0.8.1.1/bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test2
  2. Topic:test2 PartitionCount:1    ReplicationFactor:2 Configs:
  3. Topic: test2    Partition: 0    Leader: 1   Replicas: 0,1   Isr: 1
  1. $ kafka_2.8.0-0.8.1.1/bin/kafka-console-producer.sh --broker-list localhost:19092 --topic test2
  2. # 使用1号broker再发布一个消息到test2下
  3. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  4. SLF4J: Defaulting to no-operation (NOP) logger implementation
  5. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  6. Another message
  7. # 消费者查询,仍然工作正常
  8. $ kafka_2.8.0-0.8.1.1/bin/kafka-consolconsumer.sh --zookeeper localhost:2181 --from-beginning92 --topic test2
  9. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  10. SLF4J: Defaulting to no-operation (NOP) logger implementation
  11. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  12. HHH
  13. Another message

三个zookeeper + 两个kafka broker的配置



样,zookeeper也需要搭建cluster, 避免出现Single-Point-Failure.
由于zookeeper采用投票的方式来重新选举某节点失败后的leader,
所以至少需要三个zookeeper才能组成群集。且最好使用奇数个(而非偶数)。

下面是演示单机搭建最简单的zookeeper cluster, 具体的可以参考http://myjeeva.com/zookeeper-cluster-setup.html

  1. #!/bin/sh
  2. #下载
  3. wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
  4. gzip -d zookeeper-3.4.6.tar.gz
  5. tar xvf zookeeper-3.4.6.tar
  6. #重命名 zoo_sample.cfg 为 zoo.cfg
  7. mv zookeeper-3.4.6/conf/zoo_sample.cfg zookeeper-3.4.6/conf/zoo.cfg
  8. #新建一个目录
  9. sudo mkdir /usr/zookeeper-cluster
  10. sudo chown -R jerry:jerry /usr/zookeeper-cluster
  11. #3个子目录分别对应三个zookeeper服务
  12. mkdir /usr/zookeeper-cluster/server1
  13. mkdir /usr/zookeeper-cluster/server2
  14. mkdir /usr/zookeeper-cluster/server3
  15. #建立三个目录存放各自的数据文件
  16. mkdir /usr/zookeeper-cluster/data
  17. mkdir /usr/zookeeper-cluster/data/server1
  18. mkdir /usr/zookeeper-cluster/data/server2
  19. mkdir /usr/zookeeper-cluster/data/server3
  20. #建立三个目录存放各自的日志文件
  21. mkdir /usr/zookeeper-cluster/log
  22. mkdir /usr/zookeeper-cluster/log/server1
  23. mkdir /usr/zookeeper-cluster/log/server2
  24. mkdir /usr/zookeeper-cluster/log/server3
  25. #在每一个数据文件目录中,新建一个myid文件,文件必须是唯一的服务标识,在后面的配置中会用到
  26. echo '1' > /usr/zookeeper-cluster/data/server1/myid
  27. echo '2' > /usr/zookeeper-cluster/data/server2/myid
  28. echo '3' > /usr/zookeeper-cluster/data/server3/myid
  29. #将zookeeper复制三份
  30. cp -rf zookeeper-3.4.6/* /usr/zookeeper-cluster/server1
  31. cp -rf zookeeper-3.4.6/* /usr/zookeeper-cluster/server2
  32. cp -rf zookeeper-3.4.6/* /usr/zookeeper-cluster/server3

然后编辑每个zookeeper的zoo.cfg配置文件。
将dataDir和dataLogDir设置为各自独立的目录;然后保证clientPort不会和其它zookeeper冲突(因为这里演示是3个实例安装于一台服务器上)

最后加入下面几行

  1. server.1=0.0.0.0:2888:3888
  2. server.2=0.0.0.0:12888:13888
  3. server.3=0.0.0.0:22888:23888

server.X=IP:port1:port2

X是在该zookeeper数据文件目录中myid指定的服务ID.

IP是当前zookeeper绑定的IP地址,因为是演示,所以全都是localhost

port1 是Quorum Port

port2 是Leader Election Port

由于3个zookeeper在同一台机器上,需要使用不同的端口号避免冲突。

修改后的结果如下

/usr/zookeeper-cluster/server1/conf/zoo.cfg

  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=/usr/zookeeper-cluster/data/server1
  13. dataLogDir=/usr/zookeeper-cluster/log/server1
  14. # the port at which the clients will connect
  15. clientPort=2181
  16. # the maximum number of client connections.
  17. # increase this if you need to handle more clients
  18. #maxClientCnxns=60
  19. #
  20. # Be sure to read the maintenance section of the
  21. # administrator guide before turning on autopurge.
  22. #
  23. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  24. #
  25. # The number of snapshots to retain in dataDir
  26. #autopurge.snapRetainCount=3
  27. # Purge task interval in hours
  28. # Set to "0" to disable auto purge feature
  29. #autopurge.purgeInterval=1
  30. server.1=0.0.0.0:2888:3888
  31. server.2=0.0.0.0:12888:13888
  32. server.3=0.0.0.0:22888:23888

 /usr/zookeeper-cluster/server2/conf/zoo.cfg

  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=/usr/zookeeper-cluster/data/server2
  13. dataLogDir=/usr/zookeeper-cluster/log/server2
  14. # the port at which the clients will connect
  15. clientPort=12181
  16. # the maximum number of client connections.
  17. # increase this if you need to handle more clients
  18. #maxClientCnxns=60
  19. #
  20. # Be sure to read the maintenance section of the
  21. # administrator guide before turning on autopurge.
  22. #
  23. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  24. #
  25. # The number of snapshots to retain in dataDir
  26. #autopurge.snapRetainCount=3
  27. # Purge task interval in hours
  28. # Set to "0" to disable auto purge feature
  29. #autopurge.purgeInterval=1
  30. server.1=0.0.0.0:2888:3888
  31. server.2=0.0.0.0:12888:13888
  32. server.3=0.0.0.0:22888:23888

  /usr/zookeeper-cluster/server3/conf/zoo.cfg

  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=/usr/zookeeper-cluster/data/server3
  13. dataLogDir=/usr/zookeeper-cluster/log/server3
  14. # the port at which the clients will connect
  15. clientPort=22181
  16. # the maximum number of client connections.
  17. # increase this if you need to handle more clients
  18. #maxClientCnxns=60
  19. #
  20. # Be sure to read the maintenance section of the
  21. # administrator guide before turning on autopurge.
  22. #
  23. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  24. #
  25. # The number of snapshots to retain in dataDir
  26. #autopurge.snapRetainCount=3
  27. # Purge task interval in hours
  28. # Set to "0" to disable auto purge feature
  29. #autopurge.purgeInterval=1
  30. server.1=0.0.0.0:2888:3888
  31. server.2=0.0.0.0:12888:13888
  32. server.3=0.0.0.0:22888:23888

然后分别启动3个zookeeper服务

  1. $ /usr/zookeeper-cluster/server1/bin/zkServer.sh start
  2. JMX enabled by default
  3. Using config: /usr/zookeeper-cluster/server1/bin/../conf/zoo.cfg
  4. Starting zookeeper ... STARTED
  5. $ /usr/zookeeper-cluster/server2/bin/zkServer.sh start
  6. JMX enabled by default
  7. Using config: /usr/zookeeper-cluster/server2/bin/../conf/zoo.cfg
  8. Starting zookeeper ... STARTED
  9. $ /usr/zookeeper-cluster/server3/bin/zkServer.sh start
  10. JMX enabled by default
  11. Using config: /usr/zookeeper-cluster/server3/bin/../conf/zoo.cfg
  12. Starting zookeeper ... STARTED

启动完成后查看每个服务的状态,下面可以看到server2被选为了leader. 而其它2个服务为follower.

  1. $ /usr/zookeeper-cluster/server1/bin/zkServer.sh status
  2. JMX enabled by default
  3. Using config: /usr/zookeeper-cluster/server1/bin/../conf/zoo.cfg
  4. Mode: follower
  5. $ /usr/zookeeper-cluster/server2/bin/zkServer.sh status
  6. JMX enabled by default
  7. Using config: /usr/zookeeper-cluster/server2/bin/../conf/zoo.cfg
  8. Mode: leader
  9. $ /usr/zookeeper-cluster/server3/bin/zkServer.sh status
  10. JMX enabled by default
  11. Using config: /usr/zookeeper-cluster/server3/bin/../conf/zoo.cfg
  12. Mode: follower

接下来修改kafka的server.properties配置文件

kafka_2.8.0-0.8.1.1/config/server.properties和kafka_2.8.0-0.8.1.1/config/server-2.properties

将3个zookeeper的地址加入到zookeeper.connect中,如下:

  1. zookeeper.connect=localhost:2181,localhost:12181,localhost:22181

启动2个Kafka broker

  1. $ kafka_2.8.0-0.8.1.1/bin/kafka-server-start.sh kafka_2.8.0-0.8.1.1/config/server.properties
  2. $ kafka_2.8.0-0.8.1.1/bin/kafka-server-start.sh kafka_2.8.0-0.8.1.1/config/server-2.properties

接下来验证一下

  1. $ kafka_2.8.0-0.8.1.1/bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test3 --from-beginning
  2. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  3. SLF4J: Defaulting to no-operation (NOP) logger implementation
  4. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  5. fhsjdfhdsa
  6. fjdsljfdsadsfdas
  7. $ kafka_2.8.0-0.8.1.1/bin/kafka-console-consumer.sh --zookeepecalhost:12181 --topic test3 --from-beginning
  8. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  9. SLF4J: Defaulting to no-operation (NOP) logger implementation
  10. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  11. fhsjdfhdsa
  12. fjdsljfdsadsfdas
  13. $ kafka_2.8.0-0.8.1.1/bin/kafka-console-consumer.sh --zookeepecalhost:22181 --topic test3 --from-beginning
  14. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  15. SLF4J: Defaulting to no-operation (NOP) logger implementation
  16. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  17. fhsjdfhdsa
  18. fjdsljfdsadsfdas

现在模拟leader挂掉的情况,直接将server2 的zookeeper杀掉

  1. $ ps aux | grep server2
  2. user     2493  1.0  1.8 1661116 53792 pts/0   Sl   14:46   0:02 java
  3. $ kill 2493

重新查询一次各zookeeper的状态,会发现leader发生了改变

  1. $ /usr/zookeeper-cluster/server3/bin/zkServer.sh status
  2. JMX enabled by default
  3. Using config: /usr/zookeeper-cluster/server3/bin/../conf/zoo.cfg
  4. Mode: leader
  5. $ /usr/zookeeper-cluster/server1/bin/zkServer.sh status
  6. JMX enabled by default
  7. Using config: /usr/zookeeper-cluster/server1/bin/../conf/zoo.cfg
  8. Mode: follower
  9. $ /usr/zookeeper-cluster/server2/bin/zkServer.sh status
  10. JMX enabled by default
  11. Using config: /usr/zookeeper-cluster/server2/bin/../conf/zoo.cfg
  12. Error contacting service. It is probably not running.

再次验证,kafka集群仍然工作正常。

  1. $ kafka_2.8.0-0.8.1.1/bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test3 --from-beginning
  2. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  3. SLF4J: Defaulting to no-operation (NOP) logger implementation
  4. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  5. fhsjdfhdsa
  6. fjdsljfdsadsfdas
  7. $ kafka_2.8.0-0.8.1.1/bin/kafka-console-consumer.sh --zookeepecalhost:22181 --topic test3 --from-beginning
  8. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  9. SLF4J: Defaulting to no-operation (NOP) logger implementation
  10. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  11. fhsjdfhdsa
  12. fjdsljfdsadsfdas
  13. $ kafka_2.8.0-0.8.1.1/bin/katopics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 2 --topic test5
  14. Created topic "test5".
  15. $ kafka_2.8.0-0.8.1.1/bin/katopics.sh --create --zookeeper localhost:22181 --replication-factor 2 --partitions 2 --topic test6
  16. Created topic "test6".

转载 本文地址: http://blog.csdn.net/wangjia184/article/details/37921183