Spring Boot容器无法连接到Kafka容器

时间:2018-01-13 23:49:21

标签: docker spring-boot apache-kafka docker-compose

我尝试将微服务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的{​​{1}}不起作用?

P.S。 0.0.0.0 ,因为docker

被修改

我在mac os环境中添加了docker-compose.yml,但它仍然无效

kafka

1 个答案:

答案 0 :(得分:6)

您需要将 Kafka 代理广告为kafka,这是所有链接容器的有效主机名(即客户端需要从 Kafka连接的主机名协议透视):

kafka:
  ...
  environment:
    KAFKA_ADVERTISED_HOST_NAME: kafka