gpt4 book ai didi

python - 无法通过Docker连接单节点Kafka服务器

转载 作者:行者123 更新时间:2023-12-01 01:49:04 27 4
gpt4 key购买 nike

我尝试通过 Docker 连接到单节点 Kafka 服务器,但收到以下错误:

%3|1529395526.480|FAIL|rdkafka#producer-1| [thrd:localhost:9092/bootstrap]: localhost:9092/bootstrap: Connect to ipv4#127.0.0.1:9092 failed: Connection refused
%3|1529395526.480|ERROR|rdkafka#producer-1| [thrd:localhost:9092/bootstrap]: localhost:9092/bootstrap: Connect to ipv4#127.0.0.1:9092 failed: Connection refused
%3|1529395526.480|ERROR|rdkafka#producer-1| [thrd:localhost:9092/bootstrap]: 1/1 brokers are down

docker-compose.yml文件内容如下:

version: '2'
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
network_mode: host
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
extra_hosts:
- "moby:127.0.0.1"

kafka:
image: confluentinc/cp-kafka:latest
network_mode: host
depends_on:
- zookeeper
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: localhost:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_ADVERTISED_HOSTNAME: kafka
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
extra_hosts:
- "moby:127.0.0.1"

schema_registry:
image: confluentinc/cp-schema-registry
hostname: schema_registry
depends_on:
- zookeeper
- kafka
ports:
- "8081:8081"
environment:
SCHEMA_REGISTRY_HOST_NAME: schema_registry
SCHEMA_REGISTRY_KAFKASTORE_CONNECTION_URL: '127.0.0.1:2181'

Dockerfile 内容如下:

FROM python:2

WORKDIR /kafkaproducerapp

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "./BackOffice_Producer.py" ]

我做错了什么?

最佳答案

你需要这个:

KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092

否则,Kafka 代理将告诉任何连接的人,可以在 localhost:9092 上找到它,这在其他容器中不起作用。在其他容器中,使用 kafka:29092 作为代理主机和端口,以及 zookeeper:2181 作为 Zookeeper。

从本地主机,您可以访问 9092 上的代理(假设您公开该端口)。

查看this docker-compose完整示例(来自 this repo )

关于python - 无法通过Docker连接单节点Kafka服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50923582/

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