0

I'm try to run my app within a docker container, I want my app to communicate with kafka topic but I am getting errors during execution, like:

Connection to node 1001 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.

What am I doing wrong?

Following the config:

Docker file:

FROM openjdk:11-jre-slim
#WORKDIR /usr/local/java
SHELL ["bash", "-c"]
ADD target/integration-service-exec.jar app.jar
ADD entrypoint.sh entrypoint.sh
RUN chmod +x /entrypoint.sh
RUN touch /app.jar
# Install prerequisites
RUN apt-get update && apt-get install -y curl jq wget
RUN wget -O dd-java-agent.jar 'https://search.maven.org/classic/remote_content?g=com.datadoghq&a=dd-java-agent&v=0.103.0'
EXPOSE 8080
ENTRYPOINT ["/entrypoint.sh"]

docker-compose.yml:

version: '3.8'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    container_name: integration-service.zookeeper
    ports:
      - "2181:2181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

  kafka:
    image: wurstmeister/kafka
    container_name: integration-service.kafka
    ports:
      - "9092:9092"
      - "9094:9094"
    depends_on:
      - zookeeper
    environment:
      KAFKA_ZOOKEEPER_CONNECT: integration-service.zookeeper:2181
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_LISTENERS: INTERNAL://0.0.0.0:9094,OUTSIDE://0.0.0.0:9092
      KAFKA_ADVERTISED_LISTENERS: INTERNAL://integration-service.kafka:9094,OUTSIDE://localhost:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,OUTSIDE:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
  redis:
    image: bitnami/redis:latest
    ports:
      - 6379:6379
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
  application:
    container_name: app
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - kafka
      - redis
 

Spring boot -- application.yml

spring.cloud.stream:
  binders:
    kafkasip:
      type: kafka
      environment:
        spring.cloud.stream.kafka.binder:
          autoCreateTopics: true 
          autoAddPartitions: true
          healthTimeout: 10
          requiredAcks: 1
          minPartitionCount: 1
          replicationFactor: 1
          configuration:
            auto.offset.reset: earliest
          headerMapperBeanName: customHeaderMapper
          brokers: 0.0.0.0:9094
# tried also with :localhost:9092 but same error....

user1671106
  • 193
  • 1
  • 1
  • 9
  • 0.0.0.0 is not a valid bootstrap server address – OneCricketeer Feb 15 '23 at 15:08
  • That was one of many tests but the value I've put is localhost:9092 or integration-service.kafka:9094 – user1671106 Feb 16 '23 at 16:24
  • [Read the docs](https://cloud.spring.io/spring-cloud-stream-binder-kafka/spring-cloud-stream-binder-kafka.html). Your YAML isn't correct. Specifically, `kafkasip` is not a valid configuration, and you should not duplicate `spring.cloud.stream.kafka.binder` within `spring.cloud.stream.binders.kafkasip.environment`... From the very top, you would have `spring.cloud.stream:`, then `kafka.binder:` and **then** `brokers:`. – OneCricketeer Feb 16 '23 at 19:59
  • 1
    Fixed, the right address to set is: brokers: integration-service.kafka:9094 – user1671106 Feb 17 '23 at 09:54

0 Answers0