A composition of RabbitMQ examples for C# .NET developers.
This project has a specific focus on demonstrating how to use RabbitMQ with C#.NET 5. This is achieved by providing a collection of practical examples (written in C#.NET 5) that highlight the following messaging patterns.
One should have a basic understanding of RabbitMQ and its underlying protocol AMQP. I therefore provide a curated list of RabbitMQ resources to help you on your RabbitMQ journey.
CloudAMQP offers an abundance of GREAT RabbitMQ resources.
- Part 1: RabbitMQ for beginners - What is RabbitMQ?
- Part 2: RabbitMQ for beginners - Sample code
- Part 3: The RabbitMQ Management Interface
- Part 4: RabbitMQ Exchanges, routing keys and bindings
- Part 1: RabbitMQ Best Practices
- Part 2: RabbitMQ Best Practice for High Performance (High Throughput)
- Part 3: RabbitMQ Best Practice for High Availability
From Docker to Cloud, there are a number of options that can be used to start playing with RabbitMQ. Try out any of the following methods to get started:
Before we start looking at RabbitMQ hosting options, I suggest installing the RabbitMQ CLI as it allows you to easily connect to any RabbitMQ server. Therefore, please view the RabbitMQ Client section to learn more
In the following sections, a number of examples will use the rabbitmqadmin
CLI tool to manage RabbitMQ. Therefore, before getting into the "server side" of RabbitMQ, please follow the setup instructions in this section to install the rabbitmqadmin
CLI tool.
According to the official documention (Management Command Line Tool), the rabbitmqadmin
tool allows for the following management tasks:
- list exchanges, queues, bindings, vhosts, users, permissions, connections and channels
- show overview information
- declare and delete exchanges, queues, bindings, vhosts, users and permissions
- publish and get messages
- close connections and purge queues
- import and export configuration
The listing above is a summarized list of what can be done with the rabbitmqadmin
CLI tool. For more information, see the official documentation. Also, once rabbitmqadmin
is installed, be sure to try the following commands to get more information:
# get general help
rabbitmqadmin --help
# get help on subcommands
rabbitmqadmin help subcommands
The following setup instructions should work for most Debian based Linux distributions (Including those running on Windows Subsystem for Linux 2 (WSL2)).
# ensure that Python version 3 is installed
python3 --version
# get 'rabbitmqadmin' CLI tool
sudo wget https://raw.githubusercontent.com/rabbitmq/rabbitmq-server/v3.8.14/deps/rabbitmq_management/bin/rabbitmqadmin -O /usr/local/bin/rabbitmqadmin
sudo chmod +x /usr/local/bin/rabbitmqadmin
# verify 'rabbitmqadmin' setup
rabbitmqadmin --version
# get help on 'rabbitmqadmin'
rabbitmqadmin --help
rabbitmqadmin help subcommands
The following setup instructions will work in Windows 10 using Powershell. The preference is to use Powershell Version 7. It will also be required to update the Windows 10 Path environment variable. Please see Add To Windows 10 Path Environment Variable for more information.
# ensure that Python version 3 is installed
python.exe --version
# get 'rabbitmqadmin' CLI tool
mkdir ~/rabbitmq-tools
Invoke-WebRequest -Uri https://raw.githubusercontent.com/rabbitmq/rabbitmq-server/v3.8.14/deps/rabbitmq_management/bin/rabbitmqadmin -OutFile ~/rabbitmq-tools/rabbitmqadmin -UseBasicParsing
# add '~/rabbitmq-tools' to your PATH environment variable. See https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10
# try the following command to verify that '~/rabbitmq-tools' is included in the PATH environment variable
$env:Path -split ';' | findstr -i rabbitmq-tools
# verify 'rabbitmqadmin' setup
python.exe rabbitmqadmin --version
# get help on 'rabbitmqadmin'
python.exe rabbitmqadmin --help
python.exe rabbitmqadmin help subcommands
Use the Docker CLI to start a new RabbitMQ container instance.
# create docker network
docker network create rabbit-net
# run RabbitMQ container
docker container run \
--detach \
--name rabbit1 \
--hostname rabbit1 \
--publish 15672:15672 \
--publish 5672:5672 \
--env RABBITMQ_ERLANG_COOKIE='cookie_for_clustering' \
--env RABBITMQ_DEFAULT_USER=admin \
--env RABBITMQ_DEFAULT_PASS=password \
--network rabbit-net \
rabbitmq:3.8-management-alpine
# connect to rabbitmq server
rabbitmqadmin -u admin -p password list vhosts
rabbitmqadmin -u admin -p password list exchanges
# open the following url in your browser
http://localhost:15672
# enter credentials
username: admin
password: password
Define a RabbitMQ stack using a docker-compose.yml
file.
version: "3.7"
services:
rabbit1:
image: rabbitmq:3.8-management-alpine
container_name: rabbit1
hostname: rabbit1
restart: unless-stopped
ports:
- 5672:5672
- 15672:15672
environment:
RABBITMQ_ERLANG_COOKIE: cookie_for_clustering
RABBITMQ_DEFAULT_USER: admin
RABBITMQ_DEFAULT_PASS: password
volumes:
- rabbit1-etc:/etc/rabbitmq/
- rabbit1-data:/var/lib/rabbitmq/
- rabbit1-logs:/var/log/rabbitmq/
volumes:
rabbit1-etc:
rabbit1-data:
rabbit1-logs:
networks:
default:
name: rabbit-net
Manage the RabbitMQ stack using docker-compose
CLI tool.
# start RabbitMQ stack
docker-compose up -d
# stop RabbitMQ stack
docker-compose down -v
# list containers
docker-compose ps
# connect to rabbitmq server
rabbitmqadmin -u admin -p password list vhosts
rabbitmqadmin -u admin -p password list exchanges
# connect to rabbitmq server
rabbitmqadmin -u admin -p password list vhosts
rabbitmqadmin -u admin -p password list exchanges
# open the following url in your browser
http://localhost:15672
# enter credentials
username: admin
password: password
Please find the install for your platform (OS) of choice.
This is the easiest way to get up and rinning with RabbitMQ. No installation or setup is required. All one needs to do is head over to CloudAMQP CloudAMQP is a cloud provider of various "products as a service" such as RabbitMQ, Apache Kafka, and ElephantSQL. To get started using 'RabbitMQ as a Service', see the following links:
# list exchanges
rabbitmqadmin --host=your-custom-host.rmq.cloudamqp.com --port=443 --ssl --username=YOUR_USERNAME --password=YOUR_PASSWORD -V YOUR_USERNAME list exchanges
# create queue
rabbitmqadmin --host=your-custom-host.rmq.cloudamqp.com --port=443 --ssl --username=YOUR_USERNAME --password=YOUR_PASSWORD -V YOUR_USERNAME declare queue name=example.messages auto_delete=false durable=false
# list queues
rabbitmqadmin --host=your-custom-host.rmq.cloudamqp.com --port=443 --ssl --username=YOUR_USERNAME --password=YOUR_PASSWORD -V YOUR_USERNAME list queues
# publish message to queue
rabbitmqadmin --host=your-custom-host.rmq.cloudamqp.com --port=443 --ssl --username=YOUR_USERNAME --password=YOUR_PASSWORD -V YOUR_USERNAME publish routing_key=example.messages payload="Hello World"
# list messages in queue
rabbitmqadmin --host=your-custom-host.rmq.cloudamqp.com --port=443 --ssl --username=YOUR_USERNAME --password=YOUR_PASSWORD -V YOUR_USERNAME get queue=messages count=1
# purge queue
rabbitmqadmin --host=your-custom-host.rmq.cloudamqp.com --port=443 --ssl --username=YOUR_USERNAME --password=YOUR_PASSWORD -V YOUR_USERNAME purge queue name=example.messages
# delete queue
rabbitmqadmin --host=your-custom-host.rmq.cloudamqp.com --port=443 --ssl --username=YOUR_USERNAME --password=YOUR_PASSWORD -V YOUR_USERNAME delete queue name=example.messages