簡體   English   中英

如何使用 Nest Js 將消息發送到 Kafka 中的自定義分區

[英]How to send message into custom partition in Kafka with Nest Js

我嘗試將消息發送到正確的分區這里是我的 NESTJS Producer Controller



 constructor(
    private readonly appService: AppService,
    @Inject('any_name_i_want') private readonly client: ClientKafka,
  ) {}

  async onModuleInit() {
    ['test2'].forEach((key) => this.client.subscribeToResponseOf(`${key}`));
    await this.client.connect();
  }

@Get('kafka-test-with-response')
  async testKafkaWithResponse() {
    const res = await this.client.send('test2', {
      foo: 'bar',
      data: new Date().toString(),
      partition: 1,
    });

    return res;
  }

我嘗試閱讀 NestJs 文檔,但它提供了 Kafka 網站的鏈接,我在 Kafka 文檔中找到了這個

const producer = kafka.producer()

await producer.connect()
await producer.send({
    topic: 'topic-name',
    messages: [
        { key: 'key1', value: 'hello world', partition: 0 },
        { key: 'key2', value: 'hey hey!', partition: 1 }
    ],
})

在我簽入 UI Kafka 后,在此處輸入圖像描述

Producer 好像還在發送隨機分區

我嘗試深入檢查 typescript send() function 但它使用任何所以我真的不確定如何在發送 function 中指定分區

NestJS 確實使用 KafkaJS,因此您找到的代碼部分是正確的,假設您可以獲得原始的 kafkajs 客戶端實例。

或者,Kafka 使用記錄鍵對每個事件進行分區,並且在 NestJS ProducerConfig 中,您可以指定一個分區器實例。 https://github.com/nestjs/nest/blob/master/packages/microservices/external/kafka.interface.ts#L104

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM