HomeContact

Setting Up RabbitMQ with Docker and Creating Web and Desktop Applications

By Shady Nagy
Published in dotnet
April 11, 2023
4 min read
Setting Up RabbitMQ with Docker and Creating Web and Desktop Applications

Table Of Contents

01
Introduction:
02
Setting Up RabbitMQ with Docker on CentOS
03
Creating a Web Application to Send Messages to RabbitMQ
04
Creating a Desktop Application to Receive Messages from RabbitMQ
05
Advanced RabbitMQ Configuration and Management
06
Further Reading:
07
Conclusion
08
Troubleshooting:
09
Feedback and Questions:

Introduction:

In this blog post, we will demonstrate how to set up RabbitMQ with Docker on a CentOS system and create a web application and desktop application to send and receive messages using RabbitMQ. We will also show you how to configure RabbitMQ with a custom username and password for added security.

Table of Contents:

1.Setting Up RabbitMQ with Docker on CentOS
1.1. Installing Docker
1.2. Running RabbitMQ Container with Custom Username and Password
2.Creating a Web Application to Send Messages to RabbitMQ
2.1. Installing RabbitMQ.Client Library
2.2. Sending a Message to RabbitMQ
3.Creating a Desktop Application to Receive Messages from RabbitMQ
3.1. Installing RabbitMQ.Client Library
3.2. Receiving a Message from RabbitMQ
4. Advanced RabbitMQ Configuration and Management
4.1. Monitor and Manage RabbitMQ
4.2. Scaling RabbitMQ for High Availability
5.Conclusion

Setting Up RabbitMQ with Docker on CentOS

1.1. Installing Docker

To get started, we need to install Docker on our CentOS system if it’s not already installed:

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io

Now, start and enable the Docker service:

sudo systemctl start docker
sudo systemctl enable docker

1.2. Running RabbitMQ Container with Custom Username and Password

Next, pull the RabbitMQ image from Docker Hub and run the container with a custom username and password:

sudo docker pull rabbitmq:3-management
sudo docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 -e RABBITMQ_DEFAULT_USER=myuser -e RABBITMQ_DEFAULT_PASS=mypassword rabbitmq:3-management

Replace myuser and mypassword with your desired username and password.

If you want to setup the firewall then follow this post https://shadynagy.com/rocky-firewall-using-firewalld

Creating a Web Application to Send Messages to RabbitMQ

2.1. Installing RabbitMQ.Client Library

In your web application, install the RabbitMQ.Client library using the NuGet package manager:

Install-Package RabbitMQ.Client

2.2. Sending a Message to RabbitMQ

Use the following code to send a message to RabbitMQ from your web application:

using RabbitMQ.Client;
using System.Text;
public void SendMessage(string message)
{
var factory = new ConnectionFactory()
{
HostName = "your_centos_ip",
UserName = "myuser",
Password = "mypassword"
};
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.QueueDeclare(queue: "myQueue",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey: "myQueue",
basicProperties: null,
body: body);
}

Creating a Desktop Application to Receive Messages from RabbitMQ

3.1. Installing RabbitMQ.Client Library

Install the RabbitMQ.Client library in your desktop application using the NuGet package manager, just like you did for your web application.

3.2. Receiving a Message from RabbitMQ

Use the following code to receive a message from RabbitMQ in your desktop application:

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
public void ReceiveMessage()
{
var factory = new ConnectionFactory()
{
HostName = "your_centos_ip",
UserName = "myuser",
Password = "mypassword"
};
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.QueueDeclare(queue: "myQueue",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
// Process the message, protect the .exe, and email the user
};
channel.BasicConsume(queue: "myQueue",
autoAck: true,
consumer: consumer);
Console.ReadLine(); // Keep the application running to listen for messages
}

Replace “your_centos_ip” with the IP address of your CentOS machine running the Docker container, and “myuser” and “mypassword” with the custom username and password you provided when starting the RabbitMQ container.

Advanced RabbitMQ Configuration and Management

4.1. Monitor and Manage RabbitMQ

In addition to sending and receiving messages, you might want to monitor and manage your RabbitMQ instance. The RabbitMQ Management plugin provides an HTTP-based API and a web interface for monitoring, managing, and configuring your RabbitMQ server. The plugin is included by default in the RabbitMQ Docker image we used in this tutorial.

To access the RabbitMQ Management web interface, open your web browser and navigate to http://your_centos_ip:15672/. Log in with the custom username and password you provided when starting the RabbitMQ container.

In the RabbitMQ Management web interface, you can:

  1. Monitor queues, exchanges, and bindings
  2. Manage users and permissions
  3. View detailed server statistics and metrics
  4. Perform various administrative tasks, such as creating and deleting queues or exchanges

For more information on the RabbitMQ Management plugin and its features, refer to the official documentation: https://www.rabbitmq.com/management.html

4.2. Scaling RabbitMQ for High Availability

RabbitMQ supports clustering and message replication for high availability and fault tolerance. By setting up a RabbitMQ cluster, you can distribute queues, exchanges, and messages across multiple nodes, providing redundancy and load balancing for your messaging system.

To set up a RabbitMQ cluster with Docker, follow these steps:

  1. Configure the RabbitMQ nodes to use the same Erlang cookie: The Erlang cookie is a shared secret used to authenticate communication between nodes in a RabbitMQ cluster. When starting your RabbitMQ containers, pass the -e RABBITMQ_ERLANG_COOKIE='mycookie' argument, replacing ‘mycookie’ with your desired Erlang cookie value.

  2. Set up a Docker network: Create a Docker network to enable communication between your RabbitMQ containers. You can create a Docker network using the command sudo docker network create rabbitmq-network.

  3. Run RabbitMQ containers on the same Docker network: When starting your RabbitMQ containers, add the --net rabbitmq-network argument to connect them to the Docker network you created.

  4. Configure RabbitMQ nodes to form a cluster: After starting your RabbitMQ containers, you can use the rabbitmqctl command-line tool to join the nodes together into a cluster. For example, to join node B to node A, run the following command in node B’s container: rabbitmqctl join_cluster rabbit@node_A_container_name.

  5. Enable message replication (optional): If you want to replicate messages across nodes for added fault tolerance, you can configure RabbitMQ’s built-in queue mirroring feature. To enable mirroring for a queue, set its ‘x-ha-policy’ argument to ‘all’: channel.QueueDeclare(queue: "myQueue", arguments: new Dictionary<string, object> { { "x-ha-policy", "all" } });

For more information on clustering and high availability in RabbitMQ, refer to the official documentation: https://www.rabbitmq.com/clustering.html and https://www.rabbitmq.com/ha.html

By following the steps above, you can create a scalable and highly available RabbitMQ messaging system for your web and desktop applications, ensuring optimal performance and reliability.

Further Reading:

For more information and resources about the technologies used in this tutorial, you can refer to the following links:

  1. RabbitMQ Official Documentation: Explore the extensive documentation on RabbitMQ’s features, configuration, and best practices: https://www.rabbitmq.com/documentation.html
  2. Docker Official Documentation: Learn more about Docker, its installation, and usage: https://docs.docker.com/
  3. RabbitMQ.Client Library: Check out the .NET RabbitMQ.Client library documentation and examples: https://www.nuget.org/packages/RabbitMQ.Client/
  4. RabbitMQ Tutorials: Gain in-depth knowledge of RabbitMQ and its various use cases through these tutorials: https://www.rabbitmq.com/getstarted.html

Conclusion

In this blog post, we showed you how to set up RabbitMQ with Docker on a CentOS system and create a web application and desktop application to send and receive messages using RabbitMQ. We also demonstrated how to configure RabbitMQ with a custom username and password for added security. This setup provides a scalable and secure messaging solution for your applications.

Troubleshooting:

If you encounter any issues while following this tutorial, consider checking the following common problems and their solutions:

  1. Cannot connect to RabbitMQ server: Ensure that your CentOS machine’s firewall allows incoming connections on ports 5672 and 15672. You can temporarily disable the firewall for testing using the command sudo systemctl stop firewalld.
  2. RabbitMQ container not starting: Check Docker logs using sudo docker logs rabbitmq for any error messages. Ensure that your Docker installation is up-to-date and that you’ve pulled the correct RabbitMQ image.

Feedback and Questions:

We’d love to hear your feedback on this tutorial! If you have any questions or suggestions for improvement, please don’t hesitate to reach out. You can leave a comment below, or you can contact us through the following channels:

  1. Email: shady@shadynagy.com
  2. Twitter: @ShadyNagy_
  3. LinkedIn: Shady Nagy
  4. GitHub: ShadyNagy

We’ll do our best to address any questions or concerns you may have. We look forward to hearing from you and helping you make the most of RabbitMQ, Docker, and your web and desktop applications!


Tags

#RabbitMQ#microsoft#dotnet6#dotnet#dotnet core#C##csharplinuxcentos

Share


Previous Article
Managing Multiple Environments in Angular Applications
Shady Nagy

Shady Nagy

Software Innovation Architect

Topics

AI
Angular
dotnet
GatsbyJS
Github
Linux
MS SQL
Oracle

Related Posts

Integrating Hangfire with RabbitMQ for Effective Message Processing in .NET Applications
Integrating Hangfire with RabbitMQ for Effective Message Processing in .NET Applications
January 05, 2024
5 min

Quick Links

Contact Us

Social Media