今天公司有同事在做RabbitMQ的分享的时候,讲到了Connection和Channel的设计,有同学有疑惑,为什么不用连接池实现,而要通过Channel的方式实现呢?
先脑补下Connection和Channel的关系
即可以在一个连接上同时发送不同Channel的数据;
看下RabbitMQ官网对于Channel的解读:
Some applications need multiple connections to the broker. However, it is undesirable to keep many TCP connections open at the same time because doing so consumes system resources and makes it more difficult to configure firewalls. AMQP 0-9-1 connections are multiplexed withchannels that can be thought of as "lightweight connections that share a single TCP connection".
Every protocol operation performed by a client happens on a channel. Communication on a particular channel is completely separate from communication on another channel, therefore every protocol method also carries a channel ID (a.k.a. channel number), an integer that both the broker and clients use to figure out which channel the method is for.
A channel only exists in the context of a connection and never on its own. When a connection is closed, so are all channels on it.
For applications that use multiple threads/processes for processing, it is very common to open a new channel per thread/process and not share channels between them.
大概的意思就是:
一些应用需要同时创建多个连接到
broker
也就是
RabbitMQ
服务器上。然而因为防火墙的存在,很难同时创建多个连接。
AMQP 0-9-1
连接使用多个
channel
连接实现对单一
Connection
的复用。