阿里云服务器上搭建 MQTT服务

本文涉及的产品
云服务器 ECS,每月免费额度200元 3个月
云服务器ECS,u1 2核4GB 1个月
简介: 阿里云服务器上搭建 MQTT服务

一、简介

    最近,在做一个项目的时候,需要在线管理网络继电器,以前也做过硬件的项目,但是这样的项目不多。现在我想实现一个在线可以接受网络继电器发送的信号,也可以向网络继电器发送命令,控制其的运行。这个功能的实现需要物联网的支持,我在咨询了网络继电器卖家,想要在线实现网络硬件的管理有三种解决方案。

    第一种:由于我买的是阿里云的服务器,可以在买阿里云的IOT组件,而且提供了全部功能,提供优化的接口,硬件、网络的管理和维护都不用关心,但是费用不菲。

    第二种:如果公司内部有特牛的 TCP/IP 软件工程师的话,可以自己写一套出来,但是这个开发过程、调试过程、维护过程等,周期比较长,这样的人也不好找,性价比不高。

    第三种:可以自己在云服务器上搭建MQTT服务器,通过其组件实现在线管理,这种方案总体来说,性价比最高。

    我选择的是第三种解决方案,我已经在云服务器上搭建了自己的MQTT服务器,硬件之间也可以实现管理了,我今天就把过程记录下来,以防以后使用的时候到处乱找,而且网上的有的也不是很全和很详细,如果大家有需要,也可以直接使用。我写的很详细,小白照着做都没有问题,高手就略过吧。


    我的搭建环境如下:

        云平台:阿里云平台

        操作系统:Windows Server 2022 Datacenter

        机器内存:8.00 GB (7.73 GB 可用)

        系统类型:64 位操作系统, 基于 x64 的处理器

        MQTT服务器:Apache-Apollo

        MQTT客户端:MQTT.fx


    IOT初次接触,当然了,很多相关的工具也是初始使用,使用不好,大家不要见笑。

二,准备工作

你需要首先注册一个阿里云账号,点击:注册阿里云账号

image.gif 编辑

输入相关信息后,代表注册成功。

然后点击:个人中心,在个人中心去做一下实名。

image.gif 编辑

选择企业实名或者个人实名,这里注意,如果是企业行为一定要选择企业实名,而不是个人实名

然后点击:准备一台阿里云服务器

三、配置详情

    想要安装 Apache-Apollo,必须安装他的运行时,因为它是用Java 写的,当然需要安装 Java 的运行环境,下载 JDK,直接安装就可以,很简单,没什么可说的。安装完成,也要配置环境变量,否则我们不能直接在 cmd 命令行终端种使用其命令。话不多说,直接开始。


    1、由于 Apache-Apollo 的运行需要使用 Java 的运行环境,第一个必须先下载 JDK。

        1)、下载 JDK 安装程序,我贴了两个地址,都可以直接下载。
            
地址1:https://www.java.com/zh-CN/   ,

            地址2:https://www.oracle.com/cn/java/technologies/downloads/#java8-windows



image.gif 编辑


            点击两次才到真正下载页面。


image.gif 编辑

              下载到本地的文件,我下载两个,一个是 JDK,一个是 JRE,大家只下载一个就好。

              

image.gif 编辑

        2)、安装JDK步骤没有什么可说的,直接下一步就可以,根据自己的需求更改就可以。

            1】、第一步要先安装好JDK的运行环境, 点击jdk-8u271-windows-x64安装包,点击运行后弹出下列界面,点击下一步


            2】、 我把安装包放在了自定义目录下,好多国外软件安装路径出现中文或空格都会出现错误,所以我为了避免这种情况 安装国外软件的时候路径都不包含中文和空格,选择默认路径也可以。


        3)、配置Java环境变量

            1】、配置系统的环境变量


image.gif 编辑

                      

image.gif 编辑


                      右键”计算机” => ”属性” => ”高级系统设置” => ”高级” => “环境变量” => “系统变量”。

            2】、新建:

                 变量名:JAVA_HOME

                 变量值:D:\program files\Java\jdk-1.8【具体要根据你自己的安装路径,我的是安装在D盘的】


image.gif 编辑

            3】、新建:

                 变量名:CLASSPATH

                 变量值:%JAVA_HOME%/lib/dt.jar;%JAVA_HOME/%/lib/tools.jar


image.gif 编辑

            4】、添加:找到PATH变量,选择编辑。

                 把  %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin 添加到”变量值”的结尾处。


image.gif 编辑


           5】、确保电脑安装了Java 运行时环境 JRE 或者 JDK ( version >=1.7 )

                 在命令窗口中,输入java命令:java -version,回车,如果出现版本信息,说明安装成功。


image.gif 编辑


    2、下载、配置 Apollo 服务器。

        1)、先上 Apollo 服务器的下载地址。
           
地址1:https://pan.baidu.com/s/1-tU9P5srKiM3P1Vy10eOQg 百度网盘下载地址(目前有效,不知是否一直有效)

              提取码:e5l0

              (包含apache-apollo-1.7.1-windows-distro、jdk-8u271-windows-i586、jdk-8u271-windows-x64)


            地址2:Index of /dist/activemq/activemq-apollo/1.7.1



image.gif 编辑


        2)、 在CMD下输入 apollo.cmd create mqttServer,显示创建完成如下

             下载Apollo服务器并解压,在apache-apollo-1.7.1\bin的路径栏输入cmd运行,这种方式便于测试,如果想正常使用,安装为 Windows 服务。



image.gif 编辑


image.gif 编辑

            成功创建文件夹。


image.gif 编辑

        3)、配置 apollo.xml 文件。
            
在 apache-apollo-1.7.1\bin\mqttServer\etc 目录下,其中 apollo.xml 是配置文件,再次强调一下,如果想要在外网访问,必须把以前的127.0.0.1 改成 0.0.0.0 ,如果是云服务器,通过网站访问就是:http://云服务器公网IP:61680/console/index.html,如果是通过MQTT客户端访问:云服务器公网IP:61613,我第一次就配置成了通过 IPconfig 命令获取的地址了,总是不通 。


image.gif 编辑

image.gif 编辑

1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

2 <!--

3   Licensed to the Apache Software Foundation (ASF) under one or more

4   contributor license agreements. See the NOTICE file distributed with

5   this work for additional information regarding copyright ownership.

6   The ASF licenses this file to You under the Apache License, Version

7   2.0 (the "License"); you may not use this file except in compliance

8   with the License. You may obtain a copy of the License at

9   http://www.apache.org/licenses/LICENSE-2.0 Unless required by

10   applicable law or agreed to in writing, software distributed under

11   the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES

12   OR CONDITIONS OF ANY KIND, either express or implied. See the

13   License for the specific language governing permissions and

14   limitations under the License.

15 -->

16

17 <!--

18   For more information on how configure this file please

19   reference:

20

21   http://activemq.apache.org/apollo/versions/1.7.1/website/documentation/user-manual.html

22   -->

23 <broker xmlns="http://activemq.apache.org/schema/activemq/apollo">

24

25   <notes>

26     The default configuration with tls/ssl enabled.

27   </notes>

28

29   <log_category console="console" security="security" connection="connection" audit="audit"/>

30

31  

32   <authentication domain="apollo"/>

33   <!-- Give admins full access -->

34   <access_rule allow="admins" action="*"/>

35   <access_rule allow="*" action="connect" kind="connector"/>

36  

37

38   <virtual_host id="mqttServer">

39     <!--

40       You should add all the host names that this virtual host is known as

41       to properly support the STOMP 1.1 virtual host feature.

42       -->

43     <host_name>mqttServer</host_name>

44     <host_name>localhost</host_name>

45     <host_name>127.0.0.1</host_name>

46

47     <!-- Uncomment to disable security for the virtual host -->

48     <!-- <authentication enabled="false"/> -->

49

50     <!-- Uncomment to disable security for the virtual host -->

51     <!-- <authentication enabled="false"/> -->

52     <access_rule allow="users" action="connect create destroy send receive consume"/>

53    

54

55     <!-- You can delete this element if you want to disable persistence for this virtual host -->

56     <leveldb_store directory="${apollo.base}/data"/>

57    

58

59   </virtual_host>

60   //Web网站系统访问的IP地址,127.0.0.1,这样定义只能内网访问,如果想外网访问,改成:0.0.0.0,格式:http://1**.9*.11*.1**:61680/console/index.html#

61   <web_admin bind="http://0.0.0.0:61680"/> 

62   <web_admin bind="https://0.0.0.0:61681"/>

63  //这是MQTT服务器的地址,在使用MQTT客户端访问MQTT服务器的格式:云服务器公网地址(不是执行ipconfig获取的地址):端口号,格式:1**.9*.11*.1**:61613

64   <connector id="tcp" bind="tcp://0.0.0.0:61613" connection_limit="2000"/>

65   <connector id="tls" bind="tls://0.0.0.0:61614" connection_limit="2000"/>

66   <connector id="ws"  bind="ws://0.0.0.0:61623"  connection_limit="2000"/>

67   <connector id="wss" bind="wss://0.0.0.0:61624" connection_limit="2000"/>

68

69   <key_storage file="${apollo.base}/etc/keystore" password="password" key_password="password"/>

70

71 </broker>

image.gif 编辑

        4)、修改 users.properties 文件,我保持的默认。

             apache-apollo-1.7.1\bin\mqttServer\etc\users.properties 这个文件可以配置登录账号密码,示例有一个admin=password, admin是账号,password是密码,客户端连接服务器和web网页登录所用的账号密码都是这个。


image.gif 编辑

            源码如下:

image.gif 编辑

1 ## ---------------------------------------------------------------------------

2 ## Licensed to the Apache Software Foundation (ASF) under one or more

3 ## contributor license agreements.  See the NOTICE file distributed with

4 ## this work for additional information regarding copyright ownership.

5 ## The ASF licenses this file to You under the Apache License, Version 2.0

6 ## (the "License"); you may not use this file except in compliance with

7 ## the License.  You may obtain a copy of the License at

8 ##

9 ## http://www.apache.org/licenses/LICENSE-2.0

10 ##

11 ## Unless required by applicable law or agreed to in writing, software

12 ## distributed under the License is distributed on an "AS IS" BASIS,

13 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

14 ## See the License for the specific language governing permissions and

15 ## limitations under the License.

16 ## ---------------------------------------------------------------------------

17

18 #

19 # The list of users that can login.  This file supports both plain text or

20 # encrypted passwords.  Here is an example what an encrypted password

21 # would look like:

22 #

23 # admin=ENC(Cf3Jf3tM+UrSOoaKU50od5CuBa8rxjoL)

24 #

25

26 admin=password

image.gif 编辑

        5)、配置 groups.properties 文件。

            apache-apollo-1.7.1\bin\mqttServer\etc\groups.properties 这个文件可以配置多个账户,如示例中的,示例是admins=admin 如果想添加多个账户可以 admins=admin|test ,其中test就是新增账户。


image.gif 编辑

            源码如下:

image.gif 编辑

1 ## ---------------------------------------------------------------------------

2 ## Licensed to the Apache Software Foundation (ASF) under one or more

3 ## contributor license agreements.  See the NOTICE file distributed with

4 ## this work for additional information regarding copyright ownership.

5 ## The ASF licenses this file to You under the Apache License, Version 2.0

6 ## (the "License"); you may not use this file except in compliance with

7 ## the License.  You may obtain a copy of the License at

8 ##

9 ## http://www.apache.org/licenses/LICENSE-2.0

10 ##

11 ## Unless required by applicable law or agreed to in writing, software

12 ## distributed under the License is distributed on an "AS IS" BASIS,

13 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

14 ## See the License for the specific language governing permissions and

15 ## limitations under the License.

16 ## ---------------------------------------------------------------------------

17

18 #

19 # Allows you to place multiple users in a group.

20 # Example:

21 #

22 # power_users=admin|chirino

23 #

24 admins=admin

image.gif 编辑

        6)、运行 apollo-broker.cmd 命令。

            在 apache-apollo-1.7.1\bin\mqttServer\bin 路径下输入CMD打开命令行,输入apollo-broker.cmd run运行,出现下图就说明配置成功


image.gif 编辑


image.gif 编辑

        7)、客户端(外网客户端和服务器本地,访问方式不同)测试运行。   

             如果在服务器本地访问,可以直接访问:http://localhost:61680,或者 http://127.0.0.1:61680,都可以访问,会出现如下界面,说明配置成功。

            当然,如果是在外网访问的话,就要使用服务器公网IP+端口号,否则也是不通的。比如:http://1**.9*.1**.1**:61680/

            

image.gif 编辑

              其中connectors选项下的TCP就代表MQTT的访问

             如果我们有MQTT客户端连接的话,这里也是有显示的。


image.gif 编辑

              这就是我们当前的链接


image.gif 编辑

        8)、安装 Windows 服务。

            这个命令的目录是:\apache-apollo-1.7.1\bin\mqttServer\bin


image.gif 编辑

            1】、执行 apollo-broker-service install 命令和 apollo-broker-service start 命令。


image.gif 编辑

            2】、通过services.msc 命令查看服务。

                  

image.gif 编辑

    3、MQTT.fx 安装和使用

        MQTT服务器安装好了,现在我们开始使用使用MQTT工具来访问了。我使用的是MQTT.fx,使用的版本是。1.7.1,也把过程记录下来。这是启动的图标。MQTT.fx 是目前主流的mqtt客户端,可以快速验证是否可以与IoT Hub 服务交流发布或订阅消息。设备将当前所处的状态作为MQTT主题发送给IoT Hub,每个MQTT主题topic具有不同等级的名称,如“建筑/楼层/温度。” MQTT代理服务器将接收到的主题topic发送给给所有订阅的客户端。目前最新版下载地址(支持windows,linux,mac)


image.gif 编辑

        1)、下载地址:百度网盘 请输入提取码  ,现在只有百度网盘有了1.7.1版本的,提取码:1234。
          我在贴一个地址:https://softblade.de/en/download-2/ 这个应该是可以的,安装很简单,就不多说了,我就说说如何使用把。


            【1】、首先配置 MQTT 代理


image.gif 编辑


image.gif 编辑


            【2】、打开配置窗口如下:


image.gif 编辑

            【3】、回到主界面,点击connect连接到MQTT 代理服务器上,就可以进行订阅和发布消息测试了。


image.gif 编辑

            【4】、Publish 发布测试。(主题:这是测试,内容:this is my test 2023.7.13)


image.gif 编辑

            【5】Subscribe即订阅(订阅该主题之后,每次发布消息均会收到通知)


image.gif 编辑

            【6】、链接成功之后,在apache-apollo管理平台可以看到有一个链接存在了


image.gif 编辑

四、总结    

    好了,今天就写道这里了,这个测试也挺麻烦的,不熟悉的也踩草不少坑,谁都有第一次,慢慢来就好了。最近接触了很多新的东西,也学到了不少东西,每天有进步,还是很开心的,不忘初心,继续努力吧。

天下国家,可均也;爵禄,可辞也;白刃,可蹈也;中庸不可能也

相关实践学习
一小时快速掌握 SQL 语法
本实验带您学习SQL的基础语法,快速入门SQL。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情:&nbsp;https://www.aliyun.com/product/ecs
相关文章
|
2天前
|
运维 JavaScript Java
Serverless 应用引擎产品使用之在阿里云函数计算中想为两个不同的服务分别开通自定义域名如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
12 1
|
2天前
|
NoSQL 数据管理 MongoDB
数据管理DMS产品使用合集之如何通过阿里云的数据管理服务(DMS)导出MongoDB数据
阿里云数据管理DMS提供了全面的数据管理、数据库运维、数据安全、数据迁移与同步等功能,助力企业高效、安全地进行数据库管理和运维工作。以下是DMS产品使用合集的详细介绍。
|
2天前
|
弹性计算 运维 Serverless
Serverless 应用引擎产品使用之在阿里函数计算中,使用阿里云API或SDK从函数计算调用ECS实例的服务如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
25 4
|
2天前
|
运维 NoSQL Java
Serverless 应用引擎产品使用之在函数计算上部署Java服务并访问阿里云MongoDB如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
9 0
|
3天前
|
运维 Serverless Go
Serverless 应用引擎产品使用之在阿里云函数计算中,Go语言的函数计算服务Go程序没有正确打包如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
8 0
|
3天前
|
运维 Serverless 数据处理
Serverless 应用引擎产品使用之阿里云函数计算中的应用、服务及函数之间的关系如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
13 0
|
9天前
|
存储 开发工具 对象存储
Javaweb之SpringBootWeb案例之阿里云OSS服务入门的详细解析
Javaweb之SpringBootWeb案例之阿里云OSS服务入门的详细解析
15 0
|
9天前
|
存储 文字识别 安全
Javaweb之SpringBootWeb案例之阿里云OSS服务的详细解析
Javaweb之SpringBootWeb案例之阿里云OSS服务的详细解析
13 0
|
14天前
|
安全 Java 网络安全
对象存储oss使用问题之使用oss上服务器后显示服务异常如何解决
《对象存储OSS操作报错合集》精选了用户在使用阿里云对象存储服务(OSS)过程中出现的各种常见及疑难报错情况,包括但不限于权限问题、上传下载异常、Bucket配置错误、网络连接问题、跨域资源共享(CORS)设定错误、数据一致性问题以及API调用失败等场景。为用户降低故障排查时间,确保OSS服务的稳定运行与高效利用。
17 0
|
15天前
|
监控 安全 物联网
阿里云mqtt简介和使用流程
本文介绍了阿里云MQTT的准备工作、简介和使用流程。首先,用户需要注册阿里云账号并完成实名认证。接着,通过阿里云物联网平台创建产品和设备,获取连接所需的Broker Address、Port、Username和Password。然后,使用MQTT客户端(如MQTTX)配置这些信息进行连接,并激活设备。最后,创建并订阅/发布自定义Topic,实现设备间的通信。阿里云MQTT是一个适用于物联网设备的轻量级通信协议,提供高并发、高可靠性的服务,广泛应用于各种物联网场景。
阿里云mqtt简介和使用流程