Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

DOTNET: RabbitMQ with .net Demo and Lab Example

Weโ€™ll run RabbitMQ in Docker on an AWS EC2 instance, and use your Windows laptop for both the producer and consumer.

Iโ€™ll walk you through:

  1. Create & configure EC2
  2. Install Docker & run RabbitMQ container
  3. Securely expose RabbitMQ over IP
  4. Write .NET Producer (Windows)
  5. Write .NET Consumer (Windows)
  6. Test end-to-end

1. Provision AWS EC2 for RabbitMQ

1.1 Launch EC2 instance

In AWS Console:

  1. Go to EC2 โ†’ Instances โ†’ Launch instance.
  2. Name: rabbitmq-ec2.
  3. AMI:
    • Ubuntu Server 22.04 LTS (or Amazon Linux if you prefer).
  4. Instance type: t3.small or t3.micro is OK for demo.
  5. Key Pair: select or create one (for SSH).
  6. Network:
    • VPC: default (or your custom VPC).
    • Subnet: any public subnet.
    • Auto-assign Public IP: Enable โœ… (important).

1.2 Security group ports

Create or configure a security group, e.g., rabbitmq-sg, with:

  • SSH: TCP 22, Source = your IP only (e.g., x.x.x.x/32).
  • RabbitMQ AMQP: TCP 5672, Source = your IP (for .NET code) or office IP range.
  • RabbitMQ Management UI: TCP 15672, Source = your IP (for browser access).

Donโ€™t leave 0.0.0.0/0 open in production. For demo itโ€™s โ€œokay-ishโ€, but better to restrict.

Launch the instance.


2. Install Docker & run RabbitMQ on EC2

2.1 SSH into the EC2 instance

From your Windows laptop:

ssh -i "your-key.pem" ubuntu@EC2_PUBLIC_IP
Code language: CSS (css)

(Replace your-key.pem path and EC2_PUBLIC_IP with the instanceโ€™s public IP.)

2.2 Install Docker (Ubuntu example)

# Update packages
sudo apt-get update

# Install Docker
sudo apt-get install -y docker.io

# Enable & start Docker
sudo systemctl enable docker
sudo systemctl start docker

# Verify
docker --version
Code language: PHP (php)

2.3 Run RabbitMQ Docker container (with remote access)

Important: the default guest user cannot log in remotely (only from localhost), so weโ€™ll create a new user.

Run RabbitMQ with management:

sudo docker run -d \
  --hostname my-rabbit \
  --name rabbitmq-demo \
  -p 5672:5672 \
  -p 15672:15672 \
  rabbitmq:3-management
Code language: CSS (css)

Wait a few seconds, then:

sudo docker ps

You should see rabbitmq:3-management running.

2.4 Create a dedicated user for remote access

Connect into the container:

sudo docker exec -it rabbitmq-demo bash

Inside the container:

# Add a user - choose your own username & password
rabbitmqctl add_user myuser mypassword

# Give full permissions on the default vhost "/"
rabbitmqctl set_permissions -p / myuser ".*" ".*" ".*"

# (Optional) Give administrator tag for UI access
rabbitmqctl set_user_tags myuser administrator

exit
Code language: PHP (php)

Now youโ€™ll use myuser / mypassword from Windows.


3. Verify RabbitMQ Management UI from your laptop

On your Windows laptop, open browser:

http://EC2_PUBLIC_IP:15672
Code language: JavaScript (javascript)

Log in with:

  • Username: myuser
  • Password: mypassword

You should see the RabbitMQ dashboard ๐ŸŽ‰


4. Prepare .NET projects on Windows laptop

Weโ€™ll create two console apps on your laptop:

  • RabbitMqProducer โ€“ sends messages to EC2 RabbitMQ
  • RabbitMqConsumer โ€“ receives & processes them

4.1 Create solution & projects

In PowerShell / CMD:

mkdir RabbitMqEc2Demo
cd RabbitMqEc2Demo

dotnet new sln -n RabbitMqEc2Demo

dotnet new console -n RabbitMqProducer
dotnet new console -n RabbitMqConsumer

dotnet sln add RabbitMqProducer/RabbitMqProducer.csproj
dotnet sln add RabbitMqConsumer/RabbitMqConsumer.csproj
Code language: JavaScript (javascript)

4.2 Install RabbitMQ client in both apps

cd RabbitMqProducer
dotnet add package RabbitMQ.Client
cd ..

cd RabbitMqConsumer
dotnet add package RabbitMQ.Client
cd ..
Code language: CSS (css)

5. Producer code (from Windows โ†’ EC2 RabbitMQ)

Edit RabbitMqProducer/Program.cs and replace with:

using System.Text;
using RabbitMQ.Client;

Console.WriteLine("RabbitMQ Producer starting...");

// Replace with your EC2 public IP or DNS
var rabbitMqHost = "EC2_PUBLIC_IP_OR_DNS";  // e.g. "3.110.45.123"
var queueName = "demo_queue";

var factory = new ConnectionFactory
{
    HostName = rabbitMqHost,
    Port = 5672,
    UserName = "myuser",      // user created on EC2 RabbitMQ
    Password = "mypassword",  // password you set
    VirtualHost = "/"         // default vhost
};

using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();

// Declare queue - safe to call many times
channel.QueueDeclare(
    queue: queueName,
    durable: true,
    exclusive: false,
    autoDelete: false,
    arguments: null);

Console.WriteLine($"Connected to RabbitMQ at {rabbitMqHost}. Queue: {queueName}");

for (int i = 1; i <= 10; i++)
{
    var message = $"Hello from Windows Producer! Message #{i}";
    var body = Encoding.UTF8.GetBytes(message);

    var properties = channel.CreateBasicProperties();
    properties.Persistent = true; // store on disk

    channel.BasicPublish(
        exchange: "",
        routingKey: queueName,
        basicProperties: properties,
        body: body);

    Console.WriteLine($"[x] Sent: {message}");
}

Console.WriteLine("All messages sent. Press any key to exit.");
Console.ReadKey();
Code language: JavaScript (javascript)

6. Consumer code (from Windows โ†’ EC2 RabbitMQ)

Edit RabbitMqConsumer/Program.cs and replace with:

using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

Console.WriteLine("RabbitMQ Consumer starting...");

// Same EC2 host & credentials
var rabbitMqHost = "EC2_PUBLIC_IP_OR_DNS";  // e.g. "3.110.45.123"
var queueName = "demo_queue";

var factory = new ConnectionFactory
{
    HostName = rabbitMqHost,
    Port = 5672,
    UserName = "myuser",
    Password = "mypassword",
    VirtualHost = "/",
    DispatchConsumersAsync = true
};

using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();

channel.QueueDeclare(
    queue: queueName,
    durable: true,
    exclusive: false,
    autoDelete: false,
    arguments: null);

// Optional QoS to process one message at a time
channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

Console.WriteLine($"Connected to RabbitMQ at {rabbitMqHost}. Waiting for messages on queue: {queueName}");

var consumer = new AsyncEventingBasicConsumer(channel);

consumer.Received += async (model, ea) =>
{
    var body = ea.Body.ToArray();
    var message = Encoding.UTF8.GetString(body);

    Console.WriteLine($"[x] Received: {message}");

    // Simulate some processing work
    await Task.Delay(500);

    // Acknowledge the message
    channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};

channel.BasicConsume(
    queue: queueName,
    autoAck: false,
    consumer: consumer);

Console.WriteLine("Press [enter] to exit.");
Console.ReadLine();
Code language: JavaScript (javascript)

7. Run the full flow

7.1 Start the consumer (Windows)

Open terminal on your laptop:

cd RabbitMqEc2Demo/RabbitMqConsumer
dotnet run

You should see:

RabbitMQ Consumer starting...
Connected to RabbitMQ at EC2_PUBLIC_IP_OR_DNS. Waiting for messages on queue: demo_queue
Press [enter] to exit.
Code language: CSS (css)

(Queue might be empty initially.)

7.2 Run the producer (Windows)

Open another terminal:

cd RabbitMqEc2Demo/RabbitMqProducer
dotnet run

You should see:

RabbitMQ Producer starting...
Connected to RabbitMQ at EC2_PUBLIC_IP_OR_DNS. Queue: demo_queue
[x] Sent: Hello from Windows Producer! Message #1
...
[x] Sent: Hello from Windows Producer! Message #10
All messages sent. Press any key to exit.
Code language: CSS (css)

Now check the consumer terminal; you should see each message being received & processed:

[x] Received: Hello from Windows Producer! Message #1
[x] Received: Hello from Windows Producer! Message #2
...
Code language: CSS (css)

7.3 Inspect via RabbitMQ Management UI

In browser:

http://EC2_PUBLIC_IP:15672
Code language: JavaScript (javascript)

Log in with myuser / mypassword.

  • Go to Queues โ†’ click demo_queue
  • You can see:
    • Ready / unacked messages counts
    • Consumers count
    • Message rates

Try experiments:

  • Stop consumer, run producer โ†’ messages accumulate in queue.
  • Start consumer again โ†’ it drains messages from queue.

8. Hardening / Production Thoughts (for your training)

You can mention this in your tutorial:

  • Use non-default credentials, strong passwords.
  • Restrict Security Group to your office VPN / corporate IPs.
  • Consider TLS for RabbitMQ traffic (amqps).
  • Use separate vhosts for environments (dev / stage / prod).
  • Use Docker volumes or EBS volumes for persistent data.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

The DevOps Guide to Agentless Security: Scaling Protection without Breaking the Build

Today’s DevOps teams need to innovate, accelerate development, and minimize friction. In parallel, securing cloud-native environments is more challenging. Software now runs on containers, virtual machines, serverless,…

Read More

Top 10 Field Service Management (FSM) Software: Features, Pros, Cons & Comparison

Introduction Field Service Management (FSM) software is a category of business applications designed to help organizations plan, schedule, dispatch, track, and optimize field service operations. These tools…

Read More

How to Connect a WordPress Website Using an FTP Client?

Introduction -H2 Sometimes, during installing plugins or custom themes, people face issues of WordPress website breakdown. This happens due to the WordPress dashboard not accepting the new…

Read More

The Evolution of DevOps: Bridging the Gap Between Development and Operations

The Origins of DevOps The concept of DevOps emerged as a response to the traditional separation between software development and IT operations. Historically, these two disciplines operated…

Read More

B2B Gifting for DevOps and Engineering Teams: What Actually Works

Employee and client recognition is an established part of business culture, but for DevOps and engineering teams, the standard corporate gifting playbook rarely lands well. A generic…

Read More

How DevOps Teams Automate Ticket Creation from Monitoring and Backup Systems

There are 5,000 alerts generated every day in the average enterprise DevOps environment. But most of these alerts never reach a human until a system fails completely….

Read More
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Skylar Bennett
Skylar Bennett
4 months ago

This blog offers a practical and easy-to-follow introduction to using RabbitMQ with .NET, supported by clear demo and lab-style examples. The step-by-step approach helps developers understand message publishing, consumption, and queue handling in a real application context. By focusing on hands-on implementation rather than just theory, the article makes it easier to grasp asynchronous communication and decoupled system design. Overall, it is a useful resource for .NET developers who want to build reliable messaging-based applications and gain confidence working with RabbitMQ in real-world scenarios.

Jason Mitchell
Jason Mitchell
5 months ago

This is a great tutorial โ€” the detailed demo and lab example with RabbitMQ and .NET plainly show how message-based architecture can simplify asynchronous communication and improve scalability. Walking through queue setup, producer/consumer code, and handling message acknowledgments makes it much easier to grasp for developers new to messaging systems. I appreciate how the article balances theory with hands-on code โ€” that kind of practical approach helps teams adopt robust, event-driven patterns with confidence. Well done โ€” this is a valuable resource for anyone looking to implement reliable messaging in .NET applications.

2
0
Would love your thoughts, please comment.x
()
x