Apollo 1.7.1 MQTT 协议手册【官网翻译】

官网地址 http://activemq.apache.org/apollo/documentation/mqtt-manual.html

MQTT协议

Apollo允许客户端使用MQTT连接,该MQTT是一种开源协议规范,适用于使用发布-订阅域的不可靠网络上的有限资源设备。这些类型的设备通常需要一个小的内存空间,不太适合 text-based 类型的协议,如HTTP或STOMP,甚至是传统的二进制协议,如Openwire或AMQP。MQTT是一种紧凑的二进制协议,针对这些类型的有限设备和不可靠的网络进行优化。
在以前的版本中,MQTT作为独立的插件在Apollo中得到支持。到目前为止,这个插件已经成为主要开发的一部分,而MQTT支持可以在没有任何其他配置或第三方插件打包的情况下使用。
由于MQTT是一个 wire-level 的协议,任何实现该协议的客户端都应该能够连接到Apollo,并与其他 MQTT-compatibe 消息代理进行互操作。
要了解更多关于MQTT的详细信息,请参阅MQTT规范

MQTT 协议选项

要开始使用MQTT协议,使用一个有效的MQTT v3.1客户端并连接到Apollo监听的端口。Apollo将执行协议检测,并将自动识别MQTT有效负载,并将连接视为MQTT连接。您不必为MQTT(或STOMP、Openwire、AMQP等)打开一个特殊的端口,它们都可以自动检测到。为了强制某个连接器上的特定协议,有两种方法可以做到这一点。您可以选择不使用协议检测,并将连接器设置为专门针对mqtt:

<connector id="tcp" bind="tcp://0.0.0.0:61613" protocol="mqtt"/>

或者,您可以限制使用<detected>配置元素“detected”的协议:

<connector id="tcp" bind="tcp://0.0.0.0:61613">
  <detect protocols="mqtt openwire" />
</connector>

mqtt元素支持以下 configuration 属性:

  • max_message_length:可以发送给代理的最大消息的大小(以字节为单位)。默认为100 mb
  • protocol_filters:一个过滤器,它可以过滤被发送/接收到的客户的框架。它可以修改框架,甚至放弃它。
  • die_delay:在连接实际关闭之前,连接被认为是“死亡(dead)”之后多久;默认值:5000毫秒

也可以使用mqtt配置元素来控制如何解析和解释目标报头。支持的属性有:

  • queue_prefix:用来标识目标类型的标记;默认值:空
  • path_separator:用于将目标名称中的部分分隔开;默认值:/
  • any_child_wildcard:表示匹配通配符的所有子级目的地;默认值:+
  • any_UNK dant_wildcard:指明匹配通配符的目的地;默认值:#
  • regex_wildcard_start:用于标识regex开头的模式
  • regex_wildcard_end:用于标识regex结尾的模式
  • part_pattern:允许您指定约束命名主题的regex。默认值:[a-zA-Z0-9 \ _ \ \ % \ ~ :()]+

客户端

Apollo 支持MQTT协议的v3.1。以下客户将会工作:

Java : mqtt-client, MeQanTT
C : libmosquitto
Erlang : erlmqtt, my-mqtt4erl
.NET : MQTTDotNet, nMQTT
Perl : net-mqtt-perl, [anyevent-mqtt-perl]https://github.com/beanz/anyevent-mqtt-perl()
Python : nyamuk
Ruby : mqtt-ruby, ruby-em-mqtt
Javascript : Node.js MQTT Client
Delphi : TMQTTCLient
Device specific: Arduino, mbed, Nanode, Netduino
可惜了,没有php

To see an up-to-date listing of client libraries, please the MQTT website for its softwarelistings
The Apollo distribution ships with an examples
directory where you can find some simple examples of how to use some of those clients to send and receive messages from a broker instance.

Connecting

The default broker configuration secures access to the broker so that only the admin
user can connect. The default password for the admin
user is password
.
MQTT clients cannot specify a Virtual Host (see the section on Virtual Hosts in the user guide) so the default virtual host will be used. This is usually the first Virtual Host defined in the Apollo.xml configuration file.

Destination Types

The MQTT protocol is a publish/subscribe protocol. It does not permit true point-to-point messaging achieved using Queues. Therefore Apollo allows only the use of Topics for MQTT messaging. The concept of a subscription and durable subscription to Topics is similar to what you'd find in other protocols and is controlled by the MQTT CONNECT frame's clean session
attribute.

Clean Sessions

When a client sends a connect frame with the clean session
flag set to cleared (false), any previously used session with the same client_id will be re-used. This means while the client was away, that subscription could have received messages. This is the equivalent of a durable subscription in Apollo.
If the clean session
flag is set (true), then a new session will be started and any sessions tha may have been lingering would be removed. This is equivalent to a normal topic subscription in Apollo.
Topic Retained Messages
If a message has been published with the retain flag set, then the message will be 'remembered' by the topic so that if a new subscription arrives, the last retained message is sent to the subscription. For example if you're publishing measurements and you want the last mesasurement published to always be available to a client that subscribes to the topic, you can set the retain flag on the PUBLISH frame.
Note: retained messages are not retained between broker restarts for Quality of Service setting of AT MOST ONCE (QoS=0).

Last Will and Testament Message

You can set a will
message and assocaited QoS for the message when a client first connects to Apollo. The will message is basically a message that will only get sent if there is an unexpected error with the connection and it must be dropped. This can be useful in situations where you have devices that could drop but when they do, you want to know. So if a medical sensor client drops from the broker, a will message could be sent to an “alarm” Topic and handled by the system as a high-priority alert.

Reliable Messaging

MQTT allows a client to publish a message with the following Quality of Service parameters (QoS):
At Most Once (QoS=0)
At Least Once (QoS=1)
Exactly Once (QoS=2)

At Most Once
This QoS will attempt to deliver the message to a client, but it will have the lowest reliability of the three options. If you publish with a QoS=0, At Most Once, then the broker will not send back an Ack saying it received the message, nor will it retry if the broker fails. This QoS is most similar to non-persistent messages in, for example, JMS.
At Least Once
This QoS setting will ensure that the message is delivered at least once to clients. When publishing with this setting, Apollo will send back a PUBACK frame which acknowledges that the broker has received the message and has taken “ownership” for delivering the message. If the client that published the message with QoS=1 does not recieve the PUBACK in a specified period of time, the client may wish to re-publish the message again with the DUP flag set on the PUBLISH frame. It's possible the broker received the first attempt to publish the message and subsequently published it to listening clients. So if the PUBACK got lost somehwere and the client sends the message again, there will be no duplicate detection, and the broker will send the message again to the topic's subscribers.
Exactly Once
This QoS is the strongest level of reliability afforded by the MQTT protocol. This assures the publisher that its message will not only get to its intended subscribers, but that the message will not be duplicated as it could with QoS=1. This QoS, however, comes with increased network overhead.
When a message is published, the broker will store the message ID and send the message to the Topic where it would be persisted if there are any durable subscriptions. It will then send the PUBREC frame back to the client implying the broker has received the message. At this point the broker will expect the client to send the PUBREL frame to clear the message ID from its session state and complete the send with the broker sending a PUBCOMP.
Wildcard Subscriptions
Wild cards can be used in destination names when subscribing as a consumer. This allows you to subscribe to multiple destinations or hierarchy of destinations.
/
is used to separate names in a path

is used to match any name in a path

is used to recursively match path names

For example using the above, these subscriptions are possible
PRICE/#
: Any price for any product on any exchange
PRICE/STOCK/#
: Any price for a stock on any exchange
PRICE/STOCK/NASDAQ/+
: Any stock price on NASDAQ
PRICE/STOCK/+/IBM
: Any IBM stock price on any exchange

Keep Alive
Apollo will only set a keep-alive/heart-beat monitor if the client has specified a keepAlive
value in the CONNECT frame. If one is specified, the actual value used by Apollo will be 1.5 * the keep alive value. This is in keeping with the MQTT spec.
Destination Name Restrictions
Destination names are restricted to using the characters a-z
, A-Z
, 0-9
, _
, -
%
, ~
, :
, ' ', '(', ')' or .
in addition to composite separator ,
and the wild card *
. Any other characters must be UTF-8 and then URL encoded if you wish to preserve their significance.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,185评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,445评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,684评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,564评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,681评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,874评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,025评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,761评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,217评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,545评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,694评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,351评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,988评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,778评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,007评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,427评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,580评论 2 349

推荐阅读更多精彩内容