FirstJ
FirstJ

Reputation: 11

Spring Boot App refuses to connect to the Kafka brokers

So I just recently started on using Kafka and for some odd reason, although the kafka and zookeeper servers containers are running as intended on the documentation and by using the command docker ps. I keep getting this specific error

enter image description here

I have been trying to solve this issue for about a week now with different configs and yet it still keeps giving me the endless loop of an error.

I followed both of these tutorials: https://rmoff.net/2018/08/02/kafka-listeners-explained/ and https://www.youtube.com/watch?v=-0vrqMMGQbc

docker-compose up runs perfectly fine and the broker starts but when I start the mvn spring-boot:run command that's where it fails.

I've also tried changing the address through my local machine but still nothing. Would appreciate some help

This is my docker-compose.yml

version: '3.2'
services:
  zookeeper:
    container_name: zookeeper
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
  kafka:
    container_name: kafka
    image: wurstmeister/kafka
    ports:
      - "9092:9092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LISTENERS: INTERNAL://:19092,EXTERNAL://:9092
      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:19092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'false'
    depends_on:
      - zookeeper
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

And this is my application.yml

# Server
server:
  port: 8090

# Spring Kafka & H2
spring:
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration
  kafka:
    bootstrap-servers: localhost:9092
  h2:
    console:
      enabled: true
      path: /h2
  datasource:
    url: jdbc:h2:mem:api;MODE=MySQL
    username: api
    password: root
    driverClassName: org.h2.Driver

result:
  topic.name: result

# Keycloak
keycloak:
  auth-server-url: http://localhost:8080/auth
  realm: demo
  resource: demo-api
  enabled: false
  public-client: false
  bearer-only : true
  principal-attribute: preferred_username
  allow-any-hostname: true

#  credentials:
#    secret:

# Logging
logging:
  level:
    org.springframework.web: DEBUG
    org.hibernate: DEBUG
    org: INFO
    com.API.demo.Controllers: DEBUG
  config: src/main/resources/logback-spring.xml
  file:
    path: logs/log

Upvotes: 1

Views: 1125

Answers (1)

Robin Moffatt
Robin Moffatt

Reputation: 32050

The Advertised Listener is not defined correctly

Instead of

  KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:19092

you need

  KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:19092,EXTERNAL://localhost:9092

Also note that if you want the broker to create topics automagically you'll want to set

  KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'

Upvotes: 3

Related Questions