Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node-RED 节点之 Kafka 消息中间件 #67

Open
giscafer opened this issue Jul 24, 2023 · 0 comments
Open

Node-RED 节点之 Kafka 消息中间件 #67

giscafer opened this issue Jul 24, 2023 · 0 comments
Labels

Comments

@giscafer
Copy link
Owner

因项目需求需要测试 Node-RED 节点数据发送到 Kafka,本人首次玩 Kafka,通过一次本地部署的折腾了解,然后再创建腾讯云的 CKafka 服务,最终在 Node.js 和 Node-RED 节点上测试成功。

一、本地 Docker 部署 Kafka

使用 docker-compose.yml 去部署

Kafka v3.5 版本是不依赖 zookeeper 的

curl -sSL https://raw.githubusercontent.com/bitnami/containers/main/bitnami/kafka/docker-compose.yml > docker-compose.yml
docker-compose up -d

1.测试 Kafka 服务

进入容器 docker exec -it {容器名称或ID} /bin/bash

docker exec -it kafka_kafka_1 /bin/bash

1.1 创建 Topic

/opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test

查看 Topic

/opt/bitnami/kafka/bin/kafka-topics.sh --list --bootstrap-server localhost:9092

1.2 创建生产者

创建生产者发送消息

/opt/bitnami/kafka/bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test

1.3 创建消费者

此时发消息的地方有新增数据时,消费者处会实时获取到。

/opt/bitnami/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning

整体输出如下

giscafer!@d891b053607f:/$ /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
Created topic test.
giscafer!@d891b053607f:/$ /opt/bitnami/kafka/bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test
>
>Hello kafka
>exit

giscafer!@d891b053607f:/$ /opt/bitnami/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning

Hello kafka
exit

1.4 修改 server.properties

拷贝 kafka 配置出来,按需修改

docker cp {容器名称或ID}doc:/opt/bitnami/kafka/config/server.properties d:/server.properties

本地修改完成后

docker cp d:/server.properties {容器名称或ID}doc:/opt/bitnami/kafka/config/server.properties

2. Kafka 其他异常

https://zhuanlan.zhihu.com/p/426913642

二、腾讯云 CKafka

https://cloud.tencent.com/product/ckafka

可以在腾讯云上申请免费试用 CKafka 服务,体验 Cloud Kafka,可以省去本地部署和配置的麻烦。

1.1 Node.js 连接 Kafka

nodejs 模块:https://github.com/Blizzard/node-rdkafka

根据文档公网 SASL_PLAINTEXT 方式接入,去联调测试 CKafka 服务

1.2 Node-RED 节点

可以使用 Node-RED 的节点node-red-contrib-rdkafka进行测试

在腾讯云发送消息Node-RED接收

在腾讯云发送消息Node-RED接收

Node-RED模拟Producer

Node-RED模拟Producer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant