init
This commit is contained in:
354
DouyinApi.EventBus/RabbitMQPersistent/EventBusRabbitMQ.cs
Normal file
354
DouyinApi.EventBus/RabbitMQPersistent/EventBusRabbitMQ.cs
Normal file
@@ -0,0 +1,354 @@
|
||||
using Autofac;
|
||||
using DouyinApi.Common.Extensions;
|
||||
using DouyinApi.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Polly;
|
||||
using Polly.Retry;
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
using RabbitMQ.Client.Exceptions;
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DouyinApi.EventBus
|
||||
{
|
||||
/// <summary>
|
||||
/// 基于RabbitMQ的事件总线
|
||||
/// </summary>
|
||||
public class EventBusRabbitMQ : IEventBus, IDisposable
|
||||
{
|
||||
const string BROKER_NAME = "blogcore_event_bus";
|
||||
|
||||
private readonly IRabbitMQPersistentConnection _persistentConnection;
|
||||
private readonly ILogger<EventBusRabbitMQ> _logger;
|
||||
private readonly IEventBusSubscriptionsManager _subsManager;
|
||||
private readonly ILifetimeScope _autofac;
|
||||
private readonly string AUTOFAC_SCOPE_NAME = "blogcore_event_bus";
|
||||
private readonly int _retryCount;
|
||||
|
||||
private IModel _consumerChannel;
|
||||
private string _queueName;
|
||||
|
||||
/// <summary>
|
||||
/// RabbitMQ事件总线
|
||||
/// </summary>
|
||||
/// <param name="persistentConnection">RabbitMQ持久连接</param>
|
||||
/// <param name="logger">日志</param>
|
||||
/// <param name="autofac">autofac容器</param>
|
||||
/// <param name="subsManager">事件总线订阅管理器</param>
|
||||
/// <param name="queueName">队列名称</param>
|
||||
/// <param name="retryCount">重试次数</param>
|
||||
public EventBusRabbitMQ(IRabbitMQPersistentConnection persistentConnection, ILogger<EventBusRabbitMQ> logger,
|
||||
ILifetimeScope autofac,
|
||||
IEventBusSubscriptionsManager subsManager,
|
||||
string queueName = null,
|
||||
int retryCount = 5)
|
||||
{
|
||||
_persistentConnection = persistentConnection ?? throw new ArgumentNullException(nameof(persistentConnection));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager();
|
||||
_queueName = queueName;
|
||||
_consumerChannel = CreateConsumerChannel();
|
||||
_autofac = autofac;
|
||||
_retryCount = retryCount;
|
||||
_subsManager.OnEventRemoved += SubsManager_OnEventRemoved;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订阅管理器事件
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="eventName"></param>
|
||||
private void SubsManager_OnEventRemoved(object sender, string eventName)
|
||||
{
|
||||
if (!_persistentConnection.IsConnected)
|
||||
{
|
||||
_persistentConnection.TryConnect();
|
||||
}
|
||||
|
||||
using (var channel = _persistentConnection.CreateModel())
|
||||
{
|
||||
channel.QueueUnbind(queue: _queueName,
|
||||
exchange: BROKER_NAME,
|
||||
routingKey: eventName);
|
||||
|
||||
if (_subsManager.IsEmpty)
|
||||
{
|
||||
_queueName = string.Empty;
|
||||
_consumerChannel.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发布
|
||||
/// </summary>
|
||||
/// <param name="event">事件模型</param>
|
||||
public void Publish(IntegrationEvent @event)
|
||||
{
|
||||
if (!_persistentConnection.IsConnected)
|
||||
{
|
||||
_persistentConnection.TryConnect();
|
||||
}
|
||||
|
||||
var policy = RetryPolicy.Handle<BrokerUnreachableException>()
|
||||
.Or<SocketException>()
|
||||
.WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) =>
|
||||
{
|
||||
_logger.LogWarning(ex, "Could not publish event: {EventId} after {Timeout}s ({ExceptionMessage})", @event.Id, $"{time.TotalSeconds:n1}", ex.Message);
|
||||
});
|
||||
|
||||
var eventName = @event.GetType().Name;
|
||||
|
||||
_logger.LogTrace("Creating RabbitMQ channel to publish event: {EventId} ({EventName})", @event.Id, eventName);
|
||||
|
||||
using (var channel = _persistentConnection.CreateModel())
|
||||
{
|
||||
|
||||
_logger.LogTrace("Declaring RabbitMQ exchange to publish event: {EventId}", @event.Id);
|
||||
|
||||
channel.ExchangeDeclare(exchange: BROKER_NAME, type: "direct");
|
||||
|
||||
var message = JsonConvert.SerializeObject(@event);
|
||||
var body = Encoding.UTF8.GetBytes(message);
|
||||
|
||||
policy.Execute(() =>
|
||||
{
|
||||
var properties = channel.CreateBasicProperties();
|
||||
properties.DeliveryMode = 2; // persistent
|
||||
|
||||
_logger.LogTrace("Publishing event to RabbitMQ: {EventId}", @event.Id);
|
||||
|
||||
channel.BasicPublish(
|
||||
exchange: BROKER_NAME,
|
||||
routingKey: eventName,
|
||||
mandatory: true,
|
||||
basicProperties: properties,
|
||||
body: body);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订阅
|
||||
/// 动态
|
||||
/// </summary>
|
||||
/// <typeparam name="TH">事件处理器</typeparam>
|
||||
/// <param name="eventName">事件名</param>
|
||||
public void SubscribeDynamic<TH>(string eventName)
|
||||
where TH : IDynamicIntegrationEventHandler
|
||||
{
|
||||
_logger.LogInformation("Subscribing to dynamic event {EventName} with {EventHandler}", eventName, typeof(TH).GetGenericTypeName());
|
||||
|
||||
DoInternalSubscription(eventName);
|
||||
_subsManager.AddDynamicSubscription<TH>(eventName);
|
||||
StartBasicConsume();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订阅
|
||||
/// </summary>
|
||||
/// <typeparam name="T">约束:事件模型</typeparam>
|
||||
/// <typeparam name="TH">约束:事件处理器<事件模型></typeparam>
|
||||
public void Subscribe<T, TH>()
|
||||
where T : IntegrationEvent
|
||||
where TH : IIntegrationEventHandler<T>
|
||||
{
|
||||
var eventName = _subsManager.GetEventKey<T>();
|
||||
DoInternalSubscription(eventName);
|
||||
|
||||
_logger.LogInformation("Subscribing to event {EventName} with {EventHandler}", eventName, typeof(TH).GetGenericTypeName());
|
||||
|
||||
ConsoleHelper.WriteSuccessLine($"Subscribing to event {eventName} with {typeof(TH).GetGenericTypeName()}");
|
||||
|
||||
_subsManager.AddSubscription<T, TH>();
|
||||
StartBasicConsume();
|
||||
}
|
||||
|
||||
private void DoInternalSubscription(string eventName)
|
||||
{
|
||||
var containsKey = _subsManager.HasSubscriptionsForEvent(eventName);
|
||||
if (!containsKey)
|
||||
{
|
||||
if (!_persistentConnection.IsConnected)
|
||||
{
|
||||
_persistentConnection.TryConnect();
|
||||
}
|
||||
|
||||
using (var channel = _persistentConnection.CreateModel())
|
||||
{
|
||||
channel.QueueBind(queue: _queueName,
|
||||
exchange: BROKER_NAME,
|
||||
routingKey: eventName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消订阅
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="TH"></typeparam>
|
||||
public void Unsubscribe<T, TH>()
|
||||
where T : IntegrationEvent
|
||||
where TH : IIntegrationEventHandler<T>
|
||||
{
|
||||
var eventName = _subsManager.GetEventKey<T>();
|
||||
|
||||
_logger.LogInformation("Unsubscribing from event {EventName}", eventName);
|
||||
|
||||
_subsManager.RemoveSubscription<T, TH>();
|
||||
}
|
||||
|
||||
public void UnsubscribeDynamic<TH>(string eventName)
|
||||
where TH : IDynamicIntegrationEventHandler
|
||||
{
|
||||
_subsManager.RemoveDynamicSubscription<TH>(eventName);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_consumerChannel != null)
|
||||
{
|
||||
_consumerChannel.Dispose();
|
||||
}
|
||||
|
||||
_subsManager.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始基本消费
|
||||
/// </summary>
|
||||
private void StartBasicConsume()
|
||||
{
|
||||
_logger.LogTrace("Starting RabbitMQ basic consume");
|
||||
|
||||
if (_consumerChannel != null)
|
||||
{
|
||||
var consumer = new AsyncEventingBasicConsumer(_consumerChannel);
|
||||
|
||||
consumer.Received += Consumer_Received;
|
||||
|
||||
_consumerChannel.BasicConsume(
|
||||
queue: _queueName,
|
||||
autoAck: false,
|
||||
consumer: consumer);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError("StartBasicConsume can't call on _consumerChannel == null");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 消费者接受到
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="eventArgs"></param>
|
||||
/// <returns></returns>
|
||||
private async Task Consumer_Received(object sender, BasicDeliverEventArgs eventArgs)
|
||||
{
|
||||
var eventName = eventArgs.RoutingKey;
|
||||
var message = Encoding.UTF8.GetString(eventArgs.Body.Span);
|
||||
|
||||
try
|
||||
{
|
||||
if (message.ToLowerInvariant().Contains("throw-fake-exception"))
|
||||
{
|
||||
throw new InvalidOperationException($"Fake exception requested: \"{message}\"");
|
||||
}
|
||||
|
||||
await ProcessEvent(eventName, message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "----- ERROR Processing message \"{Message}\"", message);
|
||||
}
|
||||
|
||||
// Even on exception we take the message off the queue.
|
||||
// in a REAL WORLD app this should be handled with a Dead Letter Exchange (DLX).
|
||||
// For more information see: https://www.rabbitmq.com/dlx.html
|
||||
_consumerChannel.BasicAck(eventArgs.DeliveryTag, multiple: false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创造消费通道
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private IModel CreateConsumerChannel()
|
||||
{
|
||||
if (!_persistentConnection.IsConnected)
|
||||
{
|
||||
_persistentConnection.TryConnect();
|
||||
}
|
||||
|
||||
_logger.LogTrace("Creating RabbitMQ consumer channel");
|
||||
|
||||
var channel = _persistentConnection.CreateModel();
|
||||
|
||||
channel.ExchangeDeclare(exchange: BROKER_NAME,
|
||||
type: "direct");
|
||||
|
||||
channel.QueueDeclare(queue: _queueName,
|
||||
durable: true,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
arguments: null);
|
||||
|
||||
channel.CallbackException += (sender, ea) =>
|
||||
{
|
||||
_logger.LogWarning(ea.Exception, "Recreating RabbitMQ consumer channel");
|
||||
|
||||
_consumerChannel.Dispose();
|
||||
_consumerChannel = CreateConsumerChannel();
|
||||
StartBasicConsume();
|
||||
};
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
private async Task ProcessEvent(string eventName, string message)
|
||||
{
|
||||
_logger.LogTrace("Processing RabbitMQ event: {EventName}", eventName);
|
||||
|
||||
if (_subsManager.HasSubscriptionsForEvent(eventName))
|
||||
{
|
||||
using (var scope = _autofac.BeginLifetimeScope(AUTOFAC_SCOPE_NAME))
|
||||
{
|
||||
var subscriptions = _subsManager.GetHandlersForEvent(eventName);
|
||||
foreach (var subscription in subscriptions)
|
||||
{
|
||||
if (subscription.IsDynamic)
|
||||
{
|
||||
var handler = scope.ResolveOptional(subscription.HandlerType) as IDynamicIntegrationEventHandler;
|
||||
if (handler == null) continue;
|
||||
dynamic eventData = JObject.Parse(message);
|
||||
|
||||
await Task.Yield();
|
||||
await handler.Handle(eventData);
|
||||
}
|
||||
else
|
||||
{
|
||||
var handler = scope.ResolveOptional(subscription.HandlerType);
|
||||
if (handler == null) continue;
|
||||
var eventType = _subsManager.GetEventTypeByName(eventName);
|
||||
var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
|
||||
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
|
||||
|
||||
await Task.Yield();
|
||||
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("No subscription for RabbitMQ event: {EventName}", eventName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using RabbitMQ.Client;
|
||||
using System;
|
||||
|
||||
namespace DouyinApi.EventBus
|
||||
{
|
||||
/// <summary>
|
||||
/// RabbitMQ持久连接
|
||||
/// 接口
|
||||
/// </summary>
|
||||
public interface IRabbitMQPersistentConnection
|
||||
: IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否已经连接
|
||||
/// </summary>
|
||||
bool IsConnected { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 尝试重连
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
bool TryConnect();
|
||||
|
||||
/// <summary>
|
||||
/// 创建Model
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IModel CreateModel();
|
||||
|
||||
/// <summary>
|
||||
/// 发布消息
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="exchangeName"></param>
|
||||
/// <param name="routingKey"></param>
|
||||
void PublishMessage(string message, string exchangeName, string routingKey);
|
||||
|
||||
/// <summary>
|
||||
/// 订阅消息
|
||||
/// </summary>
|
||||
/// <param name="queueName"></param>
|
||||
void StartConsuming(string queueName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Polly;
|
||||
using Polly.Retry;
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
using RabbitMQ.Client.Exceptions;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace DouyinApi.EventBus
|
||||
{
|
||||
/// <summary>
|
||||
/// RabbitMQ持久连接
|
||||
/// </summary>
|
||||
public class RabbitMQPersistentConnection
|
||||
: IRabbitMQPersistentConnection
|
||||
{
|
||||
private readonly IConnectionFactory _connectionFactory;
|
||||
private readonly ILogger<RabbitMQPersistentConnection> _logger;
|
||||
private readonly int _retryCount;
|
||||
IConnection _connection;
|
||||
bool _disposed;
|
||||
|
||||
object sync_root = new object();
|
||||
|
||||
public RabbitMQPersistentConnection(IConnectionFactory connectionFactory, ILogger<RabbitMQPersistentConnection> logger,
|
||||
int retryCount = 5)
|
||||
{
|
||||
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_retryCount = retryCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否已连接
|
||||
/// </summary>
|
||||
public bool IsConnected
|
||||
{
|
||||
get
|
||||
{
|
||||
return _connection != null && _connection.IsOpen && !_disposed;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建Model
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IModel CreateModel()
|
||||
{
|
||||
if (!IsConnected)
|
||||
{
|
||||
throw new InvalidOperationException("No RabbitMQ connections are available to perform this action");
|
||||
}
|
||||
|
||||
return _connection.CreateModel();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
|
||||
_disposed = true;
|
||||
|
||||
try
|
||||
{
|
||||
_connection.Dispose();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
_logger.LogCritical(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool TryConnect()
|
||||
{
|
||||
_logger.LogInformation("RabbitMQ Client is trying to connect");
|
||||
|
||||
lock (sync_root)
|
||||
{
|
||||
var policy = RetryPolicy.Handle<SocketException>()
|
||||
.Or<BrokerUnreachableException>()
|
||||
.WaitAndRetry(_retryCount,
|
||||
retryAttempt =>
|
||||
TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) =>
|
||||
{
|
||||
_logger.LogWarning(ex, "RabbitMQ Client could not connect after {TimeOut}s ({ExceptionMessage})", $"{time.TotalSeconds:n1}", ex.Message);
|
||||
}
|
||||
);
|
||||
|
||||
policy.Execute(() =>
|
||||
{
|
||||
_connection = _connectionFactory
|
||||
.CreateConnection();
|
||||
});
|
||||
|
||||
if (IsConnected)
|
||||
{
|
||||
_connection.ConnectionShutdown += OnConnectionShutdown;
|
||||
_connection.CallbackException += OnCallbackException;
|
||||
_connection.ConnectionBlocked += OnConnectionBlocked;
|
||||
|
||||
_logger.LogInformation("RabbitMQ Client acquired a persistent connection to '{HostName}' and is subscribed to failure events", _connection.Endpoint.HostName);
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogCritical("FATAL ERROR: RabbitMQ connections could not be created and opened");
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接被阻断
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void OnConnectionBlocked(object sender, ConnectionBlockedEventArgs e)
|
||||
{
|
||||
if (_disposed) return;
|
||||
|
||||
_logger.LogWarning("A RabbitMQ connection is shutdown. Trying to re-connect...");
|
||||
|
||||
TryConnect();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接出现异常
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
void OnCallbackException(object sender, CallbackExceptionEventArgs e)
|
||||
{
|
||||
if (_disposed) return;
|
||||
|
||||
_logger.LogWarning("A RabbitMQ connection throw exception. Trying to re-connect...");
|
||||
|
||||
TryConnect();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接被关闭
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="reason"></param>
|
||||
void OnConnectionShutdown(object sender, ShutdownEventArgs reason)
|
||||
{
|
||||
if (_disposed) return;
|
||||
|
||||
_logger.LogWarning("A RabbitMQ connection is on shutdown. Trying to re-connect...");
|
||||
|
||||
TryConnect();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发布消息
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="exchangeName"></param>
|
||||
/// <param name="routingKey"></param>
|
||||
public void PublishMessage(string message, string exchangeName, string routingKey)
|
||||
{
|
||||
using var channel = CreateModel();
|
||||
channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Direct, true);
|
||||
var body = Encoding.UTF8.GetBytes(message);
|
||||
channel.BasicPublish(exchange: exchangeName, routingKey: routingKey, basicProperties: null, body: body);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订阅消息
|
||||
/// </summary>
|
||||
/// <param name="queueName"></param>
|
||||
public void StartConsuming(string queueName)
|
||||
{
|
||||
using var channel = CreateModel();
|
||||
channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
|
||||
|
||||
var consumer = new AsyncEventingBasicConsumer(channel);
|
||||
consumer.Received += new AsyncEventHandler<BasicDeliverEventArgs>(
|
||||
async (a, b) =>
|
||||
{
|
||||
var Headers = b.BasicProperties.Headers;
|
||||
var msgBody = b.Body.ToArray();
|
||||
var message = Encoding.UTF8.GetString(msgBody);
|
||||
await Task.CompletedTask;
|
||||
Console.WriteLine("Received message: {0}", message);
|
||||
|
||||
//bool Dealresult = await Dealer(b.Exchange, b.RoutingKey, msgBody, Headers);
|
||||
//if (Dealresult) channel.BasicAck(b.DeliveryTag, false);
|
||||
//else channel.BasicNack(b.DeliveryTag, false, true);
|
||||
}
|
||||
);
|
||||
|
||||
channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);
|
||||
|
||||
Console.WriteLine("Consuming messages...");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user