标签:tps rev 安装 bsp 处理 new pac readonly 消息
这个EventBus的实现是基于微软微服务https://github.com/dotnet-architecture/eShopOnContainers项目的,我把它从项目中抽离出来,打包成nuget包方便大家快速集成到项目中
从Nuget.org中安装
PM> Install-Package Toosame.EventBus.RabbitMQ -Version 1.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
ASP.NET Core基于微软微服务eShopOnContainer事件总线EventBus的实现
标签:tps rev 安装 bsp 处理 new pac readonly 消息
原文地址:https://www.cnblogs.com/myhalo/p/11445329.html