我尝试将微服务Spring Boot
与Kafka
一起使用,但我的Spring Boot
容器无法连接到Kafka
容器。
docker-compose.yml
:
version: '3'
services:
zookeeper:
image: wurstmeister/zookeeper
container_name: zookeeper
restart: always
ports:
- 2181:2181
kafka:
image: wurstmeister/kafka
container_name: kafka
restart: always
ports:
- 9092:9092
depends_on:
- zookeeper
links:
- zookeeper:zookeeper
environment:
KAFKA_ADVERTISED_HOST_NAME: localhost
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
consumer:
image: consumer
container_name: consumer
depends_on:
- kafka
restart: always
ports:
- 8084:8080
depends_on:
- kafka
links:
- kafka:kafka
producer:
image: producer
container_name: producer
depends_on:
- kafka
restart: always
ports:
- 8085:8080
depends_on:
- kafka
links:
- kafka:kafka
消费者中的 application.properties
:
spring.kafka.consumer.bootstrap-servers=kafka:9092
spring.kafka.consumer.group-id=WorkUnitApp
spring.kafka.consumer.topic=kafka_topic
制片人中的 application.properties
:
spring.kafka.producer.bootstrap-servers=kafka:9092
Kafka
并且本地Spring Boot
微服务就可以运行。 application.properties
:
spring.kafka.consumer.bootstrap-servers=0.0.0.0:9092
spring.kafka.consumer.group-id=WorkUnitApp
spring.kafka.consumer.topic=kafka_topic
制片人中的 application.properties
:
spring.kafka.producer.bootstrap-servers=0.0.0.0:9092
问题是什么,为什么links
的{{1}}不起作用?
P.S。 0.0.0.0 ,因为docker
我在mac os
环境中添加了docker-compose.yml
,但它仍然无效
kafka
答案 0 :(得分:6)
您需要将 Kafka 代理广告为kafka
,这是所有链接容器的有效主机名(即客户端需要从 Kafka连接的主机名协议透视):
kafka:
...
environment:
KAFKA_ADVERTISED_HOST_NAME: kafka