这个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