从Spring Boot应用程序连接到本地Bitnami Docker Kafka时出错

时间:2020-01-24 15:20:34

标签: docker apache-kafka

带有Spring Kafka(版本2.4)的Spring Boot(版本2.2)应用程序无法与从官方Bitnami Docker Kafka执行的docker-compose.yml(版本2)建立连接

version: '2'

services:
  zookeeper:
    image: 'bitnami/zookeeper:3'
    ports:
      - '2181:2181'
    volumes:
      - 'zookeeper_data:/bitnami'
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
  kafka:
    image: 'bitnami/kafka:2'
    ports:
      - '9092:9092'
    volumes:
      - 'kafka_data:/bitnami'
    environment:
      - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
      - ALLOW_PLAINTEXT_LISTENER=yes
    depends_on:
      - zookeeper

volumes:
  zookeeper_data:
    driver: local
  kafka_data:
    driver: local

Spring应用程序不断产生以下警告:

[kafka-admin-client-thread | adminclient-1] WARN  o.apache.kafka.clients.NetworkClient.initiateConnect - [AdminClient clientId=adminclient-1] Error connecting to node 2228a9a3b8c5:9092 (id: 1001 rack: null) java.net.UnknownHostException: 2228a9a3b8c5

[kafka-admin-client-thread | adminclient-1] WARN  o.apache.kafka.clients.NetworkClient.processDisconnection - [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.

1 个答案:

答案 0 :(得分:3)

要与在`localhost上运行的Bitnami Docker Kafka建立连接,请添加以下环境变量:

  • KAFKA_CFG_LISTENERS=PLAINTEXT://:9092
  • KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092

docker-compose.yml kafka 服务:

kafka:
  image: 'bitnami/kafka:2'
  ports:
    - '9092:9092'
  volumes:
    - 'kafka_data:/bitnami'
  environment:
    - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
    - ALLOW_PLAINTEXT_LISTENER=yes
    - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092
    - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092