28 lines
768 B
C#
28 lines
768 B
C#
using Cloud.Services.Broker.Support;
|
|
using Confluent.Kafka;
|
|
|
|
namespace Cloud.Services.Broker.Implement.Kafka
|
|
{
|
|
public class KafkaProducer : IBrokerProducer
|
|
{
|
|
private readonly IProducer<Guid, Command> _producer;
|
|
|
|
public KafkaProducer(IConfiguration configuration)
|
|
{
|
|
var producerConfig = new ProducerConfig
|
|
{
|
|
BootstrapServers = configuration["Kafka:BootstrapServers"]
|
|
};
|
|
|
|
//Build the Producer
|
|
_producer = new ProducerBuilder<Guid, Command>(producerConfig).Build();
|
|
}
|
|
public async Task ProduceAsync(string topic, Command command)
|
|
{
|
|
var message = new Message<Guid, Command> { Key = Guid.NewGuid(), Value = command };
|
|
|
|
//Produce the Message
|
|
await _producer.ProduceAsync(topic, message);
|
|
}
|
|
}
|
|
} |