且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从本地计算机连接到Docker中运行的Kafka

更新时间:2022-12-23 12:10:34


免责声明:以下使用 confluentinc docker images,而不是 wurstmeister / kafa ,虽然有类似的配置,但我还没试过。同时 Docker Compose中的这一行

Disclaimer: The following uses confluentinc docker images, not wurstmeister/kafa, although there is a similar configuration, I have not tried it. Also this line in the Docker Compose.

Confluent快速入门文档假设所有生产和消费请求都在Docker网络中。

That Confluent quickstart document assumes all produce and consume requests will be within the Docker network.

您可以通过在自己的容器中运行Kafka客户端代码来解决问题,但是否则您需要添加一些环境变量以在外部公开容器,同时仍然在Docker网络中工作。

You could fix the problem by running your Kafka client code within its own container, but otherwise you'll need to add some more environment variables for exposing the container externally, while still having it work within the Docker network.

首先添加 PLAINTEXT_HOST:PLAINTEXT 的协议映射,它将侦听器协议映射到Kafka协议

First add a protocol mapping of PLAINTEXT_HOST:PLAINTEXT that will map the listener protocol to a Kafka protocol

-e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP = PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT

然后在不同的端口上设置两个广告的侦听器。 ( kafka:9092 这里指的是docker容器名称)。请注意,协议匹配上面映射的右侧值

Then setup two advertised listeners on different ports. (kafka:9092 here refers to the docker container name). Notice the protocols match the right side values of the mappings above

-e KAFKA_ADVERTISED_LISTENERS = PLAINTEXT:// kafka:9092,PLAINTEXT_HOST:// localhost :29092

运行容器时,添加 -p 29092:29092 主机端口映射

When running the container, add -p 29092:29092 for the host port mapping

当运行任何Kafka客户端外部 Docker网络时,请使用 localhost:29092

When running any Kafka Client outside the Docker network, use localhost:29092

在Docker网络中运行应用程序 时,请使用 kafka:9092

When running an app in the Docker network, use kafka:9092

请参阅示例撰写完整Confluent堆栈的文件