跳至主内容
版本:4.2

跨协议属性转换

概述

RabbitMQ 是一个支持多种协议的消息代理,支持

消息可以很容易地通过一种协议发布,并由其他协议消费。这就需要不同协议格式之间的数据格式转换。在 RabbitMQ 3.13 之前,所有消息在发布到 RabbitMQ 时都会被转换为基于 AMQP 0.9.1 协议的内部格式。这种方法经常导致不必要的转换和/或数据保真度问题。

在当前的 RabbitMQ 版本中,AMQP、AMQP 0.9.1 和 MQTT 协议的方法已有所改变,即消息将始终以其原始格式存储,只有当消息被与发布协议不同的协议消费时,才会被转换*。这样做的好处是,当发布协议和消费协议相同时,不会丢失任何协议特定的信息。

此规则唯一的例外是 streams:无论哪种协议发布,streams 内部都将其消息以 AMQP 编码数据格式存储。因此,例如使用 AMQP 0.9.1 发布时,会进行到 AMQP 的转换。

所有转换都已重写并进行了正式化。本文档旨在捕获实现的转换规则,以便应用程序开发人员可以参考本文档来理解他们在多协议消息应用程序中消息的转换方式。

约定

shortstr:(条件)一个字符串,其长度小于 256 字节,且仅包含有效的 UTF-8 编码数据,不含 NUL(二进制零字节)字符。

*:读作“任意”(字段/类型等)。

条件可以按编写顺序读取并评估。例如,类型为 utf8 且长度超过 255 字节的 AMQP message_id 将首先失败 shortstr 条件,然后继续检查下一行。如果没有匹配的行,则数据将不会被转换(因此,消费应用程序将无法获得该数据)。

AMQP 1.0 -> AMQP 0.9.1

如果消息被 AMQP 0.9.1 消费,并且该消息是:

  • 使用 AMQP 1.0 发布,或
  • 从 stream 消费。
AMQP 部分AMQP 字段AMQP 类型条件AMQP 0.9.1 部分AMQP 0.9.1 字段AMQP 0.9.1 类型注释
headerdurablebooleanpropertiesdelivery_modeubyte2 = durable, 1 = transient
headerpriorityubytepropertiespriorityubyte
headerttlmilliseconds (uint)propertiesexpirationshortstr毫秒转换为字符串值
propertiesmessage_idutf8shortstrpropertiesmessage_idshortstr
propertiesmessage_idutf8> 256 字节propertiesheaders, Key: x-message-idlongstr
propertiesmessage_iduuid (16 字节二进制)propertiesmessage_idshortstr转换为文本,例如 "urn:uuid:550e8400-e29b-41d4-a716-446655440000"
propertiesmessage_idulongpropertiesmessage_idshortstr转换为文本
propertiesmessage_idbinarypropertiesheaders, Key: x-message-idbinary
propertiesuser_idbinaryshortstrpropertiesuser_idshortstr
properties更改为address
propertiessubjectutf8
propertiesreply_toaddressshortstrpropertiesreply_toshortstr
propertiescorrelation_idutf8shortstrpropertiescorrelation_idshortstr
propertiescorrelation_idutf8> 256 字节propertiesheaders, Key: x-correlation-idlongstr
propertiescorrelation_iduuid (16 字节二进制)propertiescorrelation_idshortstr转换为文本表示,例如 "urn:uuid:550e8400-e29b-41d4-a716-446655440000"
propertiescorrelation_idulongpropertiescorrelation_idshortstr转换为数字的文本表示
propertiescorrelation_idbinarypropertiesheaders, Key: x-correlation-idbinary
propertiescontent_typesymbolpropertiescontent_typeshortstr
propertiescontent_encodingsymbolpropertiescontent_encodingshortstr
propertiesabsolute_expiry_timetimestamp
propertiescreation_timetimestamppropertiestimestamptimestamp转换为秒
propertiesgroup_idutf8shortstrpropertiesapp_idshortstr
propertiesgroup_sequencesequence-no
propertiesreply_to_group_idutf8
application properties** (见类型转换)Key: shortstrpropertiesheaders*
message annotations* (symbol - x-cc*)Key: x-ccpropertiesheaders, Key: "CC"longstr
message annotations* (symbol - x-*)* (见类型转换)Key: shortstr & x-*propertiesheaders*通常这意味着 x- headers
datadata单个数据段payloadbinary从数据段中提取的纯二进制数据
data (multiple)**多个数据段payloadAMQP 1.0 编码的二进制数据properties.type 将被设置为 "amqp-1.0"
amqp.value**payloadAMQP 1.0 编码的二进制数据properties.type 将被设置为 "amqp-1.0"
amqp.sequence**payloadAMQP 1.0 编码的二进制数据properties.type 将被设置为 "amqp-1.0"

类型转换

AMQP 1.0 类型条件AMQP 0.9.1 类型注释
utf8 (string)longstrRabbitMQ 不支持 shortstr 头部值,因此所有 utf8 输入都会被转换为 longstr,除非它是 basic properties 中的字段。
binarybinary
longlong
ulonglong溢出风险
ubyteunsignedbyte
shortshort
ushortusignedshort
uintunsignedint
intsignedint
double既不是 NaN 也不是 Infdouble
float既不是 NaN 也不是 Inffloat
decimal类型为 decimal32、decimal64 或 decimal128 的消息注解或应用程序属性不会被转换。
booleanbool
timestamptimestamp (seconds)值除以 1000
bytebyte
nullvoid
listarray
maptable
symbollongstr

AMQP 0.9.1 -> AMQP 1.0

AMQP 0.9.1 部分AMQP 0.9.1 字段AMQP 0.9.1 类型条件AMQP 1.0 部分AMQP 1.0 字段AMQP 1.0 类型注释
basic propertiesmessage_idshortstr有效的 urn uuidpropertiesmessage_iduuid
basic propertiesmessage_idshortstrpropertiesmessage_idutf8
basic propertiescorrelation_idshortstr有效的 urn uuidpropertiescorrelation_iduuid
basic propertiescorrelation_idshortstrpropertiescorrelation_idutf8
basic propertiesuser_idshortstrpropertiesuser_idbinary
basic propertiesexpirationshortstr如果可转换为数值类型headerttluint
basic propertiestypeshortstrmessage annotationsx-basic-typeutf8
basic propertiesreply_toshortstrpropertiesreply_toutf8
basic propertiesapp_idshortstrpropertiesgroup-idutf8
basic propertiestimestamptimestamp (seconds)propertiescreation_timetimestamp转换为毫秒
basic propertiescontent-typeshortstrpropertiescontent-typesymbol
basic propertiescontent-encodingshortstrpropertiescontent-encodingsymbol
basic propertiesdelivery-modeoctetheaderdurableboolean以前在 x-basic-delivery-mode 中
basic propertiespriorityoctetheaderpriorityubyte以前在 x-basic-priority 中
basic.propertiesheadersKey=x-amqp-1.0-propertiesproperties--旧的 1.0 插件头部
basic.propertiesheadersKey=x-amqp-1.0-application-propertiesapplication properties--旧的 1.0 插件头部
basic.propertiesheadersKey=x-amqp-1.0-message-annotationsmessage annotations--旧的 1.0 插件头部
basic propertiesheaders*Key 以 "x-" 开头message annotations*参见类型转换规则
basic propertiesheaderstable值不是数组或表application properties*参见类型转换规则
basic propertiesheaderstable值是数组或表---未转换
payload-binarydata-data

类型转换

AMQP 0.9.1 类型条件AMQP 1.0 类型注释
longstrshortstrutf8性能/精度权衡
longstrbinary
longlong
ubyteubyte
shortshort
ushortushort
uintuint
intint
doubledouble
floatfloat
boolboolean
binarybinary
timestamptimestamp从秒转换为毫秒
bytebyte
voidnull
arraylist
tablemap
table.keyx-* headersymbol
table.keyutf8
table.value*根据此表进行转换

MQTT 5.0 -> AMQP 1.0

MQTT 5.0 部分MQTT 字段MQTT 5.0 类型条件AMQP 1.0 部分AMQP 1.0 字段AMQP 1.0 类型注释
Fixed HeaderDupBits设置为 header first-acquirer 没有意义,因为 DUP 标志仅适用于从客户端到服务器(从服务器到客户端的消费由 Redelivered 标志决定,该标志从队列发送)
Fixed HeaderQoSBitsheaderdurableboolean如果 QoS > 0,则 durable 为 true
Fixed HeaderRetainBits
Variable HeaderPayload Format IndicatorBits参见“Payload”行,条件为“Payload Format Indicator set”
Variable HeaderMessage Expiry Intervaluintheaderttlmilliseconds秒转换为毫秒
Variable HeaderTopic Aliasushort
Variable HeaderResponse Topicutf8propertiesreply_toutf8将 MQTT topic 转换为 AMQP routing key (RK)。将 reply-to 地址设置为 "/exchanges/" X "/" RK。
Variable HeaderCorrelation Databinaryurn:uuidpropertiescorrelation_iduuid
Variable HeaderCorrelation Databinarypropertiescorrelation_idbinary
Variable HeaderUser Propertyutf8Key 以 "x-" 开头且 key 为 ASCIImessage annotationvalue: utf8Key 为 symbol 类型
Key 不以 "x-" 开头application propertiesvalue: utf8Key 为 utf8 类型
Variable HeaderSubscription Identifieruint
Variable HeaderContent Typeutf8有效的 ASCIIpropertiescontent_typesymbolMQTT content type 是 UTF-8,而 AMQP 1.0 content type 仅为 ASCII。
PayloadPayload Format Indicator 未设置datadata
PayloadPayload Format Indicator 已设置amqp.valueutf8如果设置了 Payload Format Indicator,则将 MQTT payload 转换为字符串(即单个 AMQP value section),因为 AMQP 字符串是 UTF-8 编码的。

类型转换

MQTT 5.0 类型条件AMQP 1.0 类型注释
Bitsboolean仅转换选定的标志
ushortushort
uintuint
utf8utf8
binary datautf8utf8
binary databinary
utf8 string pairsmap去重键,但保持顺序

MQTT 5.0 -> AMQP 0.9.1

MQTT 5.0 部分MQTT 5.0 字段MQTT 5.0 类型条件AMQP 0.9.1 部分AMQP 0.9.1 字段AMQP 0.9.1 类型注释
Fixed HeaderDupBits设置为 basic.deliver 字段的 redelivered 没有意义,因为 DUP 标志仅适用于从客户端到服务器(从服务器到客户端的消费由 Redelivered 标志决定,该标志从队列发送)
Fixed HeaderQoSBitspropertiesdelivery_modeQoS 0 映射到 delivery_mode 1,QoS 1 映射到 delivery_mode 2
Fixed HeaderRetainBits
Variable HeaderPayload Format IndicatorBitsAMQP 0.9.1 的 content_encoding 指的是 MIME content encoding,其有效值列在 https://www.iana.org/assignments/http-parameters/http-parameters.xml。因此,AMQP 0.9.1 的 content_encoding 是不同的。最好将其映射到 content_type 的 text/<subtype>; charset="utf-8,但 <subtype> 未定义。此外,MQTT 5.0 的 Content Type 属性已定义。
Variable HeaderMessage Expiry Intervaluintpropertiesexpirationshortstr秒转换为毫秒
Variable HeaderTopic Aliasushort
Variable HeaderResponse Topicutf8propertiesheaders, Key: x-reply-to-topiclongstrAMQP 0.9.1 的 reply_to 属性指的是队列名称,而不是 topic 名称。
Variable HeaderCorrelation Databinaryshortstrpropertiescorrelation_idshortstr
propertiesheaders, Key: x-correlation-idlongstr
Variable HeaderUser Propertyutf8 string pair名称为 shortstrpropertiesheaderslongstrRabbitMQ 不能将 shortstr 作为例如头部值。
Variable HeaderSubscription Identifiervariable byte integer
Variable HeaderContent Typeutf8shortstrpropertiescontent_typeshortstr
Payloadpayload

类型转换

MQTT 5.0 类型条件AMQP 0.9.1 类型注释
BitsBits
ushortushort
uintuint
utf8< 256 字节 且 目标不是 headershortstrRabbitMQ 不能将 shortstr 作为例如头部值。
utf8>= 256 字节longstr
binary< 256 字节的 UTF-8 编码数据shortstr
binary无有效 UTF-8 或 >= 256 字节longstr
utf8 pairstableAMQP 0.9.1 的 Field Tables 中“重复字段是非法的”。RabbitMQ 按键对字段进行排序。

AMQP 1.0 -> MQTT 5.0

AMQP 1.0 部分AMQP 1.0 字段AMQP 1.0 类型条件MQTT 5.0 部分MQTT 5.0 字段MQTT 5.0 类型注释
headerdurablebooleanFixed HeaderQoSBitsdurable=true 映射到 QoS 1,durable=false 映射到 QoS 0
propertiesmessage_id*
propertiesuser_idbinary
properties更改为address
propertiessubjectutf8
propertiesreply_toaddress"/exchanges/" X "/" RK,其中 X 匹配 mqtt.exchangeVariable HeaderResponse Topicutf8将 AMQP topic 转换为 MQTT topic
propertiescorrelation_id*Variable HeaderCorrelation Databinary转换为二进制
propertiescontent_typesymbolVariable HeaderContent Typeutf8
propertiescontent_encodingsymbol
propertiesabsolute_expiry_timetimestamp
propertiescreation_timetimestamp
propertiesgroup_idutf8
propertiesgroup_sequencesequence-no
propertiesreply_to_group_idutf8
application properties**不是 array、list、map 或 binaryUser Propertiesutf8值被转换为字符串表示
message annotations**仅 x- headers,不包括值是 array、list、map 或 binary 的那些。User Propertiesutf8
datadata一个或多个数据段Payloadbinary
data**单个 AMQP 二进制值段Payloadbinary
dataamqp.value(utf8)可转换为 UTF-8 的单个 AMQP 值段PayloadbinaryPayload-Format-Indicator 设置为 1,以便 MQTT 5.0 客户端可以解析消息的文本表示,从而理解消息。
data*其他单个 AMQP 值段或 amqp-sequence 段Payloadbinary使用 AMQP 类型系统进行编码,并包含 Content Type message/vnd.rabbitmq.amqp。此 Content Type 未注册。

AMQP 0.9.1 -> MQTT 5.0

AMQP 0.9.1 部分AMQP 0.9.1 字段AMQP 0.9.1 类型条件MQTT 5.0 部分MQTT 5.0 字段MQTT 5.0 类型注释
basic propertiesmessage_idshortstr
basic propertiescorrelation_idshortstrVariable HeaderCorrelation Databinary
basic propertiesuser_idshortstr
basic propertiesexpirationshortstrVariable HeaderMessage Expiry Intervaluint从毫秒转换为秒
basic propertiestypeshortstr
basic propertiesreply_toshortstr
basic propertiesapp_idshortstr
basic propertiestimestamptimestamp (seconds)
basic propertiescontent_typeshortstrVariable HeaderContent Typeutf8
basic propertiescontent_encodingshortstr
basic propertiesdelivery_modeoctetFixed HeaderQoSBitsdelivery-mode 2 映射到 QoS 1,delivery-mode 1 映射到 QoS 0
basic propertiespriorityoctet
basic.propertiesheaderslongstrKey=x-reply-to-topicVariable HeaderResponse Topicutf8将 AMQP topic 转换为 MQTT topic
basic.propertiesheaderslongstrKey=x-correlation-idVariable HeaderCorrelation Databinary
basic.propertiesheaders*不是 array,而是 tableUser propertiesutf8简单值被转换为其字符串表示
basic propertiesheaderstable
payloadbinaryPayload
© . This site is unofficial and not affiliated with VMware.