1. 程式人生 > 其它 >ABP VNext 分散式事務Event Bus 整合RabbitMQ

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; }
    }