How to bridge RabbitMQ with Service Bus
Tags: azure, servicebus, rabbitmq, architecture
Published: 2020-06-22 23:43:48

---
title: How to bridge RabbitMQ with Service Bus
published: true
description: 
tags: azure, servicebus, rabbitmq, architecture
//cover_image: https://direct_url_to_image.jpg
---

In this article we are going to learn how to send messages from RabbitMQ to Azure Service Bus. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z5t8lfqaqzjof414kbml.png)

Here's a few scenarios in which we can make use of these capabilities:

- **Edge Setups**: We have an edge setup where we are sending messages to RabbitMQ, but we want to forward those messages to [Azure Service Bus](https://docs.microsoft.com/azure/service-bus-messaging/service-bus-messaging-overview?WT.mc_id=devto-blog-alvidela) for further processing, so we can use many of the [Azure Big Data capabilities](https://docs.microsoft.com/azure/architecture/guide/architecture-styles/big-data?WT.mc_id=devto-blog-alvidela).
- **Hybrid Cloud**: Your company just acquired a third party that uses RabbitMQ for their messaging needs. They are on a different cloud. While they transition to Azure you can already start sharing data by bridging RabbitMQ with Service Bus.
- **Third Party Integration**: A third party uses RabbitMQ as a broker, and wants to send their data to us, but they are outside our organization. We can provide them with SAS Key giving them access to a limited set of Service Bus queues where they can forward their messages to.

The list goes on, but we can solve most of these use cases by bridging RabbitMQ to Azure.

First you need to create a free Azure account by signing up [here](https://azure.microsoft.com/free/?WT.mc_id=devto-blog-alvidela)

Once you are signed in to your account go to the azure [portal](https://portal.azure.com/) and create a new Service Bus [namespace](https://docs.microsoft.com/azure/service-bus-messaging/service-bus-create-namespace-portal?WT.mc_id=devto-blog-alvidela). Namespaces are the scoping containers where our messaging components will live, like queues and topics.

## Adding a new Service Bus Namespace

In Azure Portal click the big plus button to add a new resource

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cmgjowwe1o9kf691lejz.png)

Then select Integration and click on Service Bus to create a messaging namespace:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2ipzjx3zzxu1odsyb9px.png)

You will be prompted to enter the information for the namespace. So first select the Azure subscription you want to use, and create a new resource group if you don't have one already (more on resource groups [here](https://docs.microsoft.com/azure/azure-resource-manager/management/manage-resource-groups-portal?WT.mc_id=devto-blog-alvidela)).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/10we4barufpnj139zken.png)

For `Namespace name` you can use `rabbitmq` but it could be anything you want. Then set `East US` for the location, and in this case take the `Basic` price tier.

If all went well you should see the following confirmation screen:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/myd5upz1nrou900wk36c.png)

Then back at the Azure Portal you'll see your new `rabbitmq` namespace listed there. Click on it to access the resource so you can add a queue to it.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kylm53n5m4ch20v3i13z.png)

## Creating our Service Bus Queue

Now that you have your service bus namespace, click on the `Queues` button on the left, under `Entities`, so you can add a new queue:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kzen18ehw875fifaxf76.png)

The name of the queue will be `from-rabbitmq` just as a reminder to where are the messages coming from. You can leave all the other options as defaults, but of course you can change them to fit the needs of your app.

## Enabling the RabbitMQ Shovel Plugin

To ship messages from RabbitMQ to Azure Service Bus we are going to use the [Shovel Plugin](https://www.rabbitmq.com/shovel.html) that comes packaged with RabbitMQ. You can enable it with the following command (you might need to run this command with administrator privileges or via the rabbitmq user):

```bash
rabbitmq-plugins enable rabbitmq_shovel_management
```

>Actually that command enables the visual interface for the plugin, which ends up enabling the shovel plugin for us.

Now is time to get the credentials required for connecting RabbitMQ to Azure.

## Connecting RabbitMQ to Azure Service Bus

You'll need to create a [Shared Access Policy](https://docs.microsoft.com/en-us/azure/storage/common/storage-sas-overview?WT.mc_id=devto-blog-alvidela) (SAS) for your queue, so RabbitMQ can publish messages to it. A SAS Policy will allow us to specify how much an external party can do with our resource. In this case the idea is that RabbitMQ is just able to send messages, but not listen to the queue, or to manage the queue.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rnuz2716uoc6ei2vi6lg.png)

So in this case tick the `Send` box and then click `Create` to have our SAS Policy in place.

Once the policy has been created click on it to see the **Primary Connection String** which is what you are going to use to let RabbitMQ talk to Azure Service Bus:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/revcrtlfzmoenoxoi37o.png)

Before you can use that connection string, you'll need to convert it to RabbitMQ's AMQP connection format. I created a basic website to ease that process. So go [here](https://red-mushroom-0f7446a0f.azurestaticapps.net/) and paste your connection string in the form, click convert, and you will get a connection string that is RabbitMQ ready. (Don't worry, that website runs everything in your browser so your data is safe). If you wanna be sure, the source code is on [GitHub](https://github.com/videlalvaro/connstring_to_amqp).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/diyn24ql63wfatlehck4.png)

Now open the RabbitMQ management plugin in our browsers [http://localhost:15672/#/dynamic-shovels](http://localhost:15672/#/dynamic-shovels) and go to `Admin -> Shovel Management`, where you can add your new shovel that will take care of sending messages from a RabbitMQ queue to your Azure Service Bus queue.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cjpbx5o6kef7985ngiji.png)

Here name your shovel `azure` and choose `AMQP 0.9.1` as the source protocol. This tells the shovel plugin how to connect to your local RabbitMQ server. In the screenshot we have `amqp://` which is the default URI that connects us to a local RabbitMQ server. Make sure to adapt that to your current deployment. 

On the queue side of things, specify the name of your queue. In this case I used `azure` which is the name of a queue that I had already created in RabbitMQ. If you don't have that queue you can create it on the RabbitMQ management plugin, under the **Queues** tab. You can leave the other options as default.

Then on the `destination` side of things, we are going to choose `AMQP 1.0` as the protocol. In the `URI` field we'll enter the connecting string that we got from the previous step that converted our Azure connection string to our RabbitMQ format. It should look like this:

```
amqps://rabbitmq-shovel:StringOfRandomChars@rabbitmq.servicebus.windows.net:5671/?sasl=plain
```

In the `Address` field we'll enter the name of our **Service Bus Queue**, in this case, it was called `from-rabbitmq`. Click `Add Shovel`, and our set up should be ready to start receiving messages.

## Publishing Messages from RabbitMQ to Azure Service Bus

In the RabbitMQ Management interface we can go to `Queues`, select the `azure` queue, and search for the `Publish message` panel. There a form will appear that will let you publish messages directly to our queue. For our example we are just going to add `fist message` as the `Payload` and hit `Publish Message`:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k918mdi6rmzvl7pc9z2f.png)

Go back to Azure and inspect your queue. Click `Service Bus Explorer` in the left panel. If all went well you'll see your queue now has one message. Yay, congrats!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/flto7xucw2snubkhxlcl.png)

But let's make sure that message is the one you sent from RabbitMQ. Select the `Peek` tab and click the `Peek` button to retrieve the last messages in your queue. Click on the message to inspect its contents. You should see something like the image below where your `first message` is listed.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/stbk7ttrttcatoduckvv.png)

## Let's Recap

Congrats! We did a lot and we managed to get our messages from RabbitMQ to Azure Service Bus, let's recap the steps:

1. Create an Azure Service Bus Namespace
2. Add a queue to the namespace
3. Add a SAS Policy to our queue
4. Get the queue connection string
5. Enable the RabbitMQ shovel plugin & the management interface
6. Convert the Azure Service Bus connection string to RabbitMQ's AMQP format
7. Add a new shovel to RabbitMQ
8. Publish messages

By following the previous steps you were able to integrate areas of your org that might be outside of Azure, by shipping messages from RabbitMQ to Azure Service Bus using the Shovel Plugin. This has enormous advantages since we can even allow trusted third parties to connect their apps with our Azure deployment.

In the end, messaging is about enabling connections, and with this technique we just opened a new one.