Distributed Event Bus RabbitMQ Integration

This document explains how to configure the RabbitMQ as the distributed event bus provider. See the distributed event bus document to learn how to use the distributed event bus system

Installation

Use the ABP CLI to add Volo.Abp.EventBus.RabbitMQ NuGet package to your project:

  • Install the ABP CLI if you haven’t installed before.
  • Open a command line (terminal) in the directory of the .csproj file you want to add the Volo.Abp.EventBus.RabbitMQ package.
  • Run abp add-package Volo.Abp.EventBus.RabbitMQ command.

If you want to do it manually, install the Volo.Abp.EventBus.RabbitMQ NuGet package to your project and add [DependsOn(typeof(AbpEventBusRabbitMqModule))] to the ABP module class inside your project.

Configuration

You can configure using the standard configuration system, like using the appsettings.json file, or using the options classes.

appsettings.json file configuration

This is the simplest way to configure the RabbitMQ settings. It is also very strong since you can use any other configuration source (like environment variables) that is supported by the AspNet Core.

Example: The minimal configuration to connect to a local RabbitMQ server with default configurations

  1. {
  2. "RabbitMQ": {
  3. "EventBus": {
  4. "ClientName": "MyClientName",
  5. "ExchangeName": "MyExchangeName"
  6. }
  7. }
  8. }
  • ClientName is the name of this application, which is used as the queue name on the RabbitMQ.
  • ExchangeName is the exchange name.

See the RabbitMQ document to understand these options better.

Connections

If you need to connect to another server than the localhost, you need to configure the connection properties.

Example: Specify the host name (as an IP address)

  1. {
  2. "RabbitMQ": {
  3. "Connections": {
  4. "Default": {
  5. "HostName": "123.123.123.123"
  6. }
  7. },
  8. "EventBus": {
  9. "ClientName": "MyClientName",
  10. "ExchangeName": "MyExchangeName"
  11. }
  12. }
  13. }

Defining multiple connections is allowed. In this case, you can specify the connection that is used for the event bus.

Example: Declare two connections and use one of them for the event bus

  1. {
  2. "RabbitMQ": {
  3. "Connections": {
  4. "Default": {
  5. "HostName": "123.123.123.123"
  6. },
  7. "SecondConnection": {
  8. "HostName": "321.321.321.321"
  9. }
  10. },
  11. "EventBus": {
  12. "ClientName": "MyClientName",
  13. "ExchangeName": "MyExchangeName",
  14. "ConnectionName": "SecondConnection"
  15. }
  16. }
  17. }

This allows you to use multiple RabbitMQ server in your application, but select one of them for the event bus.

You can use any of the ConnectionFactry properties as the connection properties.

Example: Specify the connection port

  1. {
  2. "RabbitMQ": {
  3. "Connections": {
  4. "Default": {
  5. "HostName": "123.123.123.123",
  6. "Port": "5672"
  7. }
  8. }
  9. }
  10. }

The Options Classes

AbpRabbitMqOptions and AbpRabbitMqEventBusOptions classes can be used to configure the connection strings and event bus options for the RabbitMQ.

You can configure this options inside the ConfigureServices of your module.

Example: Configure the connection

  1. Configure<AbpRabbitMqOptions>(options =>
  2. {
  3. options.Connections.Default.UserName = "user";
  4. options.Connections.Default.Password = "pass";
  5. options.Connections.Default.HostName = "123.123.123.123";
  6. options.Connections.Default.Port = 5672;
  7. });

Example: Configure the client and exchange names

  1. Configure<AbpRabbitMqEventBusOptions>(options =>
  2. {
  3. options.ClientName = "TestApp1";
  4. options.ExchangeName = "TestMessages";
  5. });

Using these options classes can be combined with the appsettings.json way. Configuring an option property in the code overrides the value in the configuration file.