?错误代码
public async Task PublishAsync<TMessage>(string topicName, TMessage message) where TMessage : class
{
var config = new ProducerConfig
{
BootstrapServers = "21.28.34.229:2181"
};
using var producer = new ProducerBuilder<NULL, string>(config).Build();
await producer.ProduceAsync(topicName, new Message<NULL, string>
{
Value = message.ToString()
});
}
正确代码
public async Task PublishAsync<TMessage>(string topicName, TMessage message) where TMessage : class
{
var config = new ProducerConfig
{
BootstrapServers = "21.28.34.229:2181"
};
using var producer = new ProducerBuilder<string, string>(config).Build();
await producer.ProduceAsync(topicName, new Message<string, string>
{
Key = Guid.NewGuid().ToString(),
Value = message.ToString()
});
}
区别:
new ProducerBuilder时需要指定key。我这里采用随机生成
如果不指定。则默认所有数据全部进入一个分区。导致无论topic设置多少个分区都失效。只有一个消费者可以消费数据
|