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

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

问题描述

我正在尝试将微服务 Spring Boot Kafka 一起使用,但是我的 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 :

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 :

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

问题是什么,为什么 docker 中的链接不起作用?

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

p.s. 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天全站免登陆