Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot containers can not connect to the Kafka container

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

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 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

Edited

I added in docker-compose.yml environments to kafka but it still does not work either

  - KAFKA_ADVERTISED_PORT=9092
like image 610
tsarenkotxt Avatar asked Jan 13 '18 23:01

tsarenkotxt


1 Answers

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
like image 121
Szymon Jednac Avatar answered Oct 17 '22 07:10

Szymon Jednac