compose的kafka连接问题

ki1q1bka  于 2021-06-04  发布在  Kafka
关注(0)|答案(1)|浏览(496)

我有下面的docker compose yml,

version: '3.5'
services:

  zookeeper:
    container_name: kafka-zookeeper
    image: wurstmeister/zookeeper:3.4.6
    ports:
      - "2181:2181"
    networks:
      - my-network

  kafka:
    container_name: kafka-server
    image: wurstmeister/kafka:2.12-2.5.0
    ports:
      - "9092:9092"
    links:
      - zookeeper
    environment:
      KAFKA_LISTENERS: PLAINTEXT://:9092
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://127.0.0.1:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - my-network
networks:
  my-network:
    driver: bridge

我参考了下面的参考资料,
https://github.com/wurstmeister/kafka-docker/wiki/connectivity
我的spring boot application.yml文件如下,

kafka:
  bootstrap:
    url: 127.0.0.1:9092

错误是,

[taKafkaProducer] org.apache.kafka.clients.NetworkClient   : [Producer clientId=SensorDataKafkaProducer] Connection to node -1 (/127.0.0.1:9092) could not be established. Broker may not be available.

我可以通过客户端工具连接Kafka,并查看侦听器的详细信息 127.0.0.1:9092 ```
bd919e5a4df2 wurstmeister/kafka:2.12-2.5.0 "start-kafka.sh" About a minute ago Up About a minute 0.0.0.0:9092->9092/tcp kafka-server

9b94b7dac237 wurstmeister/zookeeper:3.4.6 "/bin/sh -c '/usr/sb…" About a minute ago Up About a minute 22/tcp, 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp kafka-zookeeper

配置有问题吗?
to94eoyn

to94eoyn1#

使用用户定义的网桥网络,容器可以通过名称或别名相互解析。因此,在您的示例中,application.yml应该指向kafka容器名称:

kafka:
  bootstrap:
    url: kafka-server:9092

此外,对于它,还可以按容器名称配置kafka播发侦听器:

KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka-server:9092

相关问题