協議裏還有2個主要的角色:
- client,客戶端
- broker,服務器端
它們是通過TCP/IP協議連接的。因爲MQTT是協議,所以不能拿來直接用的,就好比HTTP協議一樣。需要找實現這個協議的庫或者服務器來運行。
MQTT的官網見:http://mqtt.org/。其中http://mqtt.org/software裏面提供了官方推薦的各種服務器和客戶端使用的各種語言版本的API。
下面以服務器apache-apollo-1.7.1爲例,在windows環境下測試。
1、在這裏下載Apollo服務器,下載後解壓。如下圖所示:
bin下包含apollo和apollo.cmd兩個文件:
2、運行apache-apollo-1.7.1\bin\apollo.cmd,輸入create mybroker(名字任意取,這裏是根據官網介紹的來取的)創建服務器實例,服務器實例包含了所有的配置,運行時數據等,並且和一個服務器進程關聯。如果雙擊apollo.cmd出現閃一下就關閉的情況,則需要在命令行中敲入命令:
create mybroker之後會在bin目錄下生成mybroker文件夾。
裏面包含有很多信息,其中etc\apollo.xml文件下是配置服務器信息的文件,etc\users.properties文件包含連接MQTT服務器時用到的用戶名和密碼,後面會介紹,可以修改原始的admin=password,可以接着換行添加新的用戶名密碼。
3、打開cmd,運行apache-apollo-1.7.1\bin\mybroker\bin\apollo-broker.cmd run 開啓服務器,如下圖:
可以在瀏覽器中輸入http://127.0.0.1:61680/,其自動轉入:http://127.0.0.1:61680/console/index.html,apollo的登錄頁面。
此界面表示已經安裝成功:該登錄的用戶名和密碼在\apache-apollo-1.7.1\bin\mybroker\etc\users.properties裏,打開users.properties文件:
## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements. See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License. You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------
#
# The list of users that can login. This file supports both plain text or
# encrypted passwords. Here is an example what an encrypted password
# would look like:
#
# admin=ENC(Cf3Jf3tM+UrSOoaKU50od5CuBa8rxjoL)
#
admin=password
經過上面的簡單步驟,服務器基本上就已經完成。輸入admin,password就可以登錄了,如下圖: