drminnaar / dotnet-rabbitmq

A composition of RabbitMQ examples for C# .NET developers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dotnet-rabbitmq-github

RabbitMQ for .NET Developers

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.


Pre-Requisites

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.

Official RabbitMQ Resources

Documentation

Tutorials

Erlang Solutions

Blogs

AMQP Resources

CloudAMQP offers an abundance of GREAT RabbitMQ resources.

Miscallaneous

Fundamentals Tutorials

Best Practice Tutorials


Getting Started

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


RabbitMQ Client

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

Linux Installation and Setup

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

Windows Setup

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

RabbitMQ Server

Docker

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 Using rabbitmqadmin

# connect to rabbitmq server
rabbitmqadmin -u admin -p password list vhosts
rabbitmqadmin -u admin -p password list exchanges

Connect Using Web RabbitMQ Manager

# open the following url in your browser
http://localhost:15672

# enter credentials
username: admin
password: password

Docker Compose

Definition

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

Execution

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 Using rabbitmqadmin

# connect to rabbitmq server
rabbitmqadmin -u admin -p password list vhosts
rabbitmqadmin -u admin -p password list exchanges

Connect Using Web RabbitMQ Manager

# open the following url in your browser
http://localhost:15672

# enter credentials
username: admin
password: password

Local

Please find the install for your platform (OS) of choice.

CloudAMQP

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:

cloudamqp-dashboard

Connect Using Web Admin

cloudamqp-manager-1

cloudamqp-manager-2

Connect Using CLI

# 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

About

A composition of RabbitMQ examples for C# .NET developers


Languages

Language:C# 100.0%