29 lines
795 B
C#
29 lines
795 B
C#
|
|
using Confluent.Kafka;
|
|||
|
|
using Microsoft.Extensions.Configuration;
|
|||
|
|
using Volo.Abp.DependencyInjection;
|
|||
|
|
|
|||
|
|
namespace JiShe.CollectBus.Kafka
|
|||
|
|
{
|
|||
|
|
public class ProducerService<T> : ITransientDependency
|
|||
|
|
{
|
|||
|
|
private readonly IProducer<Ignore, T> _producer;
|
|||
|
|
|
|||
|
|
public ProducerService(IConfiguration configuration)
|
|||
|
|
{
|
|||
|
|
var producerConfig = new ProducerConfig
|
|||
|
|
{
|
|||
|
|
BootstrapServers = configuration["Kafka:BootstrapServers"]
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
_producer = new ProducerBuilder<Ignore, T>(producerConfig).Build();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task ProduceAsync(string topic, T message)
|
|||
|
|
{
|
|||
|
|
var msg = new Message<Ignore, T> { Value = message };
|
|||
|
|
|
|||
|
|
await _producer.ProduceAsync(topic, msg);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|