安装文档
https://blog.csdn.net/qq_22041375/article/details/106180415
安装 zookeeper
docker run -d --name zookeeper -p 2181:2181 -v /etc/localtime:/etc/localtime wurstmeister/zookeeper
安装 kafka
docker run -d –name kafka -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=10.9.44.11:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.9.44.11:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -t wurstmeister/kafka
docker run -d –name kafka -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=192.168.0.102:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.0.102:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -t wurstmeister/kafka
3参数意义
-e KAFKA_BROKER_ID=0 在kafka集群中,每个kafka都有一个BROKER_ID来区分自己
-e KAFKA_ZOOKEEPER_CONNECT=10.9.44.11:2181/kafka 配置zookeeper管理kafka的路径10.9.44.11:2181/kafka
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.9.44.11:9092 把kafka的地址端口注册给zookeeper
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 配置kafka的监听端口
-v /etc/localtime:/etc/localtime 容器时间同步虚拟机的时间
4.验证kafka是否可以使用
4.1 进入容器
docker exec -it kafka /bin/sh
####4.2 进入路径 对应版本号(根据修改) 2.11-2.0.0
cd /opt/kafka_2.11-2.0.0/bin
####4.3运行kafka生产者发送消息
./kafka-console-producer.sh –broker-list localhost:9092 –topic sun
####4.4 发送消息
{"datas":[{"channel":"","metric":"temperature","producer":"ijinus","sn":"IJA0101-00002245","time":"1543207156000","value":"80"}],"ver":"1.0"}
在 config/server.properties 中 配置项
listeners
listeners就是主要用来定义Kafka Broker的Listener的配置项,用于本机监听。
advertised.listeners
advertised.listeners参数的作用就是将Broker的Listener信息发布到Zookeeper供客户端使用监听,是真正的对外代理地址。如果没有设置,会用listeners。
外部连接访问这个,需要设置这个值,
如 java/spring 代码连接kafka ,如果不设置advertised.listeners,就连接不上 报错:
Connection to node 1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
# The address the socket server listens on. It will get the value returned from
# java.net.InetAddress.getCanonicalHostName() if not configured.
# FORMAT:
# listeners = listener_name://host_name:port
# EXAMPLE:
# listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092
# Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured. Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092
advertised.listeners=PLAINTEXT://192.168.20.10:9092 # 配置了本机ip
主机IP改变,修改配置文件
https://blog.csdn.net/xiaosannimei/article/details/107405278