Fun MQTT- Ali cloud MQTT use (under) Fun MQTT- MQTT use Ali cloud (on)

Fun MQTT use MQTT- Ali cloud (under)

Summary

       To make things official use in the project, a smooth switch to the service platform with high concurrent high load capacity, for MQTT Ali cloud service provided by the use of authentication.

introduction

       Before writing " the MQTT use of cloud Ali - Ali cloud Fun " when this article because of the time (writing articles is to spend a lot of time), intends to split the content into two parts, the first part, we made a written out, the end of the follow-up intended to add content later.

       To my astonishment, the article is issued, the administrator actually been removed from the home page, I had a serious blow to the less powerful heart, leading to the completion of the follow-up has been less interested part.

      Look at their recent article, published in this article is actually his unfinished article read most visited one. Although the content of the article organize messy, expression is not good enough, in this occasion of 9102 will soon be over, or intend to be a sequel for themselves busy (mediocrity) of 2019 to be ending.

MQTT Service Verification

       Book connected to the back, we talk about Ali cloud RocketMQ, opened MQTT services (relying on persistence MQTT MQ message service), describes the creation MQ, create MQTT instance, top Topic resources, GroupID prefix to create an account and RAM resources create an account and authorization.

       After a series of preparatory work, ready to start using MQTT messaging middleware services.

Access Authentication

       When the message queue using micro MQTT messaging client, the server will be authentication according MQTT Username and Password parameters set by the client.

       Ali Cloud supports two authentication modes

       Signature verification: perpetual license for Client Security Trusted scene

       Token temporary authority to verify: temporary authorization scenario applies to the client unsafe

 

       Specific authentication and authorization of the relevant information here, you can access their own reading. https://help.aliyun.com/document_detail/54225.html

 

Signature verification

      A brief visit Ali at key points in the cloud MQTT access signature authentication mode

      1, MQTT connection parameters must GroupID ClientId prefixed to @@@ as connector, connecting the customer ID custom

      2, MQTT connection parameter Username authentication mode name consists of three parts, AccessKey, InstanceId composition in "|" separated.

      3, MQTT Password connection parameter with a corresponding AccessKeySecret AccessKey use ClientId HMAC-SHA1 signature calculation method to obtain a binary array, and then do the Base64 encoding binary array to obtain a final Password signature string.

 

      Signature verification data at the following address: https://help.aliyun.com/document_detail/48271.html

 

      Ali cloud MQTT service management page, provides an online version of JS verify the calculation of the signature verification page, verify the checksum algorithm for self-realization you are correct.

 

 

MQTT connection test

Let us recall Paho artifact, even to say that even a test

https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.ui.app/1.0.2/

https://www.eclipse.org/paho/downloads.php

 

 

      As shown above, first create MQTT connection, fill in the server address and port fill in client identification (ClientId)

 

 

      In MQTT connectivity options, check the open login, fill in the user name and password

 

 

 

      Click on the link, under normal circumstances, you can successfully connect and log on to Ali cloud MQTT server.

      笔者基于C#,使用开源MQTTnet的Nuget包,也实现了一个简单的MQTT客户端。我们用这个客户端进行连接测试,效果如下图。

 

消息消费

      MQTT是基于二进制消息的发布/订阅编程模式的消息协议。

      要使用MQTT进行通讯,就需要有发布者和消费者。发布者通过发布主题消息传递信息,接收者通过订阅对应的主题接收和消费信息。

订阅主题

 

      MQTT客户端与MQTT服务器建立连接之后,我们即可以进行消息订阅和消息发布。

发布主题

      我们使用上文中建立的顶级主题创建一个带有通配符的订阅主题。

      注意,MQTT允许使用通配符订阅主题,但是并不允许使用通配符广播。

 

 

 

       我们创建一个顶级主题下带有城市和发布者编号的主题,发布一条消息,Payload中携带一个json格式的状态信息和坐标信息。

       点击发布,可以看到上图中,接收框中,成功接收到订阅主题下的消息。(操作中发布了二条消息)。

 

       使用Paho测试消息发布/订阅,效果相同。

 

       一个发布者发布的消息,可以被多个订阅者接收。

       至此,阿里云MQTT服务使用介绍完毕。

 

      <<<玩转MQTT-阿里云之MQTT使用(上)

Guess you like

Origin www.cnblogs.com/flyfire-cn/p/12127512.html