spring引导容器无法连接到kafka容器

63lcw9qa  于 2021-06-07  发布在  Kafka
关注(0)|答案(1)|浏览(361)

我在尝试使用微服务 Spring BootKafka ,但是我的 Spring Boot 容器无法连接到 Kafka 容器。 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` 消费者:

spring.kafka.consumer.bootstrap-servers=kafka:9092
spring.kafka.consumer.group-id=WorkUnitApp
spring.kafka.consumer.topic=kafka_topic
``` application.properties 生产商:

spring.kafka.producer.bootstrap-servers=kafka:9092

但是如果我在一个容器中运行kafka,而spring-boot微服务在本地运行,它就可以工作了。 application.properties 消费者:

spring.kafka.consumer.bootstrap-servers=0.0.0.0:9092
spring.kafka.consumer.group-id=WorkUnitApp
spring.kafka.consumer.topic=kafka_topic
``` `application.properties` 生产商:

spring.kafka.producer.bootstrap-servers=0.0.0.0:9092

有什么问题,为什么 `links` 从 `docker` 不工作?
p、 因为 `mac os` ##编辑
我补充道 `docker-compose.yml` 环境 `kafka` 但它仍然不起作用
  • KAFKA_ADVERTISED_PORT=9092
8nuwlpux

8nuwlpux1#

你需要宣传你的Kafka经纪人 kafka ,它是所有链接容器的有效主机名(即,从kafka协议的Angular 来看,客户端需要连接到的主机名,等等) kafka:9092 是正确的,不是 0.0.0.0 ):

kafka:
  ...
  environment:
    KAFKA_ADVERTISED_HOST_NAME: kafka

相关问题