RabbitMQ集成系统文章01---ABP VNext 分布式事务Event Bus 集成RabbitMQ
1、在两个应用中都配置好要连接的RabbitMQ
"RabbitMQ": { "Connections": { "Default": { "HostName": "xxx.xxx.160.149", "Port": "5672", "UserName": "root", "Password": "123456", "VirtualHost": "/" } }, "EventBus": { "ClientName": "omsUpdate_baseInfoService", "ExchangeName": "omsUpdate" } }
2、在两个服务中Application层引入RabbitMQ中间件(如果其它层要用也要引入,我只是这个层用)
Volo.Abp.EventBus.RabbitMQ 我用的版本为4.4.4
3、在两个服务中Application层的加入依赖 [DependsOn(typeof(AbpEventBusRabbitMqModule))]
4、在服务A中Application层中的某个方法中发布消息,当然还可以在聚合根中发布消息
注入服务
在方法中发布消息
await _distributedEventBus.PublishAsync( new StockCountChangedEto { ProductId = id, NewCount = 10 } );
5、在B服务中订阅消息(前面的配置和包都在这个块模引入了)
public class MyHandler : IDistributedEventHandler<StockCountChangedEto>, ITransientDependency { public async Task HandleEventAsync(StockCountChangedEto eventData) { var productId = eventData.ProductId; } }
[EventName("OmsUpdate.Product.StockChange")] public class StockCountChangedEto { public Guid ProductId { get; set; } public int NewCount { get; set; } }