[译]rabbitmq 2.1 Consumers and producers (not an economics lesson)
我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家。
For now, all you need to know is that producers create messages
and label them for routing (see figure 2.1).
现在需要知道的是,producer创建message和用于routing的label。
Consumers are just as simple. They attach to a broker server and subscribe to a
queue. Think of a queue as a named mailbox. Whenever a message arrives in a particular
mailbox, RabbitMQ sends it to one of the subscribed/listening consumers. By the
time a consumer receives a message, it now only has one part: a payload. The labels
attached to the message don’t get passed along with the payload when the message is
routed. RabbitMQ doesn’t even tell you who the producer/sender was. It would be
like picking up your mail but all of the envelopes are blank. The only way to know a
message is from your Aunt Millie is if she signed the letter inside. Similarly, if you need
to know specifically who produced an AMQP message, it’s up to the producer to
include that information as a part of the message payload.
consumer较简单,连接到broker然后subscribed一个queue。可以把queue想象为一个mailbox。
当一个消息到达指定的mailbox,rabbitmq把消息发送到subscribed/listening的consumer。
这时consumer会收到这个消息,消息只包含一部分:payload。
rabbtimq route 消息时,消息并不会包含label部分。
rabbitmq不会告诉你消息的发送者是谁。
如果你想知道message来自谁,你需要写在消息内容中。
From the outside it’s simple: producers create messages and consumers receive
them. Your app can be a producer when it needs to send a message to another app, or
it can be a consumer when it needs to receive. It can also switch between the two. But
before it can do either it has to set up a channel. Wait a minute … what’s a channel?
你的app可以作为producer或者consumer,即收发消息,但是在这之前,你需要建立一个channel。
Before you consume from or publish to Rabbit, you first have to connect to it. By
connecting, you’re creating a TCP connection between your app and the Rabbit broker.
Once the TCP connection is open (and you’re authenticated), your app then creates
an AMQP channel. This channel is a virtual connection inside the “real” TCP
connection, and it’s over the channel that you issue AMQP commands. Every channel
has a unique ID assigned to it (your AMQP library of choice will handle remembering
the ID for you). Whether you’re publishing a message, subscribing to a queue, or
receiving a message, it’s all done over a channel. Why do we need channels, you might
ask? Why not just issue AMQP commands directly over the TCP connection?
app成为consumer或者producer之前,必须连接。在连接时,会在app合broker之前建立一个TCP连接。
当tcp连接被打开,app创建一个AMQP channel。
channel是真实tcp连接中的虚拟channel,你发布的AMQP命令也是基于channel的。
每个channel都会被分配一个唯一的ID。
消息的发送、订阅、接收都是基于channel的。
为什么需要基于channel,而不是通过tcp连接发送呢?
The main reason is because setting up and tearing down TCP sessions is expensive for an
operating system. Let’s say your app consumes messages from a queue and spins
threads up or down based on service demand. If all you had were TCP connections,
each thread would need its own connection to Rabbit, which could mean
hundreds of connections per second during high load periods. Not only
would this be a massive waste of TCP connections, but an operating system
can only build so many per second. So you could quickly hit a performance
wall. Wouldn’t it be cool if you could use one TCP connection for all of your threads for performance,
but get the same privacy
as giving each thread its own connection? This is where a channel comes in.
主要原因是tcp的建立和拆除对于操作系统来说代价太高。
假设你的app从queue中消费消息,线程的启停都基于服务需求,
如果每个线程都建立一个tcp连接到rabbitmq服务器,
这意味着高负载时,每秒钟会有几百个连接,
但是一个操作系统每秒钟只能建立这么多连接。
所以你很快就会遇到性能瓶颈。
所以应该使用channel。
As each thread spins up, it creates a channel on the existing connection and gets its own
private communication path to Rabbit without any additional load on your operating
system’s TCP stack, as in figure 2.2. As a result, you can create a channel hundreds or
thousands of times a second without your operating system seeing so much as a blip.
There’s no limit to how many AMQP channels you can have on one TCP connection.
当每个线程启动时在已经存在的tcp连接上创建一个channel,获取他自己私有的
通信路径并且不会给操作系统tcp栈增加负荷。
因此,即使你每秒创建成百上千次channel,在操作系统看来也只是一个标志。
一个TCP连接上,没有限制说只能创建多少个AMQP channel。
Think of it like a bundle of fiber optic cable.
Each fiber strand in the cable can transmit (just like a channel). But the cable has
many fiber strands, allowing all the connected threads to transmit and receive simultaneously
via multiple strands. A TCP connection is like the cable, and an AMQP channel
is like an individual fiber strand.
类似一束光缆。
每根光缆中的纤维都可以用于传输(正如channel)。
但是光缆有许多纤维,允许所有已连接的线程同时传输、接收。
tcp连接就像光缆,AMQP channel就像其中的一根纤维。
How about an example? Let’s say you’ve written a service for keeping track of valet
parking, and everyone talks to it through RabbitMQ. Your service has to fulfill two
tasks:
1 Store valet ticket IDs and the associated parking space the car is in
2 Return the parking space for any particular valet ticket ID
来个例子:
你在写一个服务来记录代客泊车的情况,每个人通过rabbitmq与你交互。
你的服务需要实现2个任务:
1.保存泊车票ID和对应的停车位置
2.返回指定泊车票对应的停车位
For the first task, your service is a consumer. It’s subscribed to a Rabbit queue waiting
for “store ticket” messages with ticket IDs and parking space numbers inside. With the
second task, your service is both a consumer and a producer. It needs to receive messages
that tell it a particular valet ticket ID and then it needs to publish a response
message with the associated parking space number.
在第一个任务中,你的服务是consumer,订阅了消息,等待包含 泊车票ID 和 停车位置的 "存票"消息。
在第二个任务中,你的服务是consumer和producer,当然受到一个指定的 泊车票ID后,返回停车位置消息。
To fulfill the second task, your app is a producer. Once the connection to the
RabbitMQ broker is open, your app creates multiple channels: chan_recv for the receiving
thread and a chan_sendX (X being the thread number) channel for each reply
thread. Using chan_recv, you set up a subscription to the queue that receives messages
containing “ticket retrieval” requests. When a ticket retrieval message is received by
your app over chan_recv, it looks up the ticket ID contained in the message.
为了实现第二个任务,你的app是一个producer,当与rabbitmq建链之后,你的app创建了多个channel:
chan_recv - 接收线程channel
chan_sendX(X代表线程号) - 答复线程channel
利用chan_recv建立订阅来接收“票检索”请求,收到请求后,根据请求中的票ID进行查找。
As soon as the associated parking space number has been identified, your app then creates a
thread to send the response (letting the original thread continue to receive new
requests). The new reply thread then creates a message containing the parking space
number. Finally, the new thread labels the response message and publishes it to Rabbit
using its chan_sendX channel. If your app had only one channel,
it wouldn’t be possible to share the TCP connection with the new reply threads.
当找到相关的信息,app创建一个线程chan_sendX来发送答复(不使用原来的线程,让原来的线程继续接收消息)。
如果你的app只有一个channel,这个tcp连接就不能被多个线程共用。
This would leave youwith two choices.
Either use one connection and one thread—meaning your app
couldn't process new ticket retrieval requests until it sent a response to the current
one—or spawn a TCP connection for each sending thread, wasting TCP resources.
With multiple channels, many threads can share the same connection simultaneously,
meaning that responding to requests doesn’t block you from consuming new requests
and you don’t waste a TCP connection for each thread. Sometimes you may choose to
use only one channel, but with AMQP you have the flexibility to use as many channels
as your app needs without the overhead of multiple TCP connections.
这使得你有2个选择。
使用一个tcp连接和一个线程,这意味着查询并回复之前,app无法处理一个新的查询请求,
或者产生大量的tcp连接,浪费tcp的资源。
使用多个channel,多个线程可以使用同一个tcp连接,
这意味着答复动作并不会阻塞你的接收动作,并且不会浪费tcp连接。
The important thing to remember about consumers and producers is that they map
to the ideas of sending and receiving rather than client and server. Messaging in general,
and AMQP in particular, can be thought of as an enhanced transport layer. With
channels, you have the ability to create as many parallel transport layers as your app
needs without being limited by TCP connection restrictions. When you understand
these concepts, you can begin thinking of RabbitMQ as a router for your software.
需要记住的是consumer和producer代表着接收和发送消息。
通常的消息,特殊的AMQP,可以被看做加强版的消息传输层。
有了channel,你可以创建很多平行的传输层,而不受tcp连接限制。
理解了这些概念,你可以认为rabbitmq是软件的路由器。
[译]rabbitmq 2.1 Consumers and producers (not an economics lesson)的更多相关文章
- [译]rabbitmq 2.5 Where’s my message? Durability and you
我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. There’s a dirty secret about creating queues and exchanges in ...
- [译]rabbitmq 2.2 Building from the bottom: queues
我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. You have consumers and producers under your belt, and now you ...
- [译]RabbitMQ教程C#版 - “Hello World”
[译]RabbitMQ教程C#版 - “Hello World” 先决条件本教程假定RabbitMQ已经安装,并运行在localhost标准端口(5672).如果你使用不同的主机.端口或证书,则需 ...
- [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation
我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. With exchanges, bindings, and queues under your belt, you mig ...
- [译]RabbitMQ教程C#版 - 工作队列
先决条件 本教程假定RabbitMQ已经安装,并运行在localhost标准端口(5672).如果你使用不同的主机.端口或证书,则需要调整连接设置. 从哪里获得帮助 如果您在阅读本教程时遇到困难,可以 ...
- [译]RabbitMQ教程C#版 - 远程过程调用(RPC)
先决条件 本教程假定 RabbitMQ 已经安装,并运行在localhost标准端口(5672).如果你使用不同的主机.端口或证书,则需要调整连接设置. 从哪里获得帮助 如果您在阅读本教程时遇到困难, ...
- [译]RabbitMQ教程C#版 - 主题
先决条件 本教程假定 RabbitMQ 已经安装,并运行在localhost标准端口(5672).如果你使用不同的主机.端口或证书,则需要调整连接设置. 从哪里获得帮助 如果您在阅读本教程时遇到困难, ...
- [译]RabbitMQ教程C#版 - 路由
先决条件 本教程假定 RabbitMQ 已经安装,并运行在localhost标准端口(5672).如果你使用不同的主机.端口或证书,则需要调整连接设置. 从哪里获得帮助 如果您在阅读本教程时遇到困难, ...
- [译]RabbitMQ教程C#版 - 发布订阅
先决条件 本教程假定 RabbitMQ 已经安装,并运行在localhost标准端口(5672).如果你使用不同的主机.端口或证书,则需要调整连接设置. 从哪里获得帮助 如果您在阅读本教程时遇到困难, ...
随机推荐
- 学习练习 java数据库查询小题
10. 查询Score表中的最高分的学生学号和课程号.(子查询或者排序) 11. 查询每门课的平均成绩. 12.查询Score表中至少有5名学生选修的并以3开头的课程的平均分数. 13.查询分数大于7 ...
- 洛谷P2733 家的范围 Home on the Range
P2733 家的范围 Home on the Range• o 26通过o 61提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题解 最新讨论• 暂时没有讨论题目背景 ...
- ASPxGridView中批量提交及个别提交的写法
//获取chech box ID protected string GetProtoID() { string protoId = ""; //获取选中的记录Id List< ...
- 设置Eclipse自动跳转到debug模式的小技巧
默认情况下,eclipse中右键debug,当运行到设置的断点时会自动跳到debug模式下.但由于我的eclipse环境,从开始一直用到现在,中间包括装.卸各种插件,更换版本,从英文界面导到中文界面又 ...
- 常用思科设备图标(JPG+矢量图)
常用思科设备图标 在制作网络拓扑图示时我们利用MS Visio或亿图图示等制图软件自带的网络设备绘制拓扑图感觉提供的设备图标非常有限,往往不满足我们的要求,这时就常需要找一些思科的设备图标,在网上到处 ...
- js对象的相关操作方法
//在js的对象中可以直接将一个方法赋值给一个属性,这种方法非常好用. //在js中虽然没有像其他语言中的class关键字,但是我们也可以用函数,字面量的方式来模拟. var myHome={ nam ...
- 【MySQL】MySQL回滚工具
1.mysqlbinlog把事务从binlog中导出 2.从导出的binlog中找到要回滚的事务,去掉第一个DML语句前和最后一个DML语句后与DML无关的binlog信息 3.在目录中新建一个tab ...
- C++ 学习基础一
1.预处理器指示符如果文件名用尖括号“<”和”>”括起来的,则表示该文件是工程或标准头文件,查找过程会检查预定义的目录.如果文件名用双引号括起来,则表示该文件是用户自定义的头文件,查找该文 ...
- MVC5使用SignalR进行双向通信(1)
MVC5使用SignalR进行双向通信 (1) 配置Signal 在NuGet中通过 install-package Microsoft.AspNet.SignalR 命令进行安装 在Scripts文 ...
- [转]CentOS开机启动脚本
转载自http://www.2cto.com/os/201306/220559.html 我的一个Centos开机自启动脚本的制作 一.切换到/etc/init.d/ 二.制作sh脚本 v ...