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


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

從Nuget.org中安裝

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

使用

共3步:

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

1.添加事件

創建YourEvent.cs文件

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

1.添加事件處理器

創建YourEventHandler.cs文件

 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.從控制器中發布事件

剛剛創建了一個事件,並且添加了事件處理器來處理事件,這里演示了如何發布事件;雖然剛剛添加了事件處理器,但是沒有將事件處理器注冊到ASP.NET Core中,下面的安裝環境將演示如何注冊。

 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM