设为首页 加入收藏

TOP

ASP.NET Core基于微软微服务eShopOnContainer事件总线EventBus的实现(一)
2019-09-18 11:14:40 】 浏览:75
Tags: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<
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C#关键字 下一篇Setup Factory 9 简单打包

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目