ASP.NET Core基於微軟微服務eShopOnContainer事件總線EventBus的實現

這個EventBus的實現是基於微軟微服務https://github.com/dotnet-architecture/eShopOnContainers項目的,我把它從項目中抽離出來,打包成nuget包方便你們快速集成到項目中git

從Nuget.org中安裝github

PM> Install-Package Toosame.EventBus.RabbitMQ -Version 1.1.2

使用json

共3步:app

  1. 添加事件
  2. 添加事件處理器
  3. 從控制器發佈事件

1.添加事件ide

建立YourEvent.cs文件微服務

複製代碼
1 public class YourEvent : IntegrationEvent
2 {
3     public string Name { get; set; }
4 
5     public int Age { get; set; }
6 }
複製代碼

1.添加事件處理器post

建立YourEventHandler.cs文件ui

複製代碼
 1 public class YourEventHandler : IIntegrationEventHandler<YourEvent>
 2 {
 3     private readonly IConfiguration _configuration;
 4 
 5     public YourEventHandler(IConfiguration configuration){
 6         //這裏只是告訴你,能夠使用依賴注入的服務.
 7         _configuration = configuration;
 8     }
 9 
10     public Task Handle(YourEvent @event)
11     {
12         //你能夠拿到 @event.Name
13         //你能夠拿到 @event.Age
14 
15         //實現你本身的事件處理邏輯...
16     
17         return Task.CompletedTask;
18     }
19 }
複製代碼

1.從控制器中發佈事件spa

剛剛建立了一個事件,而且添加了事件處理器來處理事件,這裏演示瞭如何發佈事件;雖然剛剛添加了事件處理器,可是沒有將事件處理器註冊到ASP.NET Core中,下面的安裝環境將演示如何註冊。code

複製代碼
 1 public class HomeController : Controller
 2 {
 3     private readonly IEventBus _eventBus;
 4 
 5     public YourEventHandler(IEventBus eventBus){
 6         _eventBus = eventBus;
 7     }
 8 
 9     [HttpGet]
10     public IAcionResult Index(){
11         _eventBus.Publish(new YourEvent(){
12             Name: "my name",
13             Age: 22
14         })
15     }
16 }
複製代碼

安裝:註冊事件和事件處理器

共2步:

  1.配置appsettings.json

  2.在Startup.cs中安裝

1.配置appsettings.json

複製代碼
{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "RabbitMQ": {
    "EventBusConnection": "<yourRabbitMqHost>[:port(default 5672)]",
    "EventBusUserName": "<rabbitMqUserName>",
    "EventBusPassword": "<rabbitMqPassword>",
    "EventBusRetryCount": 5,
    "EventBusBrokeName": "<rabbitMqExchangeName>",
    "SubscriptionClientName": "<queueName>" //在微服務中,不一樣的微服務的應該是不一樣的名字
  }
}
複製代碼

2.在Startup.cs中安裝

 

經典安裝:

複製代碼
 1 public void ConfigureServices(IServiceCollection services)
 2 {
 3     services.AddEventBus(Configuration.GetSection("RabbitMQ").Get<RabbitMQOption>(),
 4                     eventHandlers =>
 5                     {
 6                         eventHandlers.AddEventHandler<YourEventHandler1>();
 7                         eventHandlers.AddEventHandler<YourEventHandler2>();
 8                     });
 9 
10     services.AddMvc()
11         .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
12 }
13 
14 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
15 {
16     app.UseEventBus(eventBus =>
17     {
18         eventBus.Subscribe<YourEvent1, YourEventHandler1>();
19         eventBus.Subscribe<YourEvent2, YourEventHandler2>();
20     });
21 
22     app.UseMvc();
23 }
複製代碼

請把YourEvent和YourEventHandler換成你本身的事件和事件處理器

使用Autofac安裝:

請先安裝Autofac.Extensions.DependencyInjection這個包再使用如下代碼

複製代碼
 1 public IServiceProvider ConfigureServices(IServiceCollection services)
 2 {
 3     services.AddMvc()
 4         .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
 5         .AddControllersAsServices();
 6 
 7     return services.AddEventBusAsAutofacService(Configuration.GetSection("RabbitMQ").Get<RabbitMQOption>(),
 8                 eventHandlers =>
 9             {
10                 eventHandlers.AddEventHandler<YourEventHandler1>();
11                 eventHandlers.AddEventHandler<YourEventHandler2>();
12             });
13 }
14 
15 
16 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
17 {
18     app.UseEventBus(eventBus =>
19     {
20         eventBus.Subscribe<YourEvent1, YourEventHandler1>();
21         eventBus.Subscribe<YourEvent2, YourEventHandler2>();
22     });
23 
24     app.UseMvc();
25 }
複製代碼

這樣剛剛咱們建立的EventHandler就能正常的收到事件了;

注意:不一樣微服務經過事件總線交換消息,Event的名字在不一樣的微服務項目中必須一致,由於RabbitMQ是經過事件名找隊列(一個隊列對應一個微服務)

項目github:https://github.com/lhdboy/Toosame.EventBus

相關文章
相關標籤/搜索