一、简介
    最近,在做一个项目的时候,需要在线管理网络继电器,以前也做过硬件的项目,但是这样的项目不多。现在我想实现一个在线可以接受网络继电器发送的信号,也可以向网络继电器发送命令,控制其的运行。这个功能的实现需要物联网的支持,我在咨询了网络继电器卖家,想要在线实现网络硬件的管理有三种解决方案。
    第一种:由于我买的是阿里云的服务器,可以在买阿里云的IOT组件,而且提供了全部功能,提供优化的接口,硬件、网络的管理和维护都不用关心,但是费用不菲。
    第二种:如果公司内部有特牛的 TCP/IP 软件工程师的话,可以自己写一套出来,但是这个开发过程、调试过程、维护过程等,周期比较长,这样的人也不好找,性价比不高。
    第三种:可以自己在云服务器上搭建MQTT服务器,通过其组件实现在线管理,这种方案总体来说,性价比最高。
    我选择的是第三种解决方案,我已经在云服务器上搭建了自己的MQTT服务器,硬件之间也可以实现管理了,我今天就把过程记录下来,以防以后使用的时候到处乱找,而且网上的有的也不是很全和很详细,如果大家有需要,也可以直接使用。我写的很详细,小白照着做都没有问题,高手就略过吧。

    我的搭建环境如下:
        云平台:阿里云平台
        操作系统:Windows Server 2022 Datacenter
        机器内存:8.00 GB (7.73 GB 可用)
        系统类型:64 位操作系统, 基于 x64 的处理器
        MQTT服务器:Apache-Apollo
        MQTT客户端:MQTT.fx

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

二、配置详情
    想要安装 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

            

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

            

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

              

        2)、安装JDK步骤没有什么可说的,直接下一步就可以,根据自己的需求更改就可以。
            1】、第一步要先安装好JDK的运行环境, 点击jdk-8u271-windows-x64安装包,点击运行后弹出下列界面,点击下一步
                
            2】、 我把安装包放在了自定义目录下,好多国外软件安装路径出现中文或空格都会出现错误,所以我为了避免这种情况 安装国外软件的时候路径都不包含中文和空格,选择默认路径也可以。
                
        3)、配置Java环境变量
            1】、配置系统的环境变量
                 

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

            2】、新建:

                 变量名:JAVA_HOME

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


            3】、新建:

                 变量名:CLASSPATH

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

                 


            4】、添加:找到PATH变量,选择编辑。
                 把  %JAVA_HOME%bin;%JAVA_HOME%jrebin 添加到”变量值”的结尾处。
                 

           5】、确保电脑安装了Java 运行时环境 JRE 或者 JDK ( version >=1.7 )
                 在命令窗口中,输入java命令:java -version,回车,如果出现版本信息,说明安装成功。

                                     

    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:http://archive.apache.org/dist/activemq/activemq-apollo/1.7.1/

            

        2)、 在CMD下输入 apollo.cmd create mqttServer,显示创建完成如下
             下载Apollo服务器并解压,在apache-apollo-1.7.1bin的路径栏输入cmd运行,这种方式便于测试,如果想正常使用,安装为 Windows 服务。

            

            
            成功创建文件夹。
            

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

            
 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>

        4)、修改 users.properties 文件,我保持的默认。
             apache-apollo-1.7.1binmqttServeretcusers.properties 这个文件可以配置登录账号密码,示例有一个admin=password, admin是账号,password是密码,客户端连接服务器和web网页登录所用的账号密码都是这个。
            

            源码如下:

 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
        5)、配置 groups.properties 文件。
            apache-apollo-1.7.1binmqttServeretcgroups.properties 这个文件可以配置多个账户,如示例中的,示例是admins=admin 如果想添加多个账户可以 admins=admin|test ,其中test就是新增账户。
            

            源码如下:

 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

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

            

        7)、客户端(外网客户端和服务器本地,访问方式不同)测试运行。   
             如果在服务器本地访问,可以直接访问:http://localhost:61680,或者 http://127.0.0.1:61680,都可以访问,会出现如下界面,说明配置成功。
            当然,如果是在外网访问的话,就要使用服务器公网IP+端口号,否则也是不通的。比如:http://1**.9*.1**.1**:61680/
             
              其中connectors选项下的TCP就代表MQTT的访问
             如果我们有MQTT客户端连接的话,这里也是有显示的。
            
              这就是我们当前的链接

              


        8)、安装 Windows 服务。
            这个命令的目录是:apache-apollo-1.7.1binmqttServerbin
            
            1】、执行 apollo-broker-service install 命令和 apollo-broker-service start 命令。
                 

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

    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)
        
        1)、下载地址:https://pan.baidu.com/s/1jwA7GUcT8h-rk_kt4CZq1A  ,现在只有百度网盘有了1.7.1版本的,提取码:1234。
          我在贴一个地址:https://softblade.de/en/download-2/ 这个应该是可以的,安装很简单,就不多说了,我就说说如何使用把。

            【1】、首先配置 MQTT 代理
                

                           

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

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

                

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

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

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

三、总结    
    好了,今天就写道这里了,这个测试也挺麻烦的,不熟悉的也踩草不少坑,谁都有第一次,慢慢来就好了。最近接触了很多新的东西,也学到了不少东西,每天有进步,还是很开心的,不忘初心,继续努力吧。
内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/PatrickLiu/p/17547286.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!