凯发k8国际娱乐官网入口-k8凯发> 智能边缘平台 ief> > > > 云端下发消息到边缘节点
更新时间:2022-11-09 gmt 08:00

云端下发消息到边缘节点-凯发k8国际娱乐官网入口

操作场景

ief支持从云端systemrest下发消息到边缘节点的systemeventbus(mqtt broker)或servicebus两种场景,本章节是以前者为例。

通过调用开放在公网的ief云端的rest gateway接口,结合节点id和自定义topic,向边缘节点中的systemeventbus发送消息。您的终端设备可以通过订阅对应的自定义topic进行消息接收,实现自定义topic的从云到边的消息下发。

使用方法主要分如下三个步骤。

  1. 创建消息路由
  2. 发送消息
  3. 接收消息

创建消息路由

systemrest和systemeventbus为系统默认端点,无需创建。其中systemrest代表该region下ief云端rest gateway接口。systemeventbus代表边缘节点的mqtt broker。

  1. 登录ief管理控制台。
  2. 选择左侧导航栏“边云消息 > 消息路由”
  3. 单击页面右上角“创建消息路由”
  4. 填写相关参数。
    图1 创建消息路由
    • 消息路由名称:输入消息路由名称,如systemrest2systemeventbus。

      消息路由和是同一种资源,命名不能冲突。

    • 源端点:选择systemrest。
    • 源端点资源:填写符合url路径的以“/”开头的字符串,可以使用{}作为入参进行层级模糊匹配,如/aaa/{any-str}/bbb可以匹配/aaa/ccc/bbb或者/aaa/ddd/bbb。整个源端点资源为调用rest接口时的匹配字段。
    • 目的端点:选择systemeventbus。
    • 目的端点资源:目的端点资源为消息转发至mqtt时对应的topic前缀。

    源端点资源和目的端点资源都填写为“/”时,ief会对所有发送到rest接口的请求转发到对应节点的mqtt中,且消息的topic与请求url一致。

  5. 单击“创建”

    “消息路由”页面可以看到已创建的路由。

    图2 消息路由

发送消息

从云端systemrest下发到边缘节点的systemeventbus,您首先需要获取systemrest的endpoint,然后你需要构造请求,将消息通过路由发送至systemeventbus。

  1. 获取systemrest的endpoint。
    1. 登录ief管理控制台。
    2. 选择左侧导航栏“边云消息 > 消息端点”。在systemrest所在行“消息端点属性”列,public的取值即为systemrest的endpoint,如下图。
      图3 systemrest的endpoint

      此处systemrest的endpoint还支持如下地址,如下地址同时支持ak/sk和token认证,推荐使用下面地址。

      • 华南-广州:ief-stargate.cn-south-1.myhuaweicloud.com
      • 华北-北京一:ief-stargate.cn-north-1.myhuaweicloud.com
      • 华北-北京四:ief-stargate.cn-north-4.myhuaweicloud.com
      • 华东-上海一:ief-stargate.cn-east-3.myhuaweicloud.com
      • 华东-上海二:ief-stargate.cn-east-2.myhuaweicloud.com
  2. 构造请求,发送消息。

    往systemrest发送消息,需构造https请求,具体信息如下。

    • method:post
    • url:https://{rest_endpoint}/{node_id}/{topic},rest_endpoint即1中获取的endpoint,node_id为边缘节点id,topic为消息的主题,即创建消息路由中定义的源端点资源。
    • body:发送的消息内容,由用户自定义。
    • header:x-auth-token,取值为该用户在iam获取的有效token,token获取方法请参见。
    • ak/sk认证方法请参见。

    例如使用postman发送如下消息。

    图4 发送消息

接收消息

  1. 登录边缘节点。
  2. 使用mqtt客户端接收消息。

    当前边缘节点mqtt支持两种模式。

    • 一种是内置的mqtt broker(使用8883端口),需要使用节点证书认证,然后订阅对应topic接收消息,具体使用方式请参见。
    • 另外一种是使用外置的mqtt broker(使用1883端口),需要先安装第三方mqtt broker,然后订阅对应topic接收消息。

    这里介绍使用外置的mqtt broker的方式,外置的mqtt broker需要先安装mqtt broker,例如安装mosquitto,步骤如下。

    • 对于ubuntu操作系统,可以使用如下命令安装mosquitto:
      apt-get install mosquitto
      systemctl start mosquitto
      systemctl enable mosquitto
    • 对于centos操作系统,使用如下命令安装mosquitto:
      yum install epel-release
      yum install mosquitto
      systemctl start mosquitto
      systemctl enable mosquitto

    安装完成后,使用订阅命令订阅,订阅后如果有消息发送,就会收到消息,如下所示。其中#表示订阅任何主题,可以替换为指定的主题,如/aaa/bbb等。

    [root@ief-node ~]# mosquitto_sub -t '#' -d 
    client mosq-m02iwjsp4j2ismw6rw sending connect 
    client mosq-m02iwjsp4j2ismw6rw received connack (0)
    client mosq-m02iwjsp4j2ismw6rw sending subscribe (mid: 1, topic: #, qos: 0, options: 0x00) 
    client mosq-m02iwjsp4j2ismw6rw received suback 
    subscribed (mid: 1): 0
    client mosq-m02iwjsp4j2ismw6rw received publish (d0, q0, rq, mq, '/aaa', ... (31 bytes))
    {
        "test-key": "test-value"
    }
分享:
网站地图