《RabbitMQ Tutorial》第 1 章 简介(Introduction)
RabbitMQ is a message broker: it accepts and forwards messages. You can think about it as a post office: when you put the mail that you want posting in a post box, you can be sure that Mr. Postman will eventually deliver the mail to your recipient. In this analogy, RabbitMQ is a post box, a post office and a postman.
RabbitMQ 是这样一个消息代理:它接收和转发消息。你可以把它相像成是一个邮局:当你把一份邮件投递到信箱时,你可确信的是邮递员先生终究会把邮件投递给接收者。在这个比喻中,RabbitMQ 扮演了信箱、邮局以及邮递员这一系列角色。
The major difference between RabbitMQ and the post office is that it doesn't deal with paper, instead it accepts, stores and forwards binary blobs of data ‒ messages.
RabbitMQ与邮局最大的不同在于它并不处理纸质信件,取而代之的是它接收、储存以及转发二进制消息数据。
RabbitMQ, and messaging in general, uses some jargon.
RabbitMQ 以及消息传递,通过会使用到一些专业术语。
Producing means nothing more than sending. A program that sends messages is a producer :
生产者的意思无非就是发送,一个程序在发送消息时它就是生产者:
A queue is the name for a post box which lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can only be stored inside a queue. A queue is only bound by the host's memory & disk limits, it's essentially a large message buffer. Many producers can send messages that go to one queue, and many consumers can try to receive data from one queue. This is how we represent a queue:
队列其实就是 RabbitMQ 内部的“信箱”,作为消息,尽管自 RabbitMQ 流经应用程序,但它最终只会存储于队列中。队列只会受限于主机内存和磁盘空间,本质上来讲它其实是一个庞大的消息缓存区。多个生产者可以发送消息到同一个队列,并且多个消费者也可以从同一个队列接收数据。下图是我们描绘的一个队列的模样:
Consuming has a similar meaning to receiving. A consumer is a program that mostly waits to receive messages:
同理,消费也有着类似接收的含义,消费者就是一个主要用来等待接收消息的程序:
Note that the producer, consumer, and broker do not have to reside on the same host; indeed in most applications they don't.
要注意的是,生产者、消费者,以及代理之间不必存在于同一台主机,事实上大部分应用程序也不会如此。
"Hello World"
"起步"
In this part of the tutorial we'll write two programs in C#; a producer that sends a single message, and a consumer that receives messages and prints them out. We'll gloss over some of the detail in the .NET client API, concentrating on this very simple thing just to get started. It's a "Hello World" of messaging.
在教程的当前部分我们会编写两个 C# 程序。作为生产者会发送一条消息,同时消费者会接收消息并将其打印出来。我们会忽略 API 当中的一些细节,把精力集中在简单的事情上从而更好的起步。这是一个“Hello World”的消息。
In the diagram below, "P" is our producer and "C" is our consumer. The box in the middle is a queue - a message buffer that RabbitMQ keeps on behalf of the consumer.
在下图中,"P" 就是生产者,"C"就是消费者。中间的方形盒子就是队列,即 RabbitMQ 为消费者保留的消息缓冲区。
The .NET client library .NET 客户端库RabbitMQ speaks multiple protocols. This tutorial uses AMQP 0-9-1, which is an open, general-purpose protocol for messaging. There are a number of clients for RabbitMQ in many different languages. We'll use the .NET client provided by RabbitMQ.
RabbitMQ 支持多种协议,本教程使用的是 AMQP 0-9-1,它是公开的较为通用的消息协议。RabbitMQ 支持多种语言的客户端,在这里我们将使用 RabbitMQ 提供的 .NET 客户端。
The client supports .NET Core as well as .NET Framework 4.5.1+. This tutorial will use RabbitMQ .NET client 5.0 and .NET Core so you will ensure you have it installed and in your PATH.
RabbitMQ 提供的 .NET 客户端已支持 .NET Core 以及 .NET Framework 4.5.1+,本教程将会使用 RabbitMQ .NET client 5.0 和 .NET Core,所以你需要确认已安装成功。
You can also use the .NET Framework to complete this tutorial however the setup steps will be different.
你也可以使用 NET Framework 来完成本教程,然而其安装步骤会有所不同。
RabbitMQ .NET client 5.0 and later versions are distributed via nuget.
RabbitMQ .NET client 5.0 以及最新的版本是经由 Nuget 来发布的。
This tutorial assumes you are using powershell on Windows. On MacOS and Linux nearly any shell will work.
本教程假定你在 Windows 操作系统中会使用 PowerShell,可以放心的是,在 MacOS 和 Linux 中几乎所有的 Shell 环境都能正常运行。
Setup 安装First lets verify that you have .NET Core toolchain in PATH:
首先让我们验证一下本地环境变量 PATH 中的 .NET Core 工具链:
dotnet --helpshould produce a help message.
这时应当产生一个帮助消息。
Now let's generate two projects, one for the publisher and one for the consumer: