单节点Broker部署(zookeeper为伪集群模式,已启动)
# vim ../config/server.properties
broker.id=0
listeners=PLAINTEXT://192.168.2.10:9092
log.dirs=/data/logs/kafka-logs
zookeeper.connect=192.168.2.10:2181,192.168.2.10:2182,192.168.2.10:2183
启动kafka
# mkdir -pv /data/logs/kafka-logs
# ./kafka-server-start.sh -daemon ../config/server.properties
测试
## 创建一个主题
./kafka-topics.sh --create --zookeeper 192.168.2.10:2181 --replication-factor 1 --partitions 1 --topic test
Created topic "test"
## 查看主题
./kafka-topics.sh --list --zookeeper 192.168.2.10:2181
test
## 启动一个生产者,同时启动一个消费者(另起窗口)
./kafka-console-producer.sh --broker-list 192.168.2.10:9092 --topic topic-demo
>hello1
>hello2
>hello3
## 同时启动一个消费者
//生产者输入信息,消费者窗口就会输出对应信息
./kafka-console-consumer.sh --bootstrap-server 192.168.2.10:9092 --topic topic-demo --from-beginning
hello1
hello2
hello3
kafka集群模式(zookeeper为集群模式)
# vim /etc/hosts
192.168.2.101 slave1
192.168.2.102 slave2
192.168.2.103 slave3
以slave3配置为例,其他两台同理
# vim /usr/local/kafka/config/server.properties
broker.id=2 //slave1-3分别为0,1,2
listeners=PLAINTEXT://192.168.2.103:9092
advertised.listeners=PLAINTEXT://slave3:9092
log.dirs=/data/kafka-logs
num.partitions=2
offsets.topic.replication.factor=3
delete.topic.enable=true
auto.create.topics.enable = false
#如果上面开启true,则需要配置下面
default.replication.factor=3
zookeeper.connect=slave1:2181,slave2:2181,slave3:2181
启动kafka
# mkdir -pv /data/logs/kafka-logs
# ./kafka-server-start.sh -daemon ../config/server.properties
测试
创建主题
# ./kafka-topics.sh --create --zookeeper slave1:2181,slave2:2181,slave3:2181 --replication-factor 3 --partitions 3 --topic wangs898
查看主题
# ./kafka-topics.sh --list --zookeeper slave1:2181,slave2:2181,slave3:2181
删除主题
# ./kafka-topics.sh --delete --zookeeper slave1:2181,slave2:2181,slave3:2181 --topic wangs898
创建生产者
# ./kafka-console-producer.sh --broker-list slave1:9092,slave2:9092,slave3:9092 --topic wangs898
创建消费者
# ./kafka-console-consumer.sh --bootstrap-server bootstrap-servers=slave1:9092,slave2:9092,slave3:9092 --topic wangs898 --from-beginning