
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 CentOS1.1. Installing Docker1.2. Running RabbitMQ Container with Custom Username and Password2.Creating a Web Application to Send Messages to RabbitMQ2.1. Installing RabbitMQ.Client Library2.2. Sending a Message to RabbitMQ3.Creating a Desktop Application to Receive Messages from RabbitMQ3.1. Installing RabbitMQ.Client Library3.2. Receiving a Message from RabbitMQ4. Advanced RabbitMQ Configuration and Management4.1. Monitor and Manage RabbitMQ4.2. Scaling RabbitMQ for High Availability5.Conclusion
To get started, we need to install Docker on our CentOS system if it’s not already installed:
sudo yum install -y yum-utilssudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.reposudo yum install -y docker-ce docker-ce-cli containerd.io
Now, start and enable the Docker service:
sudo systemctl start dockersudo systemctl enable docker
Next, pull the RabbitMQ image from Docker Hub and run the container with a custom username and password:
sudo docker pull rabbitmq:3-managementsudo 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
In your web application, install the RabbitMQ.Client library using the NuGet package manager:
Install-Package RabbitMQ.Client
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);}
Install the RabbitMQ.Client library in your desktop application using the NuGet package manager, just like you did for your web application.
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.
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:
For more information on the RabbitMQ Management plugin and its features, refer to the official documentation: https://www.rabbitmq.com/management.html
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:
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.
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
.
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.
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
.
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.
For more information and resources about the technologies used in this tutorial, you can refer to the following links:
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.
If you encounter any issues while following this tutorial, consider checking the following common problems and their solutions:
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:
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!
Quick Links
Legal Stuff