如何将 RabbitMQ 与 Azure 服务总线集成How to integrate RabbitMQ with Azure Service Bus

在本指南中,我们将学习如何将消息从 RabbitMQ 发送到 Azure 服务总线。In this guide, we're going to learn how to send messages from RabbitMQ to Azure Service Bus.

下面是一些可以利用这些功能的方案:Here's a few scenarios in which we can make use of these capabilities:

  • 边缘设置:我们有一种边缘设置,可以在其中将消息发送到 RabbitMQ,但我们希望将这些消息转发到 Azure 服务总线进行进一步的处理,以便使用许多 Azure 大数据功能Edge Setups: We have an edge setup where we're sending messages to RabbitMQ, but we want to forward those messages to Azure Service Bus for further processing, so we can use many of the Azure Big Data capabilities.
  • 混合云:你的公司刚收购了一家使用 RabbitMQ 来满足其消息传递需求的第三方公司。Hybrid Cloud: Your company just acquired a third party that uses RabbitMQ for their messaging needs. 它们在不同的云上。They are on a different cloud. 当它们过渡到 Azure 时,你就已经可以通过使用 Azure 服务总线桥接 RabbitMQ 来开始共享数据。While they transition to Azure you can already start sharing data by bridging RabbitMQ with Azure Service Bus.
  • 第三方集成:第三方使用 RabbitMQ 作为中转站,并想要将其数据发送给我们,但它们在我们的组织外部。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. 我们可以向其提供 SAS 密钥,使其能够访问用来转发消息的一组有限的 Azure 服务总线队列。We can provide them with SAS Key giving them access to a limited set of Azure Service Bus queues where they can forward their messages to.

列表还在继续扩充,但是我们可以通过将 RabbitMQ 桥接到 Azure 来解决大多数此类用例的问题。The list goes on, but we can solve most of these use cases by bridging RabbitMQ to Azure.

首先,你需要通过在此处注册来创建一个 Azure 试用帐户First you need to create a trial Azure account by signing up here

登录到你的帐户后,请转到 Azure 门户并创建一个新的 Azure 服务总线命名空间Once you're signed in to your account, go to the Azure portal and create a new Azure Service Bus namespace. 命名空间是消息传递组件(例如队列和主题)所在的作用域容器。Namespaces are the scoping containers where our messaging components will live, like queues and topics.

添加新的 Azure 服务总线命名空间Adding a new Azure Service Bus Namespace

在 Azure 门户中,单击大加号按钮来添加新资源In Azure portal, click the large plus button to add a new resource


然后选择“集成”并单击“Azure 服务总线”来创建消息命名空间:Then select Integration and click on Azure Service Bus to create a messaging namespace:

选择 Azure 服务总线

系统会提示你输入命名空间信息。You'll be prompted to enter the namespace information. 选择要使用的 Azure 订阅。Select the Azure subscription you want to use. 如果你没有资源组,可新建一个。If you don't have a resource group, you can create a new one.


对于“Namespace name”,请使用 rabbitmq,但它可以是你想要的任何名称。Use rabbitmq for Namespace name, but it could be anything you want. 然后,设置“China East”作为位置。Then set China East for the location. 选择“Basic”作为定价层。Choose Basic as the price tier.

如果一切正常,你应该会看到以下确认屏幕:If all went well, you should see the following confirmation screen:


然后返回到 Azure 门户,此时会看到其中列出了新的 rabbitmq 命名空间。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.


创建我们的 Azure 服务总线队列Creating our Azure Service Bus Queue

现在,你已有 Azure 服务总线命名空间,请单击左侧“Entities”下的“Queues”按钮,以便添加新队列:Now that you have your Azure Service Bus namespace, click on the Queues button on the left, under Entities, so you can add a new queue:


队列的名称将为 from-rabbitmq,只是为了提醒消息来自何处。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 you can change them to fit the needs of your app.

启用 RabbitMQ Shovel 插件Enabling the RabbitMQ Shovel Plugin

为了将消息从 RabbitMQ 发送到 Azure 服务总线,我们打算使用随 RabbitMQ 打包的 Shovel 插件To ship messages from RabbitMQ to Azure Service Bus, we're going to use the Shovel Plugin that comes packaged with RabbitMQ. 可以使用以下命令启用此插件及其可视化界面:You can enable the plugin and its visual interface with this command:

rabbitmq-plugins enable rabbitmq_shovel_management

你可能需要以 root 身份运行该命令。You might need to run that command as root.

现在可以获取将 RabbitMQ 连接到 Azure 所需的凭据。Now is time to get the credentials required for connecting RabbitMQ to Azure.

将 RabbitMQ 连接到 Azure 服务总线Connecting RabbitMQ to Azure Service Bus

你需要为队列创建共享访问策略 (SAS),以便 RabbitMQ 将消息发布到队列中。You'll need to create a Shared Access Policy (SAS) for your queue, so RabbitMQ can publish messages to it. 使用 SAS 策略,你可以指定允许哪个外部方对你的资源执行操作。A SAS Policy let's you specify what external party is allowed to do with your resource. 这里的构思是允许 RabbitMQ 发送消息,但不侦听或管理队列。The idea is that RabbitMQ is able to send messages, but not listen or manage the queue.

添加 SAS 策略

勾选“Send”框,然后单击“Create”以创建我们的 SAS 策略。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. 我们将使用它来让 RabbitMQ 与 Azure 服务总线通信:We're going to use it to let RabbitMQ talk to Azure Service Bus:

获取 SAS 策略

在使用该连接字符串之前,需要将其转换为 RabbitMQ 的 AMQP 连接格式。Before you can use that connection string, you'll need to convert it to RabbitMQ's AMQP connection format. 为此,请转到连接字符串转换器工具并将连接字符串粘贴到窗体中,然后单击“转换”。So go to the connection string converter tool and paste your connection string in the form, click convert. 你将获得一个可用于 RabbitMQ 的连接字符串。You'll get a connection string that's RabbitMQ ready. (该网站在浏览器中以本地方式运行所有内容,因此数据不会通过网络发送)。(That website runs everything local in your browser so your data isn't sent over the wire). 可以在 GitHub 上访问其源代码。You can access its source code on GitHub.


现在,在浏览器中打开 RabbitMQ 管理插件 http://localhost:15672/#/dynamic-shovels 并转到 Admin -> Shovel Management,你可以在其中添加新的 shovel,以便将消息从 RabbitMQ 队列发送到 Azure 服务总线队列。Now open the RabbitMQ management plugin in our browsers 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.

添加 RabbitMQ Shovel

在此处调用你的 Shovel azure 并选择 AMQP 0.9.1 作为源协议。Here call your Shovel azure and choose AMQP 0.9.1 as the source protocol. 在屏幕截图中,我们有 amqp://,这是将我们连接到本地 RabbitMQ 服务器的默认 URI。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.

对于“队列”,可以使用 azure 作为队列的名称。On the queue side of things, you can use azure as the name of your queue. 如果该队列不存在,RabbitMQ 会为你创建它。If that queue doesn't exist, RabbitMQ will create it for you. 你还可以选择已存在的队列的名称。You can also choose the name of a queue that exists already. 可以让其他选项保留默认值。You can leave the other options as default.

然后,对于“destination”,选择 AMQP 1.0 作为协议。Then on the destination side of things, choose AMQP 1.0 as the protocol. URI 字段中,输入你在上一步获得的连接字符串,而在上一步中,你将 Azure 连接字符串转换成了 RabbitMQ 格式。In the URI field, enter the connecting string that you got from the previous step, were you converted your Azure connection string to the RabbitMQ format. 它应如下所示:It should look like this:


在“Address”字段中,我们将输入你的 Azure 服务总线队列 的名称。在本例中,它名为 from-rabbitmqIn the Address field we'll enter the name of your Azure Service Bus Queue, in this case, it was called from-rabbitmq. 单击“Add Shovel”,你的设置项就可以开始接收消息。Click Add Shovel, and your setup should be ready to start receiving messages.

将消息从 RabbitMQ 发布到 Azure 服务总线Publishing Messages from RabbitMQ to Azure Service Bus

在 RabbitMQ 管理界面中,我们可以转到“Queues”,选择 azure 队列,然后搜索“Publish message”面板。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 your queue. 对于我们的示例,我们只是将 fist message 添加为“Payload”并点击“Publish Message”:For our example we're just going to add fist message as the Payload and hit Publish Message:


返回到 Azure 并检查你的队列。Go back to Azure and inspect your queue. 单击左侧面板中的 Service Bus Explorer,然后单击“速览”按钮。Click Service Bus Explorer in the left panel, and then click the Peek button. 如果一切顺利,你会看到队列现在包含一条消息。If all went well, you'll see your queue now has one message. 好极了,恭喜你!Yay, congrats!

Azure 服务总线队列

但请确保消息是你从 RabbitMQ 发送的消息。But let's make sure that message is the one you sent from RabbitMQ. 选择“Peek”选项卡,然后单击“Peek”按钮检索队列中的最后消息。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. 你应该会看到如下图所示的内容,其中列出了你的 first messageYou should see something like the image below where your first message is listed.


让我们回顾一下Let's Recap

恭喜!Congrats! 你已取得了很多成果!You achieved a lot! 你已设法将消息从 RabbitMQ 传送到 Azure 服务总线。让我们回顾一下相关步骤:You managed to get your messages from RabbitMQ to Azure Service Bus, let's recap the steps:

  1. 创建 Azure 服务总线命名空间Create an Azure Service Bus Namespace
  2. 向命名空间添加队列Add a queue to the namespace
  3. 向队列添加 SAS 策略Add a SAS Policy to your queue
  4. 获取队列连接字符串Get the queue connection string
  5. 启用 RabbitMQ shovel 插件和管理界面Enable the RabbitMQ shovel plugin & the management interface
  6. 将 Azure 服务总线连接字符串转换为 RabbitMQ 的 AMQP 格式Convert the Azure Service Bus connection string to RabbitMQ's AMQP format
  7. 将一个新的 Shovel 添加到 RabbitMQ 并将其连接到 Azure 服务总线Add a new Shovel to RabbitMQ & connect it to Azure Service Bus
  8. 发布消息Publish messages

通过前面的步骤,你集成了组织在 Azure 外部的区域。By following the previous steps, you integrated areas of your org that were outside Azure. Shovel 插件允许将消息从 RabbitMQ 发送到 Azure 服务总线。The Shovel plugin allowed you to ship messages from RabbitMQ to Azure Service Bus. 这具有巨大的优势,因为你现在可以允许受信任的第三方通过你的 Azure 部署来连接其应用。This has enormous advantages since you can now allow trusted third parties to connect their apps with your Azure deployment.

归根到底,消息传递就是启用连接,而我们刚刚使用此技术打开了一个新连接。In the end, messaging is about enabling connections, and with this technique we just opened a new one.

后续步骤Next steps