前提是先安装好JDK
- 下载kafka版本
http://kafka.apache.org/downloads
linux上执行下载:
wget https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.8.0/kafka_2.13-2.8.0.tgz
解压包:
tar -zxvf kafka_2.13-2.8.0.tgz
- zookeeper直接使用kafka中自带的,修改zookeeper配置
如果zookeeper是集群配置,zookeeper.properties增加下面参数
# limitations under the License.
# the directory where the snapshot is stored.
dataDir=/opt/kafka/kafka_2.13-2.8.0/data/zookeeper
dataLogDir=/opt/kafka/kafka_2.13-2.8.0/log/zookeeper
# the port at which the clients will connect
#clientPort=2181
# disable the per-ip limit on the number of connections since this is a non-production config
#maxClientCnxns=0
# Disable the adminserver by default to avoid port conflicts.
# Set the port to something non-conflicting if choosing to enable this
#admin.enableServer=false
# admin.serverPort=8080
clientPort=2181
maxClientCnxns=0
admin.enableServer=false
tickTime=2000
initLimit=10
syncLimit=5
server.0=3.1.1.1:2888:3888
server.1=3.1.1.2:2888:3888
#server.2=3.1.1.3:2888:3888
dataDir目录下echo 0 > myid,注意0对应sever.0中的0
- kafka server参数配置, server.properties,关键参数:
listeners=PLAINTEXT://:9092
log.dirs=/root/kafka/log
zookeeper.connect=localhost:2181
集群
broker.id=2 都不一样
zookeeper.connect=3.1.1.1:2181,3.1.1.2:2181 zookeeper都列上
-
启动kafka:
/root/kafka/kafka_2.13-2.8.0/bin/zookeeper-server-start.sh /root/kafka/kafka_2.13-2.8.0/config/zookeeper.properties &
/root/kafka/kafka_2.13-2.8.0/bin/kafka-server-start.sh /root/kafka/kafka_2.13-2.8.0/config/server.properties & -
创建topic
bin/kafka-topics.sh --create --zookeeper 127.0.0.1:2181 --replication-factor 1 --partitions 1 --topic test111 -
列出topicbin/kafka-topics.sh --list --zookeeper localhost:2181
-
测试生产者写消息
bin/kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic test111 -
测试消费者写消息
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic test111