高吞吐量的分布式发布订阅消息系统Kafka

一、Kafka概述 

二、Kafka相关术语

  • Broker
    Kafka集群包含一个或多个服务器,这种服务器被称为broker
  • Topic
    每条发布到Kafka集群的消息都有一个类别,这个类别被称为Topic。(物理上不同Topic的消息分开存储,逻辑上一个Topic的消息虽然保存于一个或多个broker上但用户只需指定消息的Topic即可生产或消费数据而不必关心数据存于何处)
  • Partition
    Partition是物理上的概念,每个Topic包含一个或多个Partition.
  • Producer
    负责发布消息到Kafka broker
  • Consumer
    消息消费者,向Kafka broker读取消息的客户端。
  • Consumer Group
    每个Consumer属于一个特定的Consumer Group(可为每个Consumer指定group name,若不指定group name则属于默认的group)。

二、Kafka下载及安装

     1、下载

1
wget http://apache.fayea.com/kafka/0.9.0.1/kafka_2.11-0.9.0.1.tgz

  2、安装

1
2
tar zxvf kafka_2. 11 - 0.9 . 0.1 .tgz
cd kafka_2. 11 - 0.9 . 0.1

  3、集群配置

         设定有两台服务器192.168.1.237、192.168.1.238,两台服务器各安装有两zookeeper,端口都为2181(zookeeper不再说明),每个服务器都为Kafka配置3个broker。

     3.1、server.properties配置

1
2
3
4
5
6
broker.id = 10
port = 9090
host.name= 192.168 . 1.237
advertised.host.name= 192.168 . 1.237
log.dirs=/tmp/kafka-logs/server0
zookeeper.connect= 192.168 . 1.237 : 2181 , 192.168 . 1.238 : 2181

  说明:host.name\advertised.host.name两个参数还是要配置为IP,否则会有各种各样的问题。

    3.2、server1.properties配置

1
cp config/servier.properties config/server1.properties<br>vim config/server1.properties
?
1
2
3
4
5
6
broker.id = 11
port = 9091
host.name= 192.168 . 1.237
advertised.host.name= 192.168 . 1.237
log.dirs=/tmp/kafka-logs/server1
zookeeper.connect= 192.168 . 1.237 : 2181 , 192.168 . 1.238 : 2181

   3.3、server2.properties配置

1
2
cp config/servier.properties config/server2.properties
vim config/server2.properties
?
1
2
3
4
5
6
broker.id = 12
port = 9092
host.name= 192.168 . 1.237
advertised.host.name= 192.168 . 1.237
log.dirs=/tmp/kafka-logs/server2
zookeeper.connect= 192.168 . 1.237 : 2181 , 192.168 . 1.238 : 2181

  说明:同一台服务器port、log.dirs不能相同,不同的服务器broker.id只要在一个集群中都不能相同。

      3.4、同理 另一台服务器的server.properties,server1.properties,server2.properties的broker.id分别为:20、21、22,port分别为:9090、9091、9092  其它:host.name=192.168.1.238、advertised.host.name=192.168.1.238

      3.5、启动

1
2
3
bin/kafka-server-start.sh config/server.properties &
bin/kafka-server-start.sh config/server1.properties &
bin/kafka-server-start.sh config/server2.properties &

  3.6、监控端口

1
2
3
4
netstat -tunpl |grep 2181
netstat -tunpl |grep 9090
netstat -tunpl |grep 9091
netstat -tunpl |grep 9092

  看一下这4个端口起来没有,并看一下iptables有没有加入这4个IP的启动,或要把iptables相关,否则JAVA连接不进来。

    四、测试

        4.1、创建Topic

1
bin/kafka-topics.sh --create --zookeeper 192.168 . 1.237 : 2181 --replication-factor 3 --partitions 1 --topic testTopic

    4.2、查看创建情况

?
1
bin/kafka-topics.sh --describe --zookeeper 192.168 . 1.237 : 2181 --topic testTopic

    4.3、生产者发送消息

1
bin/kafka-console-producer.sh --broker-list 192.168 . 1.237 : 9090 --topic testTopic

        4.4、消费都接收消息

1
bin/kafka-console-consumer.sh --zookeeper 192.168 . 1.237 : 2181 --from-beginning --topic testTopic

    4.5、检查consumer offset位置

1
bin/kafka-run- class .sh kafka.tools.ConsumerOffsetChecker --zkconnect 192.168 . 1.237 : 2181 --group testTopic

  五、遇到的问题

          1、运行一段时间报错

1
2
3
4
5
6
#
# There is insufficient memory for the Java Runtime Environment to continue .
# Native memory allocation (malloc) failed to allocate 986513408 bytes for committing reserved memory.
# An error report file with more information is saved as:
# //hs_err_pid6500.log
OpenJDK 64 -Bit Server VM warning: INFO: os::commit_memory( 0x00000000bad30000 , 986513408 , 0 ) failed; error= 'Cannot allocate memory' (errno= 12 )

      解决:

    you can adjust the JVM heap size by editing kafka-server-start.sh, zookeeper-server-start.shand so on:

1
export KAFKA_HEAP_OPTS= "-Xmx1G -Xms1G"

    The -Xms parameter specifies the minimum heap size. To get your server to at least start up, try changing it to use less memory. Given that you only             have 512M, you should change the maximum heap size (-Xmx) too:

1
export KAFKA_HEAP_OPTS= "-Xmx256M -Xms128M"

猜你喜欢

转载自www.linuxidc.com/Linux/2016-06/132067.htm