跨协议属性转换
跨协议属性转换
概述
RabbitMQ 是一个多协议消息代理,支持
AMQP 1.0(以下简称 AMQP),
AMQP 0.9.1
MQTT
STOMP
RabbitMQ Streams
消息可以很容易地通过一种协议发布,并被其他协议消费。 这就需要在不同协议格式之间进行数据格式转换。 在 RabbitMQ 3.13 之前,所有消息在发布到 RabbitMQ 时都会被转换为基于 AMQP 0.9.1 协议的内部格式。 这种方法通常导致不必要的转换和/或数据保真度问题。
在当前的 RabbitMQ 版本中,AMQP、AMQP 0.9.1 和 MQTT 协议的方法已更改,消息将始终以其原始格式存储,并且仅在被与原始协议不同的协议消费时才进行转换。 这样做的好处是,当原始协议和消费协议相同时,不会丢失任何协议特定的信息。
此规则的唯一例外是流:无论使用哪种协议发布,流在内部都将其消息存储为 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 到 AMQP 0.9.1 的转换
是使用 AMQP 1.0 发布的,或者
是从流中消费的。
AMQP 部分AMQP 字段AMQP 类型条件AMQP 0.9.1 部分AMQP 0.9.1 字段AMQP 0.9.1 类型注释headerdurablebooleanpropertiesdelivery_modeubyte2 = 持久,1 = 瞬态headerpriorityubytepropertiespriorityubyteheaderttlmilliseconds (uint)propertiesexpirationshortstr毫秒转换为字符串值propertiesmessage_idutf8shortstrpropertiesmessage_idshortstrpropertiesmessage_idutf8> 256 字节propertiesheaders,键:x-message-idlongstrpropertiesmessage_iduuid (16 字节二进制)propertiesmessage_idshortstr转换为文本,例如“urn:uuid:550e8400-e29b-41d4-a716-446655440000”propertiesmessage_idulongpropertiesmessage_idshortstr转换为文本propertiesmessage_idbinarypropertiesheaders,键:x-message-idbinarypropertiesuser_idbinaryshortstrpropertiesuser_idshortstrpropertiestoaddresspropertiessubjectutf8propertiesreply_toaddressshortstrpropertiesreply_toshortstrpropertiescorrelation_idutf8shortstrpropertiescorrelation_idshortstrpropertiescorrelation_idutf8> 256 字节propertiesheaders,键:x-correlation-idlongstrpropertiescorrelation_iduuid (16 字节二进制)propertiescorrelation_idshortstr转换为文本表示形式,例如“urn:uuid:550e8400-e29b-41d4-a716-446655440000”propertiescorrelation_idulongpropertiescorrelation_idshortstr转换为数字的文本表示形式propertiescorrelation_idbinarypropertiesheaders,键:x-correlation-idbinarypropertiescontent_typesymbolpropertiescontent_typeshortstrpropertiescontent_encodingsymbolpropertiescontent_encodingshortstrpropertiesabsolute_expiry_timetimestamppropertiescreation_timetimestamppropertiestimestamptimestamp转换为秒propertiesgroup_idutf8shortstrpropertiesapp_idshortstrpropertiesgroup_sequencesequence-nopropertiesreply_to_group_idutf8application properties**(参见类型转换)键:shortstrpropertiesheaders*message annotations*(符号 - x-cc*)键:x-ccpropertiesheaders,键:“CC”longstrmessage annotations*(符号 - x-*)*(参见类型转换)键:shortstr & x-*propertiesheaders*通常这意味着 x- headersdatadatasingle data sectionpayloadbinary从数据部分提取的纯二进制data(多个)**multiple data sectionspayloadAMQP 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(字符串)longstrRabbitMQ 不支持 shortstr 标头值,因此除非它是基本属性中的字段,否则所有 utf8 输入都将转换为 longstrbinarybinarylonglongulonglong溢出风险ubyteunsignedbyteshortshortushortusignedshortuintunsignedintintsignedintdoubledoublefloatfloatbooleanbooltimestamptimestamp(秒)值除以 1000bytebytenullvoidlistarraymaptablesymbollongstr
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_idshortstrvalid urn uuidpropertiesmessage_iduuidbasic propertiesmessage_idshortstrpropertiesmessage_idutf8basic propertiescorrelation_idshortstrvalid urn uuidpropertiescorrelation_iduuidbasic propertiescorrelation_idshortstrpropertiescorrelation_idutf8basic propertiesuser_idshortstrpropertiesuser_idbinarybasic propertiesexpirationshortstrif convertible to numeric typeheaderttluintbasic propertiestypeshortstrmessage annotationsx-basic-typeutf8basic propertiesreply_toshortstrpropertiesreply_toutf8basic propertiesapp_idshortstrpropertiesgroup-idutf8basic propertiestimestamptimestamp(秒)propertiescreation_timetimestamp转换为毫秒basic propertiescontent-typeshortstrpropertiescontent-typesymbolbasic propertiescontent-encodingshortstrpropertiescontent-encodingsymbolbasic 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.propertiesheadersKey=x-reply-to-topicpropertiesreply_toutf8"/topic/" RKbasic propertiesheaders*Key begins with "x-"message annotationsKey*参见类型转换规则basic propertiesheaderstableValue not array or tableapplication propertiesKey*参见类型转换规则basic propertiesheaderstableValue is array or table---不转换payload-binarydata-data
类型转换
AMQP 0.9.1 类型条件AMQP 1.0 类型注释longstrshortstrutf8性能/准确性权衡longstrbinarylonglongubyteubyteshortshortushortushortuintuintintintdoubledoublefloatfloatboolbooleanbinarybinarytimestamptimestamp从秒转换为毫秒bytebytevoidnullarraylisttablemaptable.keyx-* headersymboltable.keyutf8table.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 为 trueFixed HeaderRetainBitsVariable HeaderPayload Format IndicatorBits参见“Payload”行,条件为“Payload Format Indicator set”Variable HeaderMessage Expiry Intervaluintheaderttlmilliseconds秒转换为毫秒Variable HeaderTopic AliasushortVariable HeaderResponse Topicutf8propertiesreply_toutf8将 MQTT 主题转换为 AMQP 路由键 (RK)。 将 reply-to 地址设置为 "/exchange/" X "/" RK。Variable HeaderCorrelation Databinaryurn:uuidpropertiescorrelation_iduuidVariable HeaderCorrelation Databinarypropertiescorrelation_idbinaryVariable HeaderUser Propertyutf8键以 "x-" 开头且键为 ASCIImessage annotation值:utf8键的类型为 symbol键不以 "x-" 开头application properties值:utf8键的类型为 utf8Variable HeaderSubscription IdentifieruintVariable HeaderContent Typeutf8valid ASCIIpropertiescontent_typesymbolMQTT 内容类型是 UTF-8,而 AMQP 1.0 内容类型仅为 ASCIIPayloadPayload Format Indicator unsetdatadataPayloadPayload Format Indicator setamqp.valueutf8如果设置了 Payload Format Indicator,则将 MQTT payload 转换为字符串(即单个 AMQP 值部分),因为 AMQP 字符串是 UTF-8 编码的
类型转换
MQTT 5.0 类型条件AMQP 1.0 类型注释Bitsboolean仅转换选定的标志ushortushortuintuintutf8utf8binary datautf8utf8binary databinaryutf8 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 2Fixed HeaderRetainBitsVariable HeaderPayload Format IndicatorBitsAMQP 0.9.1 content_encoding 指的是 MIME 内容编码,其有效值在 https://www.iana.org/assignments/http-parameters/http-parameters.xml 中列出。 因此,AMQP 0.9.1 content_encoding 是不同的东西。 它最好映射到 content_type 的 text/
类型转换
MQTT 5.0 类型条件AMQP 0.9.1 类型注释BitsBitsushortushortuintuintutf8< 256 字节 且 目标不是标头shortstrRabbitMQ 不能将 shortstr 作为例如标头值。utf8>= 256 字节longstrbinary< 256 字节的 UTF-8 编码数据shortstrbinary无效的 UTF-8 或 >= 256 字节longstrutf8 pairstable“重复字段是非法的”在 AMQP 0.9.1 字段表中。 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 0propertiesmessage_id*propertiesuser_idbinarypropertiestoaddresspropertiessubjectutf8propertiesreply_toaddress"/exchange/" X "/" RK,其中 X 匹配 mqtt.exchangeVariable HeaderResponse Topicutf8将 AMQP 主题转换为 MQTT 主题propertiescorrelation_id*Variable HeaderCorrelation Databinary转换为二进制propertiescontent_typesymbolVariable HeaderContent Typeutf8propertiescontent_encodingsymbolpropertiesabsolute_expiry_timetimestamppropertiescreation_timetimestamppropertiesgroup_idutf8propertiesgroup_sequencesequence-nopropertiesreply_to_group_idutf8application properties**不是数组、列表、map 或二进制User Propertiesutf8值转换为字符串表示形式message annotations**仅限 x- 标头,排除具有数组、列表、map 或二进制值的标头User Propertiesutf8datadata一个或多个数据部分Payloadbinarydata**单个 AMQP 二进制值部分Payloadbinarydataamqp.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_idshortstrbasic propertiescorrelation_idshortstrVariable HeaderCorrelation Databinarybasic propertiesuser_idshortstrbasic propertiesexpirationshortstrVariable HeaderMessage Expiry Intervaluint从毫秒转换为秒basic propertiestypeshortstrbasic propertiesreply_toshortstrbasic propertiesapp_idshortstrbasic propertiestimestamptimestamp(秒)basic propertiescontent_typeshortstrVariable HeaderContent Typeutf8basic propertiescontent_encodingshortstrbasic propertiesdelivery_modeoctetFixed HeaderQoSBitsdelivery-mode 2 映射到 QoS 1,delivery-mode 1 映射到 QoS 0basic propertiespriorityoctetbasic.propertiesheaderslongstrKey=x-reply-to-topicVariable HeaderResponse Topicutf8将 AMQP 主题转换为 MQTT 主题basic.propertiesheaderslongstrKey=x-correlation-idVariable HeaderCorrelation Databinarybasic.propertiesheaders*不是数组、表User propertiesutf8简单值转换为其字符串表示形式basic propertiesheaderstablepayloadbinaryPayload