2024-12-04 01:57:10 +04:00

36 lines
915 B
C#

using Cloud.Services.Broker.Support;
using Confluent.Kafka;
namespace Cloud.Services.Broker.Implement.Kafka
{
public class KafkaProducer : IBrokerProducer
{
private IProducer<Guid, Command> _producer;
private readonly IConfiguration _config;
public KafkaProducer(IConfiguration configuration)
{
_config = configuration;
ChangeBrokerIp(_config["Kafka:BootstrapServers"]);
}
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);
}
public void ChangeBrokerIp(string ip)
{
var producerConfig = new ProducerConfig
{
BootstrapServers = ip
};
//Build the Producer
_producer = new ProducerBuilder<Guid, Command>(producerConfig).Build();
}
}
}