Browse Source

add rabbitmq client.

master
yangxiaodong 7 years ago
parent
commit
ad7abc50e1
2 changed files with 46 additions and 0 deletions
  1. +4
    -0
      src/Cap.Consistency.RabbitMQ/Cap.Consistency.RabbitMQ.csproj
  2. +42
    -0
      src/Cap.Consistency.RabbitMQ/RabbitMQProducerClient.cs

+ 4
- 0
src/Cap.Consistency.RabbitMQ/Cap.Consistency.RabbitMQ.csproj View File

@@ -13,4 +13,8 @@
<ProjectReference Include="..\Cap.Consistency\Cap.Consistency.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Microsoft.Extensions.DependencyInjection\" />
</ItemGroup>

</Project>

+ 42
- 0
src/Cap.Consistency.RabbitMQ/RabbitMQProducerClient.cs View File

@@ -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<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;
}
}
}
}

Loading…
Cancel
Save