如果你是开发或测试环境使用,可以使用内置 zookeeper
cd /usr/local/src wget http://apache.communilink.net/kafka/0.10.2.0/kafka_2.12-0.10.2.0.tgz tar zxvf kafka_2.12-0.10.2.0.tgz mv kafka_2.12-0.10.2.0 /srv/ cp /srv/kafka_2.12-0.10.2.0/config/server.properties{,.original} echo "advertised.host.name=localhost" >> /srv/kafka_2.12-0.10.2.0/config/server.properties ln -s /srv/kafka_2.12-0.10.2.0 /srv/kafka /srv/kafka/bin/zookeeper-server-start.sh config/zookeeper.properties /srv/kafka/bin/kafka-server-start.sh /srv/kafka/config/server.properties
启动 Kafka 服务
/srv/kafka/bin/zookeeper-server-start.sh -daemon /srv/kafka/config/zookeeper.properties /srv/kafka/bin/kafka-server-start.sh -daemon /srv/kafka/config/server.properties
-daemon 表示守护进程方式在后台启动
停止 Kafka 服务
/srv/kafka/bin/kafka-server-stop.sh /srv/kafka/bin/zookeeper-server-stop.sh
如果是生产环境安装脚本如下,独立安装zookeeper.
#!/bin/bash cd /usr/local/src wget http://apache.communilink.net/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz tar zxvf zookeeper-3.4.9.tar.gz cp zookeeper-3.4.9/conf/zoo_sample.cfg zookeeper-3.4.9/conf/zoo.cfg vim zookeeper-3.4.9/conf/zoo.cfg mv zookeeper-3.4.9 /srv/ ln -s /srv/zookeeper-3.4.9 /srv/zookeeper #cd zookeeper-3.4.9 /srv/zookeeper/bin/zkServer.sh start cd /usr/local/src wget http://apache.communilink.net/kafka/0.10.2.0/kafka_2.12-0.10.2.0.tgz tar zxvf kafka_2.12-0.10.2.0.tgz mv kafka_2.12-0.10.2.0 /srv/ cp /srv/kafka_2.12-0.10.2.0/config/server.properties{,.original} echo "advertised.host.name=localhost" >> /srv/kafka_2.12-0.10.2.0/config/server.properties ln -s /srv/kafka_2.12-0.10.2.0 /srv/kafka /srv/kafka/bin/kafka-server-start.sh /srv/kafka/config/server.properties
启动 zookeeper
$ /srv/zookeeper/bin/zkServer.sh start
停止 zookeeper
$ /srv/zookeeper/bin/zkServer.sh stop ZooKeeper JMX enabled by default Using config: /srv/zookeeper/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED
$ cd /srv/kafka
创建Topic
$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test Created topic "test".
查看Topic
$ bin/kafka-topics.sh --list --zookeeper localhost:2181 test
启动Producer 生产消息
$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test This is a message This is another message
启动Consumer 消费消息
$ bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning This is a message This is another message
默认 kafka对localhost提供访问,如果开放外面的IP进来你需要配置 config/server.properties
listeners = PLAINTEXT://147.189.135.55:9092
以及
advertised.host.name=147.189.135.55
进入控制台
bin/zookeeper-shell.sh localhost:2181
删除Topic
$ /srv/kafka/bin/kafka-run-class.sh kafka.admin.TopicCommand --delete --topic kafkatopic --zookeeper localhost:2181
查看Topic 的 offset
$ /srv/kafka/bin/kafka-consumer-offset-checker.sh --zookeeper localhost:2181 --topic kafkatopic --group consumer
解决方法
echo "advertised.host.name=localhost" >> /srv/kafka/config/server.properties
root@VM_7_221_centos /srv/kafka % bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test Error while executing topic command : Replication factor: 1 larger than available brokers: 0. [2017-11-26 10:55:11,532] ERROR org.apache.kafka.common.errors.InvalidReplicationFactorException: Replication factor: 1 larger than available brokers: 0. (kafka.admin.TopicCommand$)
检查 broker.id 配置 broker.id 必须大于 0
root@netkiller /srv/kafka % cat config/server.properties | grep broker.id broker.id=1
Kafka 在防火墙后面,防火墙上面配置 NAT 规则映射到服务器
# bind 任何IP地址 listeners=PLAINTEXT://:9092 # Wan IP 地址 advertised.host.name=223.207.161.225
提示 | |
---|---|
修改 advertised.host.name 后要删除 /tmp/kafka-logs 中的日志文件,否则无论如何你你都难以配置成功 |
rm -rf /tmp/kafka-logs
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。