RabbitMQ Messaging
This tutorial takes you through setting up 2 .NET Core applications that interact through RabbitMQ.
Note
For more detailed examples, please refer to the Messaging solution in the Steeltoe Samples Repository.
First, start a RabbitMQ instance. Using the Steeltoe dockerfile, start a local instance of RabbitMQ
docker run --publish 5672:5672 steeltoeoss/rabbitmq
Next create a .NET Core WebAPI that will ensure the queue is created and write messages to it.
Create a new ASP.NET Core WebAPI app with the Steeltoe Initializr
Name the project "WriteToRabbitMQ"
Add RabbitMQ Messaging Dependency
Click Generate Project to download a zip containing the new project
Extract the zipped project and open in your IDE of choice
Open the package manager console
Install NuGet distributed packages
Install-Package -Id Steeltoe.Messaging.RabbitMQ
Ensure RabbitMQHost appears in Program.cs
See RabbitMQHost documentation to further understand what is happening behind the scenes
public static IHostBuilder CreateHostBuilder(string[] args) => RabbitMQHost.CreateDefaultBuilder() .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
Add rabbit messaging configurations to Startup.cs
using Steeltoe.Messaging.RabbitMQ.Config; using Steeltoe.Messaging.RabbitMQ.Extensions; public class Startup { public const string RECEIVE_AND_CONVERT_QUEUE = "steeltoe_message_queue"; public void ConfigureServices(IServiceCollection services){ // Add a queue to the message container that the rabbit admin will discover and declare at startup services.AddRabbitQueue(new Queue(RECEIVE_AND_CONVERT_QUEUE)); } }
Create a new controller class Controllers\WriteMessageQueueController.cs
using Microsoft.Extensions.Logging; using Steeltoe.Messaging.RabbitMQ.Core; [ApiController] [Route("[controller]")] public class WriteMessageQueueController : ControllerBase { public const string RECEIVE_AND_CONVERT_QUEUE = "steeltoe_message_queue"; private readonly ILogger<WriteMessageQueueController> _logger; private readonly RabbitTemplate _rabbitTemplate; private readonly RabbitAdmin _rabbitAdmin; public WriteMessageQueueController(ILogger<WriteMessageQueueController> logger, RabbitTemplate rabbitTemplate, RabbitAdmin rabbitAdmin) { _logger = logger; _rabbitTemplate = rabbitTemplate; _rabbitAdmin = rabbitAdmin; } [HttpGet()] public ActionResult<string> Index() { var msg = "Hi there from over here."; _rabbitTemplate.ConvertAndSend(RECEIVE_AND_CONVERT_QUEUE, msg); _logger.LogInformation($"Sending message '{msg}' to queue '{RECEIVE_AND_CONVERT_QUEUE}'"); return "Message sent to queue."; } }
Validate the port number the app will be served on, in Properties\launchSettings.json
"iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:8080", "sslPort": 0 } }
Run the application (you won't be interacting directly with it, we just need it running in the background)
dotnet run<PATH_TO>\WriteToRabbitMQ.csproj
Note
Minimize windows and leave the application running as you continue on to the next step.
Now create a .NET Core WebAPI that will monitor the queue and output anything received.
Create a new ASP.NET Core WebAPI app with the Steeltoe Initializr
Name the project "MonitorRabbitMQ"
No need to add any dependencies
Click Generate Project to download a zip containing the new project
Extract the zipped project and open in your IDE of choice
Tip
Open the second app in a different Visual Studio instance.
Open the package manager console
Install NuGet distributed packages
Install-Package -Id Steeltoe.Messaging.RabbitMQ
Create a new service class named RabbitListenerService.cs
using Microsoft.Extensions.Logging; using Steeltoe.Messaging.RabbitMQ.Attributes; public class RabbitListenerService { public const string RECEIVE_AND_CONVERT_QUEUE = "steeltoe_message_queue"; private ILogger _logger; public RabbitListenerService(ILogger<RabbitListenerService> logger) { _logger = logger; } [RabbitListener(RECEIVE_AND_CONVERT_QUEUE)] public void ListenForAMessage(string msg) { _logger.LogInformation($"Received the message '{msg}' from the queue."); } }
Add rabbit messaging configurations to Startup.cs
using Steeltoe.Messaging.RabbitMQ.Extensions; public class Startup { public const string RECEIVE_AND_CONVERT_QUEUE = "steeltoe_message_queue"; public void ConfigureServices(IServiceCollection services){ // Add steeltoe rabbit services services.AddRabbitServices(); // Add singleton that will process incoming messages services.AddSingleton<RabbitListenerService>(); // Tell steeltoe about singleton so it can wire up queues with methods to process queues services.AddRabbitListeners<RabbitListenerService>(); } }
Validate the port number the app will be served on, in Properties\launchSettings.json
"iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:8081", "sslPort": 0 } }
Run the application
dotnet run<PATH_TO>\WriteToRabbitMQ.csproj
Note
If a browser window popped up, minimize it.
Validate the apps are working properly and the message queue is in use.
- View the WriteToRabbitMQ project message logs and verify there is a message stating it is "Sending message to queue". If you don't see the message refresh the endpoint
https://localhost:8080/WriteMessageQueue
to have a new message written. - View the MonitorRabbitMQ project message logs and verify there is a message stating it "Received the message from the queue".