Mosquitto部署方法

概述

mosquitto是一款mqtt服务器,常作为内部的数据总线。

oh上部署方法有以下几种部署方式:

按oh的三方库移植
交叉编译二进制包,包含bin/etc/lib,尤其lib里要把依赖库等都包括在内
docker

第一种方案需要分析make文件,比较复杂。目前经过oh社区的支持,已经完成方式2的验证。本文档即描述方式2 二进制包的部署过程,不包括交叉编译的过程(此过程由华为韩晋飞帮助完成)。

部署方法

获取安装包

由社区华为同事提供,分arm32位版本和arm64位版本。

64位版本,下载附件📎mosquitto_bin.tar.gz。
32位版本,下载附件📎ohos_arm_ndk.tar.gz

本地解压后修改mosquitto配置文件

因为ohos内没有vi,编辑配置文件比较麻烦只能在外面改好后推送进去,所以在本地改好后等后面再推送进去。附件配置文件已经修改完毕📎mosquitto.conf,可以直接使用。

mosquitto配置文件在解压后的/etc/mosquitto/mosquitto.conf,主要修改以下配置:

允许匿名访问

如果不设置允许匿名访问的话,就需要ACL认证,ACL可以参考mosquitto基本配置 – 云否归档。

allow_anonymous true

设置启动用户

# When run as root, drop privileges to this user and its primary
# group.
# Set to root to stay as root, but this is not recommended.
# If set to "mosquitto", or left unset, and the "mosquitto" user does not exist
# then it will drop privileges to the "nobody" user instead.
# If run as a non-root user, this setting has no effect.
# Note that on Windows this has no effect and so mosquitto should be started by
# the user you wish it to run as.
user root

设置侦听地址

配置为全地址侦听“0.0.0.0”,否则是127.0.0.1 local mode,只能本地访问。

# =================================================================
# Listeners
# =================================================================

# Listen on a port/ip address combination. By using this variable
# multiple times, mosquitto can listen on more than one port. If
# this variable is used and neither bind_address nor port given,
# then the default listener will not be started.
# The port number to listen on must be given. Optionally, an ip
# address or host name may be supplied as a second argument. In
# this case, mosquitto will attempt to bind the listener to that
# address and so restrict access to the associated network and
# interface. By default, mosquitto will listen on all interfaces.
# Note that for a websockets listener it is not possible to bind to a host
# name.
#
# On systems that support Unix Domain Sockets, it is also possible
# to create a # Unix socket rather than opening a TCP socket. In
# this case, the port number should be set to 0 and a unix socket
# path must be provided, e.g.
# listener 0 /tmp/mosquitto.sock
#
# listener port-number [ip address/host name/unix socket path]
#listener
listener 1883 0.0.0.0

上传配置文件到开发板etc下

D:>hdc file send mosquitto.conf /etc/

上传安装包到开发板

注意oh默认没有写文件权限,先本地进入os加写文件权限

D:>hdc shell
#mount -o rw,remount -t auto /

然后再上传到开发板(注意:由于os是3.2,所以工具套件也建议用3.2sdk)

>hdc file send mosquitto_bin.tar.gz /data/
FileTransfer finish, Size:40464, File count = 1, time:23ms rate:1759.30kB/s

Tip:

最好开两个CMD窗口,一个负责传文件,一个作为os内的主要操作。因为ohos内没有vi,没法编辑配置文件,所以只能在外面改好后推送进去。另外服务启动要配置环境变量,目前还没有找到类似bachrc的配置方法,所以最好有一个持久的hdc会话。

解压并复制执行文件到相应目录

#cd /data/
#tar -zxvf mosquitto_bin.tar.gz
#cd mosquitto_bin/sbin
#cp mosquitto /bin

设置LD_LIBRARY_PATH环境变量

注意:需要对应到解压后的lib目录,下面是64位包对应的目录位置。

#export LD_LIBRARY_PATH=/data/mosquitto_bin/lib:$LD_LIBRARY_PATH

32bit包,如果在data目录解压后,export路径如下

#export LD_LIBRARY_PATH=/data/ohos_arm_ndk/lib:$LD_LIBRARY_PATH

启动服务

以conosole方式,带参数启动mosquitto,可方便查看连接等日志信息

# mosquitto -c /etc/mosquitto.conf -v
1675241442: Warning: Mosquitto should not be run as root/administrator.
1675241442: mosquitto version 2.0.15 starting
1675241442: Config loaded from /etc/mosquitto.conf.
1675241442: Opening ipv4 listen socket on port 1883.
1675241442: mosquitto version 2.0.15 running
1675241445: New connection from 192.168.1.100:64336 on port 1883.
1675241445: New client connected from 192.168.1.100:64336 as MQTT_FX_Client (p2, c1, k60).
1675241445: No will message specified.
1675241445: Sending CONNACK to MQTT_FX_Client (0, 0)
1675241457: Received SUBSCRIBE from MQTT_FX_Client
1675241457:     /test/# (QoS 0)
1675241457: MQTT_FX_Client 0 /test/#
1675241457: Sending SUBACK to MQTT_FX_Client
1675241466: Received PUBLISH from MQTT_FX_Client (d0, q0, r0, m0, '/test//a', ... (9 bytes))
1675241466: Sending PUBLISH to MQTT_FX_Client (d0, q0, r0, m0, '/test//a', ... (9 bytes))
1675241526: Received PINGREQ from MQTT_FX_Client

使用MQTTclient客户端工具进行测试

本地用MQTTFX等连接,订阅一个主题并发送数据,看是否能够接收

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容