Spring Boot 容器无法连接到 Kafka 容器 [英] Spring Boot containers can not connect to the Kafka container

查看:106
本文介绍了Spring Boot 容器无法连接到 Kafka 容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将微服务 Spring BootKafka 一起使用,但是我的 Spring Boot 容器无法连接到 Kafka 容器.

I'm trying to use microservices Spring Boot with Kafka, but my Spring Boot containers can not connect to the Kafka container.

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 在 Producer 中:

application.properties in Producer:

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

但是如果我在容器中运行 Kafka 并且在本地运行 Spring Boot 微服务,它就可以工作.

application.properties 在消费者中:

But if I run the Kafka in a container and the Spring Boot microservices locally it works.

application.properties in Consumer:

spring.kafka.consumer.bootstrap-servers=0.0.0.0:9092
spring.kafka.consumer.group-id=WorkUnitApp
spring.kafka.consumer.topic=kafka_topic

application.properties 在 Producer 中:

application.properties in Producer:

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

有什么问题,为什么 docker 中的 links 不起作用?

What's the problem, why does the links from the docker not work ?

附言0.0.0.0 因为 mac os

我在 docker-compose.yml 环境中添加了 kafka 但它仍然不起作用

I added in docker-compose.yml environments to kafka but it still does not work either

  - KAFKA_ADVERTISED_PORT=9092

推荐答案

您需要将您的 Kafka 代理宣传为 kafka,这是所有链接容器的有效主机名(即客户端需要从 Kafka 协议角度连接的主机名,所以 kafka:9092 是正确的,而不是 0.0.0.0):

You need to advertise your Kafka broker as kafka, which is the effective hostname for all linking containers (i.e. the hostname that the client needs to connect to from the Kafka protocol perspective, and so kafka:9092 is correct, not 0.0.0.0):

kafka:
  ...
  environment:
    KAFKA_ADVERTISED_HOST_NAME: kafka

这篇关于Spring Boot 容器无法连接到 Kafka 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆