编辑 docker-compose.yml ,要使外网能访问docker内部的kafka,
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka001:9092,这个配置是关键
docker-compose.yml :
version: '2'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://:9092
KAFKA_LISTENERS: PLAINTEXT://:9092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka001:9092
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
volumes:
- /var/run/docker.sock:/var/run/docker.sock
启动之后会提示无法连接kafka001:9092的错误,docker ps查看容器id
docker exec -it 容器id /bin/bash,进入容器,编辑hosts指定ip
#vi hosts
192.168.100.22 kafka001
保存后重新启动容器,这样外部程序就可以访问kafka了
springboot 配置:
#kafka
# 指定kafka 代理地址,可以多个
spring.kafka.bootstrap-servers=kafka001:9092
# 指定listener 容器中的线程数,用于提高并发量
spring.kafka.listener.concurrency=1
# 每次批量发送消息的数量
spring.kafka.producer.batch-size=1000
# 指定默认消费者group id
spring.kafka.consumer.group-id=myGroup
# 指定默认topic id
spring.kafka.template.default-topic=topic-1
spring.kafka.consumer.group-id=test-consumer-group
*****记得windows也要配hosts