gpt4 book ai didi

java - 无法从容器外部运行的 Spring Boot 应用程序连接到容器中运行的 Kafka

转载 作者:行者123 更新时间:2023-11-30 06:42:45 25 4
gpt4 key购买 nike

<分区>

我通过以下方式在本地运行 kafka:

docker-compose.yml

  zookeeper:
image: 'bitnami/zookeeper:latest'
ports:
- 2181:2181
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
kafka:
image: 'bitnami/kafka:latest'
ports:
- 9092:9092
environment:
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_ADVERTISED_PORT=9092
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092

我的 Spring Boot 应用程序运行在:

应用程序.yml:

spring:
application:
name: testkafka
kafka:
bootstrap-servers: localhost:9092

server:
port: 8080

当我运行它并尝试发送到 kafka 上的主题时,我得到:

org.springframework.kafka.KafkaException: Reply timed out
at org.springframework.kafka.requestreply.ReplyingKafkaTemplate.lambda$sendAndReceive$0(ReplyingKafkaTemplate.java:196) ~[spring-kafka-2.1.10.RELEASE.jar:2.1.10.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264) ~[na:na]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:na]
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]

如果我从 docker 容器内部运行 spring boot(使用一个 docker compose 文件),那么它工作:

在同一个 Compose 中运行:

version: "3.0"
services:
service1:
build: ./Service
ports:
- 8080:8080
- 5005:5005
links:
- zookeeper
- kafka
zookeeper:
image: 'bitnami/zookeeper:latest'
ports:
- 2181:2181
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
kafka:
image: 'bitnami/kafka:latest'
ports:
- 9092:9092
environment:
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_ADVERTISED_PORT=9092
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092

如何让 kafka 容器允许来自自身/docker 外部的连接?

编辑:尝试了这些更改:

kafka:
image: 'bitnami/kafka:latest'
ports:
- 9092:9092
environment:
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_ADVERTISED_PORT=9092
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092

和:

spring:
application:
name: testkafka
kafka:
bootstrap-servers: kafka:9092

server:
port: 8080

这仍然超时

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com