简体   繁体   English

Kafka 和 zookeeper docker 容器在主机上与我的 api 对话

[英]Kafka and zookeeper docker containers talking to my api on the host machine

step 1:\步骤1:\

I've followed some steps in this guide and have created a zookeeper container and connected it to a Kafka container so they can talk to each other via local ports.我已经按照本指南中的一些步骤创建了一个 zookeeper 容器并将其连接到 Kafka 容器,以便它们可以通过本地端口相互通信。 I've also created a topic "foo" with 1 partition and sent 42 messages with a docker command in there.我还创建了一个带有 1 个分区的主题“foo”,并在其中使用 docker 命令发送了 42 条消息。 As I understand there is a Kafka port on my local os (localhost:29092) which is exposed to producing and listening to messages.据我了解,我的本地操作系统(localhost:29092)上有一个 Kafka 端口,它可以生成和收听消息。

docker commands to run zookeeper container: docker 命令运行zookeeper容器:

docker run -d --net=host --name=zookeeper -e ZOOKEEPER_CLIENT_PORT=32181 confluentinc/cp-zookeeper:4.0.0   

docker commands to run kafka container: docker 命令运行 kafka 容器:

docker run -d --net=host --name=kafka -e KAFKA_ZOOKEEPER_CONNECT=localhost:32181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:29092 -e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 confluentinc/cp-kafka:4.0.0

在此处输入图像描述

step 2:\第2步:\

Next, I'm trying to connect my Kafka producer and consumer scripts in my Springboot boot API without any docker-composing to this port as follow (I'd like to prove if these two containers are able two work as a normal Kafka distributive on my host os):接下来,我试图在我的 Springboot 启动 API 中连接我的 Kafka 生产者和消费者脚本,而没有任何 docker-compose 到这个端口,如下所示(我想证明这两个容器是否能够作为一个正常的 Kafka 分布我的主机操作系统):

public class KafkaProducer {private static final String TOPIC = "foo";...}
public class KafkaConsumer { @KafkaListener(topics = "foo", groupId = "group1")...}

spring.kafka.producer.bootstrap-servers: localhost:29092
spring.kafka.consumer.bootstrap-servers: localhost:29092

as result, I'm getting these ERRORS:结果,我收到了这些错误:

2020-12-23 14:38:45.937  WARN 14056 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-group1-1, groupId=group1] Connection to node -1 (localhost/127.0.0.1:29092) could not be established. Broker may not be available.
2020-12-23 14:38:45.937  WARN 14056 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-group1-1, groupId=group1] Bootstrap broker localhost:29092 (id: -1 rack: null) disconnected.

Could you please tell me what I got wrong?你能告诉我我做错了什么吗? I'm completely new to Java, Kafka, and Docker.我对 Java、Kafka 和 Docker 完全陌生。 What should I read to understand it better?我应该阅读什么才能更好地理解它?

For some reason, it works with a new version of the guide.出于某种原因,它适用于新版本的指南。

https://docs.confluent.io/platform/current/quickstart/ce-docker-quickstart.html https://docs.confluent.io/platform/current/quickstart/ce-docker-quickstart.html

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

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