B6: Add Producer để gửi message vào topic
public class KafkaProducerService
{
private readonly IProducer<string, string> _producer;
public KafkaProducerService(string bootstrapServers)
{
var config = new ProducerConfig { BootstrapServers = bootstrapServers };
_producer = new ProducerBuilder<string, string>(config).Build();
}
public async Task SendMessageAsync(string topic, EmailRequest emailRequest)
{
var message = new Message<string, string>
{
Key = emailRequest.RequestId,
Value = JsonSerializer.Serialize(emailRequest, typeof(EmailRequest))
};
await _producer.ProduceAsync(topic, message);
}
}