如果你是开发或测试环境使用,可以使用内置 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 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
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。