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 in Consumer:
spring.kafka.consumer.bootstrap-servers=kafka:9092
spring.kafka.consumer.group-id=WorkUnitApp
spring.kafka.consumer.topic=kafka_topic
application.properties in Producer:
spring.kafka.producer.bootstrap-servers=kafka:9092
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 in Producer:
spring.kafka.producer.bootstrap-servers=0.0.0.0:9092
What's the problem, why does the links from the docker not work ?
p.s. 0.0.0.0 because mac os
I added in docker-compose.yml environments to kafka but it still does not work either
- KAFKA_ADVERTISED_PORT=9092
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With