简体   繁体   English

Spring 应用程序在同一个集群内运行时无法连接到 Kafka,但在从集群外运行时可以工作

[英]Spring Application cannot connect to Kafka when it's run inside of the same cluster, but works when it runs from outside cluster

Spring application is working fine when it runs locally on my machine and accesses Kafka through docker, but it doesn't work when I add my Spring Application as a container inside of the cluster. Spring 应用程序在我的机器上本地运行并通过 docker 访问 Kafka 时工作正常,但是当我将 Spring 应用程序添加为集群内的容器时它不起作用。 I get error message: "Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available"我收到错误消息:“无法建立到节点 -1 (localhost/127.0.0.1:9092) 的连接。代理可能不可用”

Listed below is the docker-compose, dockerfile, and application.properties of the spring application.下面列出的是 docker-compose、dockerfile 和 spring 应用程序的 application.properties。

docker-compose.yml docker-compose.yml

version: "3"

services:
  zookeeper:
    image: 'bitnami/zookeeper:latest'
    ports:
      - '2181:2181'
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
  kafka:
    image: 'bitnami/kafka:latest'
    ports:
      - '9092:9092'
    environment:
      - KAFKA_BROKER_ID=1
      - KAFKA_LISTENERS=PLAINTEXT://:9092
      - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://127.0.0.1:9092
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
      - ALLOW_PLAINTEXT_LISTENER=yes
    depends_on:
      - zookeeper
  app:
    image: 'someuser/imagename'
    ports:
      - '8080:8080'
    depends_on:
      - kafka

applications.properties应用程序.properties

server.port = 8080
spring.kafka.consumer.bootstrap.servers=localhost:9092
spring.kafka.consumer.group-id=mygroup 
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consume.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer

spring.kafka.producer.bootstrap-servers=localhost:9092
spring.kafka.producer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.producer.value-deserialzier=org.apache.kafka.common.serialization.StringDeserializer

Dockerfile Dockerfile

FROM openjdk:17

WORKDIR /app

EXPOSE 8080
EXPOSE 9092

COPY .mvn/ .mvn
COPY mvnw pom.xml ./
RUN ./mvnw dependency:go-offline

COPY src ./src

CMD ["./mvnw", "spring-boot:run"]

With spring.kafka.producer.bootstrap-servers=localhost:9092 and spring.kafka.consumer.bootstrap.servers=localhost:9092 your application is technically calling it's own container on port 9092 and not the one that kafka is deployed on.使用 spring.kafka.producer.bootstrap-servers=localhost:9092 和 spring.kafka.consumer.bootstrap.servers=localhost:9092 您的应用程序在技术上是部署在端口 9 上的,而不是部署在端口 9 上的自己的容器。

If you change localhost:9092 to kafka:9092 it should work ( https://docs.docker.com/compose/networking/ ).如果您将 localhost:9092 更改为 kafka:9092 它应该可以工作( https://docs.docker.com/compose/networking/ )。 (locally it wont, but it should connect when deployed through docker) (本地不会,但通过 docker 部署时应该连接)

Try setting a different application profile for local and docker deployments.尝试为本地和 docker 部署设置不同的应用程序配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM