This topic describes how to migrate clients from open source MQTT brokers to ApsaraMQ for MQTT brokers.
Background information
ApsaraMQ for MQTT supports development based on various protocols and programming languages. ApsaraMQ for MQTT also supports device-level permission management and Secure Sockets Layer (SSL)/Transport Layer Security (TLS) encryption. This way, you can migrate clients from open source MQTT brokers to ApsaraMQ for MQTT brokers to obtain higher security, higher stability, and more O&M capabilities. The migration requires you to change only the endpoint and permission configurations.
This topic describes how to modify the parameters of open source client SDK for Java to connect to an ApsaraMQ for MQTT broker.
The following figure shows the process.
Limits
If a client is migrated from an open source EMQX broker, the original system topics cannot be used. For example, you cannot use topics whose names start with a dollar sign ($), such as $delayed and $share.
Step 1: Create ApsaraMQ for MQTT resources
Create an instance
Log on to the ApsaraMQ for MQTT console. In the left-side navigation pane, click Instances.
In the top navigation bar, select the region where the instance that you want to manage resides. Then, in the upper-left corner, click Create Instance.
In the panel that appears, use the default value Subscription for the Billing Method parameter. Then, Click OK.
On the buy page that appears, select the instance specifications that you want to purchase based on your business requirements, select Terms of Service, and then click Buy Now.
For information about the instance editions provided by ApsaraMQ for MQTT and their feature differences, see Instance editions.
On the order payment page, follow the on-screen instructions to complete the payment.
Create a topic
Log on to the ApsaraMQ for MQTT console. In the left-side navigation pane, click Instances.
In the top navigation bar, select the region where the instance that you want to manage resides. On the Instances page, click the instance name to go to the Instance Details page.
- In the left-side navigation pane, click Topics. In the upper-left corner of the Topics page, click Create Topic.
- In the Create Topic panel, set the Name and Description parameters for the topic and click OK in the lower-left corner.
Create a group
Log on to the ApsaraMQ for MQTT console. In the left-side navigation pane, click Instances.
In the top navigation bar, select the region where the instance that you want to manage resides. On the Instances page, click the instance name to go to the Instance Details page.
- In the left-side navigation pane, click Groups. In the upper-left corner of the Groups page, click Create Group.
- In the Create Group panel, set the Group ID parameter and click OK.
Step 2: Map parameters in the open source SDK
Add dependencies
Add the following dependencies to the pom.xml file of the original client SDK:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.48</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-onsmqtt</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.0</version>
</dependency>Add a signature algorithm
If you use signature authentication to authenticate a client, you must add a signature algorithm to specify a value for the mqttConnectOptions.setPassword parameter.
Tools.macSignature(clientId, secretKey).toCharArray()For information about the sample code, see Demo projects.
Modify parameter values in the SDK
The following table describes the parameters you must modify based on ApsaraMQ for MQTT parameters in the SDK.
Method | Parameters to be updated | Description |
| broker | The endpoint of the ApsaraMQ for MQTT instance. The value is in the tcp:<Endpoint>:1883 format. You can obtain the endpoint on the Endpoints for Client SDK tab of the Instances page in the ApsaraMQ for MQTT console. Example: tcp://mqtt-cn-******.mqtt.aliyuncs.com:1883. |
clientId | The ID of the client. The value is in the <GroupID>@@@<DeviceID> format. GroupID in the format specifies the ID of a group. You can create the group in the ApsaraMQ for MQTT console. For more information, see Step 1: Create ApsaraMQ for MQTT resources. DeviceID in the format specifies the ID of the device and is specified by the user. For information about valid values of GroupID and DeviceID in the clientId parameter, see Clients. Example: GID_test@@@device001. | |
| topic | The topic on the ApsaraMQ for MQTT instance. You can create the topic in the ApsaraMQ for MQTT console. For more information, see Step 1: Create ApsaraMQ for MQTT resources. |
| UserName | The access credential used to authenticate the client. For more information, see Authentication overview and Custom authentication. |
| Password | The access credential used to authenticate the client. For more information, see Authentication overview and Custom authentication. |
Step 3: Send and receive messages
After you modify the parameter values, you can start the open source client SDK. Then, you can access ApsaraMQ for MQTT to send and receive messages by using the SDK.
References
Shared subscription
If you want to use the shared subscription capability, you can use cloud SDKs provided by ApsaraMQ for MQTT.
You can use cloud SDKs to deploy distributed applications in the cloud. Cloud SDKs support the shared subscription, messaging, message subscription, and client status notification capabilities. Cloud SDK for Java and cloud SDK for Go are available. For more information, see Overview.
Cloud SDKs provided by ApsaraMQ for MQTT allow you to send and receive messages on ApsaraMQ for MQTT brokers instead of the MQTT protocol.