Spring Boot 容器无法连接到 Kafka 容器
我正在尝试将微服务 Spring Boot
与 Kafka
一起使用,但是我的 Spring Boot
容器无法连接到 Kafka
容器.
I'm trying to use microservices Spring Boot
with Kafka
, but my Spring Boot
containers can not connect to the Kafka
container.
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
在 Producer 中:
application.properties
in Producer:
spring.kafka.producer.bootstrap-servers=kafka:9092
但是如果我在容器中运行 Kafka
并且在本地运行 Spring Boot
微服务,它就可以工作.
application.properties
在消费者中:
But if I run the Kafka
in a container and the Spring Boot
microservices locally it works.
application.properties
in Consumer:
spring.kafka.consumer.bootstrap-servers=0.0.0.0:9092
spring.kafka.consumer.group-id=WorkUnitApp
spring.kafka.consumer.topic=kafka_topic
application.properties
在 Producer 中:
application.properties
in Producer:
spring.kafka.producer.bootstrap-servers=0.0.0.0:9092
有什么问题,为什么 docker
中的 links
不起作用?
What's the problem, why does the links
from the docker
not work ?
附言0.0.0.0 因为 mac os
我在 docker-compose.yml
环境中添加了 kafka
但它仍然不起作用
I added in docker-compose.yml
environments to kafka
but it still does not work either
- KAFKA_ADVERTISED_PORT=9092
您需要将您的 Kafka 代理宣传为 kafka
,这是所有链接容器的有效主机名(即客户端需要从 Kafka 协议角度连接的主机名,所以 kafka:9092
是正确的,而不是 0.0.0.0
):
You need to advertise your Kafka broker as kafka
, which is the effective hostname for all linking containers (i.e. the hostname that the client needs to connect to from the Kafka protocol perspective, and so kafka:9092
is correct, not 0.0.0.0
):
kafka:
...
environment:
KAFKA_ADVERTISED_HOST_NAME: kafka