34 lines
753 B
C#
34 lines
753 B
C#
|
using Confluent.Kafka;
|
|||
|
|
|||
|
namespace Cloud.Services
|
|||
|
{
|
|||
|
public class ProducerService
|
|||
|
{
|
|||
|
private readonly IProducer<string, string> _producer;
|
|||
|
|
|||
|
public ProducerService(IConfiguration configuration)
|
|||
|
{
|
|||
|
var producerConfig = new ProducerConfig
|
|||
|
{
|
|||
|
BootstrapServers = configuration["Kafka:BootstrapServers"]
|
|||
|
};
|
|||
|
|
|||
|
//Build the Producer
|
|||
|
_producer = new ProducerBuilder<string, string>(producerConfig).Build();
|
|||
|
}
|
|||
|
|
|||
|
//Method for Produce the Message to Kafka Topic
|
|||
|
public async Task ProduceMessageAsync(string topic, string value)
|
|||
|
{
|
|||
|
var kafkaMessage = new Message<string, string>
|
|||
|
{
|
|||
|
Key = Guid.NewGuid().ToString(),
|
|||
|
Value = value
|
|||
|
};
|
|||
|
|
|||
|
//Produce the Message
|
|||
|
await _producer.ProduceAsync(topic, kafkaMessage);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|