1、MQ服务开启与配置

消息队列MQ服务开启与配置

2、生成token

生成连接所需的token,需要用到实例名称accessKey

消息队列的实例名称

image-20220209171630952

消息队列MQ实例的accessKey

image-20220209171708783

根据这些信息来生成token,生成token的java代码如下:

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;


public class Token {

    public static String assembleToken(String version, String resourceName, String expirationTime, String signatureMethod, String accessKey)
            throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        StringBuilder sb = new StringBuilder();
        String res = URLEncoder.encode(resourceName, "UTF-8");
        String sig = URLEncoder.encode(generatorSignature(version, resourceName, expirationTime
                , accessKey, signatureMethod), "UTF-8");
        sb.append("version=")
                .append(version)
                .append("&res=")
                .append(res)
                .append("&et=")
                .append(expirationTime)
                .append("&method=")
                .append(signatureMethod)
                .append("&sign=")
                .append(sig);
        return sb.toString();
    }

    public static String generatorSignature(String version, String resourceName, String expirationTime, String accessKey, String signatureMethod) 
            throws NoSuchAlgorithmException, InvalidKeyException {
        String encryptText = expirationTime + "\n" + signatureMethod + "\n" + resourceName + "\n" + version;
        String signature;
        byte[] bytes = HmacEncrypt(encryptText, accessKey, signatureMethod);
        signature = Base64.getEncoder().encodeToString(bytes);
        return signature;
    }

    public static byte[] HmacEncrypt(String data, String key, String signatureMethod)
            throws NoSuchAlgorithmException, InvalidKeyException {
        //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
        SecretKeySpec signinKey = null;
        signinKey = new SecretKeySpec(Base64.getDecoder().decode(key),
                "Hmac" + signatureMethod.toUpperCase());

        //生成一个指定 Mac 算法 的 Mac 对象
        Mac mac = null;
        mac = Mac.getInstance("Hmac" + signatureMethod.toUpperCase());

        //用给定密钥初始化 Mac 对象
        mac.init(signinKey);

        //完成 Mac 操作
        return mac.doFinal(data.getBytes());
    }

    public enum SignatureMethod {
        SHA1, MD5, SHA256;
    }

    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        // 参数组版本号,目前仅支持"2018-10-31"
        String version = "2018-10-31";
        // 固定为mqs/{实例名称}
        String resourceName = "mqs/yuyun-mq-test";
        // 消息队列MQ实例的accessKey
        String accessKey = "fFjnCBIQ5dhZg2wtI6306NoxT17oA1DWraGVhrzKf0A=";

        String expirationTime = System.currentTimeMillis() / 1000 + 100 * 24 * 60 * 60 + "";
        String signatureMethod = SignatureMethod.SHA1.name().toLowerCase();
        String token = assembleToken(version, resourceName, expirationTime, signatureMethod, accessKey);

        System.out.println("Authorization:" + token);
    }
}

运行main方法后生成的token:

version=2018-10-31&res=mqs%2Fyuyun-mq-test&et=1653038025&method=sha1&sign=8A9ZajDjffIB%2F6RbnzjN8MlyJ4Y%3D

3、MQTT.fx客户端配置

(1)打开客户端

(2)配置参数
连接协议 证书 地址 端口
MQTT 证书下载 IPv4: 183.230.40.96 IPv6: 2409:8060:8ea:601::13:7c64 8883

如图,username设置为实例名,password为前面代码生成的token:

image-20220209175203894

(3)SSL/TLS加密设置

选择CA certificate file,导入解压后的证书

image-20220209175318453

点击OK,完成配置

4、客户端连接

选择刚才配置好的服务,点击Connect,连接服务

image-20220209175737507

5、订阅并消费数据

选择Subscribe配置,输入订阅名称,订阅命名规则如下:

$sys/pb/consume/$实例名称/$TOPIC/$SUB

我的订阅名称为:

$sys/pb/consume/yuyun-mq-test/yuyun-topic-test/yuyun-topic-data

选择QoS1,点击Subscribe,完成MQTT订阅

image-20220209180143671

订阅成功

image-20220209180214214

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐