LearningKafka学习笔记(二) - 18965050/learning-kafka GitHub Wiki
Kafka集群组成元素
- topic: topic是消息生成者发送消息的去处. 在Kafka中,topic被进行了分区(partition),每个分区都是有序且不可变(ordered and immutable)的消息集合.每个消息在分区中有个唯一标识符,称为offset
- broker: kafka集群包含一个或多个代理(broker). 每个broker中又有一个或多个服务进程.topic是在broker上下文中创建的. (broker的概念类似于JMS规范的broker)
- zookeeper: zookeeper在kafka中用于协调管理broker和consumer. zk保存协调数据, 比如状态信息,配置,位置信息等等
- producer: 生产者用于向选定的topic分区发送消息.分区选择可以是轮询的(round-robin)或是自定义的
- consumer:消费者用于从topic中接收数据并反馈给生产者
single node-single broker cluster

步骤
- 启动zk server:
bin/zookeeper-server-start.sh config/zookeeper.properties

- 启动broker:
bin/kafka-server-start.sh config/server.properties

broker配置文件server.properties中有如下配置
- broker.id: 每个broker的配置不能相同
- host.name: 主机名
- advertised.host.name:供外网访问, 可配置为外网能访问的ip
- port: server监听端口
- log.dir: 逗号分隔列表.用于日志文件存储
- num.partitons: 每个topic的分区数
- zookeeper.connect:zk连接字符串
- 创建topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic kafkatopic
创建后可通过
bin/kafka-topics.sh --list --zookeeper localhost:2181
来进行查看

- 发送message
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic kafkatopic
并在屏幕输入
Welcome to Kafka
This is a single broker cluster
相应的配置文件为producer.properties

- 接收message
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic kafkatopic --from-beginning
相应的配置文件为consumer.properties
可收到响应:

single node-multiple broker cluster

步骤
- 启动zk server
- 启动broker. 注意broker配置文件server.properties如下配置项需要不同
-
broker.id
-
port
-
log.dir 比如对于3个broker,可创建3个不同的broker配置文件(server-1.properties,server-2.properties,server-3.properties)
broker.id=1 port=9092 log.dir=/tmp/kafka-logs-1[server-1.properties]
broker.id=2 port=9093 log.dir=/tmp/kafka-logs-2[server-2.properties]
broker.id=3 port=9094 log.dir=/tmp/kafka-logs-3[server-3.properties]
-
- 创建topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 2 --partitions 1 --topic
- 发送消息
bin/kafka-console-producer.sh --broker-list localhost:9092, localhost:9093 --topic replicated-kafkatopic
- 接收消息
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic replicated-kafkatopic
multiple node-multiple broker cluster
步骤略
broker属性
