Commit ad7abc50 authored by yangxiaodong's avatar yangxiaodong

add rabbitmq client.

parent aa247b19
...@@ -13,4 +13,8 @@ ...@@ -13,4 +13,8 @@
<ProjectReference Include="..\Cap.Consistency\Cap.Consistency.csproj" /> <ProjectReference Include="..\Cap.Consistency\Cap.Consistency.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Microsoft.Extensions.DependencyInjection\" />
</ItemGroup>
</Project> </Project>
\ No newline at end of file
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<ConsistencyOptions> 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;
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment