在开发微服务时,出现了在任何事件发生时将信息从一种服务提供给其他服务的问题。重要的是要有一个不将发送方绑定到接收方的系统。在这种情况下,将使用发布者-订阅者模式。市场上有许多支持发布者-订阅者模式的消息传递产品,例如Azure Service Bus,RabbitMQ或Apache Kafka。最近,我发布了两个NuGet库,用于使用Azure Service Bus和RabbitMQ快速方便地设置基于事件的通信。这篇简短的方法指南文章介绍了使用后者的步骤。资源
发布者是充当发件人的.NET Core应用程序。订阅服务器是充当收件人的.NET Core应用程序。如何
1.在发布者和订阅者应用程序中,安装两个NuGet库。PM> Install-Package Autofac.Extensions.DependencyInjection
PM> Install-Package EventBus.RabbitMQ.Standard
2.在发布者和订阅者应用程序中,将配置添加到appsettings.json。{
"RabbitMq": {
"BrokerName": "test_broker",
"AutofacScopeName": "test_autofac",
"QueueName": "test_queue",
"RetryCount": "5",
"VirtualHost": "your_virtual_host",
"Username": "your_username",
"Password": "your_password",
"Host": "your_host",
"DispatchConsumersAsync": true
}
}
这些设置在CoudAMQP上可用。另外,您可以在本地使用RabbitMQ(Docker image)。3.在发布者和订阅者应用程序中,为将要发布并随后处理的事件创建一个类。public class ItemCreatedIntegrationEvent : IntegrationEvent
{
public string Title { get; set; }
public string Description { get; set; }
public ItemCreatedIntegrationEvent(string title, string description)
{
Title = title;
Description = description;
}
}
4.在订阅者应用程序中,为ItemCreatedIntegrationEvent事件处理程序创建一个类。public class ItemCreatedIntegrationEventHandler : IIntegrationEventHandler<ItemCreatedIntegrationEvent>
{
public ItemCreatedIntegrationEventHandler()
{
}
public async Task Handle(ItemCreatedIntegrationEvent @event)
{
}
}
5.在发布者和订阅者应用程序中,更新Program.cs以添加一行。public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
6.在发布者应用程序,更新ConfigureServices方法中Startup.cs。public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
var rabbitMqOptions = Configuration.GetSection("RabbitMq").Get<RabbitMqOptions>();
services.AddRabbitMqConnection(rabbitMqOptions);
services.AddRabbitMqRegistration(rabbitMqOptions);
...
}
}
7.在订阅者应用程序中,创建EventBusExtension扩展。public static class EventBusExtension
{
public static IEnumerable<IIntegrationEventHandler> GetHandlers()
{
return new List<IIntegrationEventHandler>
{
new ItemCreatedIntegrationEventHandler()
};
}
public static IApplicationBuilder SubscribeToEvents(this IApplicationBuilder app)
{
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
eventBus.Subscribe<ItemCreatedIntegrationEvent, ItemCreatedIntegrationEventHandler>();
return app;
}
}
8.在订户应用,更新ConfigureServices和配置方法在Startup.cs。public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
var rabbitMqOptions = Configuration.GetSection("RabbitMq").Get<RabbitMqOptions>();
services.AddRabbitMqConnection(rabbitMqOptions);
services.AddRabbitMqRegistration(rabbitMqOptions);
services.AddEventBusHandling(EventBusExtension.GetHandlers());
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseAuthorization();
...
}
}
9.为了进行演示和简化,您可以使用事件的发布方法在发布者应用程序中创建一个控制器。public class ItemController : ControllerBase
{
private readonly IEventBus _eventBus;
public ItemController(IEventBus eventBus)
{
_eventBus = eventBus;
}
[HttpPost]
public IActionResult Publish()
{
var message = new ItemCreatedIntegrationEvent("Item title", "Item description");
_eventBus.Publish(message);
return Ok();
}
}
10.现在,您可以发布ItemCreatedIntegrationEvent。启动这两个应用程序,调用发布方法POST 用户在处理该事件的用户。参考文献