|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using Confluent.Kafka;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using Newtonsoft.Json;
- using TelpoPush.Models.Config;
-
- namespace TelpoPush.Service.Mq.Kafka
- {
- /// <summary>
- /// 消息生产者
- /// </summary>
- public class MessageProducer
- {
- private readonly ILogger<MessageProducer> _logger;
- private readonly ServiceConfig _configService;
- private readonly IProducer<Null, string> _producer;
-
- public MessageProducer(ILogger<MessageProducer> logger, IOptions<ServiceConfig> optConfigService)
- {
- _logger = logger;
- _configService = optConfigService.Value;
-
- var config = new ProducerConfig
- {
- BootstrapServers = _configService.KafkaBootstrapServers,
- EnableIdempotence = true,
- Acks = Acks.All,
- //LingerMs=5000,
- //BatchNumMessages =1000,
- //BatchSize=32768,
- //CompressionType= CompressionType.Lz4,
- MessageSendMaxRetries = 3
- };
- _producer = new ProducerBuilder<Null, string>(config).Build();
- }
-
- public Headers CreateHeader(Dictionary<string, int> pair = null)
- {
- if (pair == null)
- {
- return null;
- }
- else
- {
- Headers headers = new Headers();
-
- foreach (var item in pair)
- {
- headers.Add(item.Key, BitConverter.GetBytes(item.Value));
- }
- return headers;
- }
- }
-
- public async Task ProduceAsync(List<TopicModel> topic, object message)
- {
- try
- {
- foreach (var item in topic)
- {
- // producer = new ProducerBuilder<Null, string>(config).Build();
- await _producer.ProduceAsync(item.Topic, new Message<Null, string>
- {
- Headers = item.Headers,
- Value = JsonConvert.SerializeObject(message)
- });
- }
- }
- catch (ProduceException<Null, string> e)
- {
- _logger.LogError($"推送到kafka失败,topic: {topic},\n message:{JsonConvert.SerializeObject(message)}: \n{e.Error.Reason}");
- }
- }
-
-
-
- }
- }
|