简体   繁体   English

如何通过套接字正确地与 Kafka Broker 通信?

[英]How to properly communicate to a Kafka Broker through a socket?

Recently I have been trying to create a client library for Kafka in JavaScript (similar to KafkaJS).最近我一直在尝试在JavaScript(类似于KafkaJS)中为Kafka创建一个客户端库。

I looked into how KafkaJS does it and I successfully fetched a messaged from a topic by opening a socket and writing the Kafka protocol.我研究了 KafkaJS 是如何做到的,并通过打开一个套接字并编写 Kafka 协议成功地从一个主题中获取了一条消息。

The issue I am facing is, right after I send the first request (Fetch) to the broker and receive an answer, the broker sends a FIN packet and ends the socket.我面临的问题是,在我向代理发送第一个请求(Fetch)并收到答案后,代理发送一个 FIN 数据包并结束套接字。

I think this behavior is not normal and I searched in the official documents and also in KafkaJS code, I couldn't find anything.我认为这种行为是不正常的,我在官方文档和 KafkaJS 代码中进行了搜索,我找不到任何东西。

const socket = new net.Socket();
socket.connect(9092, 'localhost');
socket.setKeepAlive(true, 60000);

socket.write(payload.buffer, 'binary');

//rest of the events (end, data, error) are here.

Kafka has its own TCP protocol, which is implemented in several Nodejs libraries. Kafka 有自己的 TCP 协议,该协议在多个 Nodejs 库中实现。 Would be best to use those rather than implementing it yourself, but if you need to, the first action is to send a bootstrap request, not the payload, then producer records are sent in batches, to specific topic partitions of certain brokers (even if there's only one broker).最好使用这些而不是自己实现,但如果需要,第一个操作是发送引导请求,而不是有效负载,然后将生产者记录分批发送到某些代理的特定主题分区(即使只有一个经纪人)。

Not sure about your FIN issue, but if that's what the server returns, then somehow other clients would be handling it不确定您的 FIN 问题,但如果这是服务器返回的内容,那么其他客户端会以某种方式处理它

Also, KafkaJS is a server side library.此外,KafkaJS 是一个服务器端库。 Ideally, you wouldn't have a client (browser) communicating directly to Kafka理想情况下,您不会让客户端(浏览器)直接与 Kafka 通信

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

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