From ad7abc50e1be729d4211a4beba4e4b2a126e5fb2 Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Wed, 31 May 2017 18:47:32 +0800 Subject: [PATCH] add rabbitmq client. --- .../Cap.Consistency.RabbitMQ.csproj | 4 ++ .../RabbitMQProducerClient.cs | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/Cap.Consistency.RabbitMQ/RabbitMQProducerClient.cs diff --git a/src/Cap.Consistency.RabbitMQ/Cap.Consistency.RabbitMQ.csproj b/src/Cap.Consistency.RabbitMQ/Cap.Consistency.RabbitMQ.csproj index b0de771..834407f 100644 --- a/src/Cap.Consistency.RabbitMQ/Cap.Consistency.RabbitMQ.csproj +++ b/src/Cap.Consistency.RabbitMQ/Cap.Consistency.RabbitMQ.csproj @@ -13,4 +13,8 @@ + + + + \ No newline at end of file diff --git a/src/Cap.Consistency.RabbitMQ/RabbitMQProducerClient.cs b/src/Cap.Consistency.RabbitMQ/RabbitMQProducerClient.cs new file mode 100644 index 0000000..86e0b34 --- /dev/null +++ b/src/Cap.Consistency.RabbitMQ/RabbitMQProducerClient.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Cap.Consistency.Infrastructure; +using Cap.Consistency.Producer; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using RabbitMQ.Client; + +namespace Cap.Consistency.RabbitMQ +{ + public class RabbitMQProducerClient : IProducerClient + { + + private readonly ConsistencyOptions _options; + private readonly ILogger _logger; + + public RabbitMQProducerClient(IOptions options, ILoggerFactory loggerFactory) { + _options = options.Value; + _logger = loggerFactory.CreateLogger(nameof(RabbitMQProducerClient)); + } + + public Task SendAsync(string topic, string content) { + var factory = new ConnectionFactory() { HostName = _options.BrokerUrlList }; + using (var connection = factory.CreateConnection()) + using (var channel = connection.CreateModel()) { + channel.ExchangeDeclare(exchange: "topic_logs", + type: "topic"); + + + var body = Encoding.UTF8.GetBytes(content); + channel.BasicPublish(exchange: "topic_logs", + routingKey: topic, + basicProperties: null, + body: body); + + return Task.CompletedTask; + } + } + } +}