Docker環境下PHP安裝Mosquitto擴展,並配合Swoole測試。PHP對MQTT的使用——完整教程


最近在工作中碰到物聯網相關的項目,所以為了幫助理解MQTT,直接擼一個Demo。至於為啥選擇MQTT大家可以自行搜索做拓展了解。補充一點:網上MQTT服務還是挺多的,選擇Mosquitto入手的原因是因為本身php就有Mosquitto的擴展。並且Mosquitto服務也是支持多語言的,所以對於協同開發來說還是比較友好的。以下教程全程在docker環境中部署,所以無關操作系統

 

1、首先我們需要一台Mosquitto的消息代理服務,這里我直接選擇使用docker的官方鏡像

 

按照他官方鏡像的參考文檔,我們會需要有配置、持久存儲和日志文件。那么我選擇的目錄結構如下

點擊查看代碼
Mosquitto	主目錄
|-data			   持久化數據目錄
|-log			   日志目錄
|-mosquitto.conf   	   配置文件	
|-pwdfile		   密碼文件

 

接下來我們先編寫 mosquitto 的配置文件也就是上面的 mosquitto.conf

點擊查看代碼
# =================================================================
# General configuration
# =================================================================

# 客戶端心跳的間隔時間
#retry_interval 20

# 系統狀態的刷新時間
#sys_interval 10

# 系統資源的回收時間,0表示盡快處理
#store_clean_interval 10

# 服務進程的PID
#pid_file /var/run/mosquitto.pid

# 服務進程的系統用戶
#user mosquitto

# 客戶端心跳消息的最大並發數
#max_inflight_messages 10

# 客戶端心跳消息緩存隊列
#max_queued_messages 100

# 用於設置客戶端長連接的過期時間,默認永不過期
#persistent_client_expiration

# =================================================================
# Default listener
# =================================================================

# 服務綁定的IP地址
#bind_address

# 服務綁定的端口號,老版本的mosquitto使用的port設置端口,新版改用listener
#port 1883

#新版使用的端口配置
listener 1883 0.0.0.0

# 允許的最大連接數,-1表示沒有限制
max_connections 1000

# cafile:CA證書文件
# capath:CA證書目錄
# certfile:PEM證書文件
# keyfile:PEM密鑰文件
#cafile
#capath
#certfile
#keyfile

# 必須提供證書以保證數據安全性
#require_certificate false

# 若require_certificate值為true,use_identity_as_username也必須為true
#use_identity_as_username false

# 啟用PSK(Pre-shared-key)支持
#psk_hint

# SSL/TSL加密算法,可以使用“openssl ciphers”命令獲取
# as the output of that command.
#ciphers

# =================================================================
# Persistence
# =================================================================

# 消息自動保存的間隔時間
#autosave_interval 1800

# 消息自動保存功能的開關
#autosave_on_changes false

# 持久化功能的開關
persistence true

# 持久化DB文件
#persistence_file mosquitto.db

# 持久化DB文件目錄
persistence_location /mosquitto/data

# =================================================================
# Logging
# =================================================================

# 4種日志模式:stdout、stderr、syslog、topic
# none 則表示不記日志,此配置可以提升些許性能
log_dest file /mosquitto/log/mosquitto.log

# 選擇日志的級別(可設置多項)
log_type error
log_type warning
log_type notice
log_type information

# 是否記錄客戶端連接信息
#connection_messages true

# 是否記錄日志時間
log_timestamp true

# =================================================================
# Security
# =================================================================

# 客戶端ID的前綴限制,可用於保證安全性
#clientid_prefixes

# 允許匿名用戶
allow_anonymous false

# 用戶/密碼文件,默認格式:username:password
password_file /mosquitto/pwdfile

# PSK格式密碼文件,默認格式:identity:key
#psk_file

# pattern write sensor/%u/data
# ACL權限配置,常用語法如下:
# 用戶限制:user <username>
# 話題限制:topic [read|write] <topic>
# 正則限制:pattern write sensor/%u/data
#acl_file

# =================================================================
# Bridges
# =================================================================

# 允許服務之間使用“橋接”模式(可用於分布式部署)
#connection <name>
#address <host>[:<port>]
#topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]

# 設置橋接的客戶端ID
#clientid

# 橋接斷開時,是否清除遠程服務器中的消息
#cleansession false

# 是否發布橋接的狀態信息
#notifications true

# 設置橋接模式下,消息將會發布到的話題地址
# $SYS/broker/connection/<clientid>/state
#notification_topic

# 設置橋接的keepalive數值
#keepalive_interval 60

# 橋接模式,目前有三種:automatic、lazy、once
#start_type automatic

# 橋接模式automatic的超時時間
#restart_timeout 30

# 橋接模式lazy的超時時間
#idle_timeout 60

# 橋接客戶端的用戶名
#username

# 橋接客戶端的密碼
#password

# bridge_cafile:橋接客戶端的CA證書文件
# bridge_capath:橋接客戶端的CA證書目錄
# bridge_certfile:橋接客戶端的PEM證書文件
# bridge_keyfile:橋接客戶端的PEM密鑰文件
#bridge_cafile
#bridge_capath
#bridge_certfile
#bridge_keyfile

# 自己的配置可以放到以下目錄中
#include_dir /etc/mosquitto/conf.d

 

然后咱們直接把 mosquitto 容器服務起起來就行了 

點擊查看代碼
# 拉取 mosquitto 官方鏡像
docker pull eclipse-mosquitto

# 創建並運行容器
docker run -itd --name mosquitto -p 1883:1883 -v mosquitto.conf:/mosquitto/mosquitto.conf -v ./data:/mosquitto/data -v ./log:/mosquitto/log eclipse-mosquitto

 

 到這一步還沒完,我們還需要給 mosquitto 服務配置用戶名密碼。首先我們在 pwdfile 寫入一對用戶名密鑰對格式如下

點擊查看代碼
# 用戶名:密碼
admin:123456

 

配置完成之后我們需要進入到容器里面執行一段密碼文件的加密指令,否則 mosquitto 服務將無法認證該密鑰對(如果是使用 Dockerfile 創建的容器可以直接在編寫 Dockerfile 完成該步驟) 

點擊查看代碼
# 進入 docker 容器
docker exec -it [容器id] sh

# 執行加密
mosquitto_passwd -U /mosquitto/mosquitto.conf

 

然后我們就可以看到 pwdfile 文件里面的內容已經被 hash 過了的密文,接着重啟 mosquitto 容器即可。此時 mosquitto 服務已經成功部署

 

 

2、接下來咱們部署PHP環境,並且簡單使用一下 mosquitto 服務

 

這里介紹一下我的目錄結構

點擊查看代碼
Mosquitto	主目錄
|-Dockerfile	
|-docker-compose.yml
|-mqtt_pub.php	發布demo	
|-mqtt_sub.php	訂閱demo

 

下面我們編寫一下 mqtt_pub.php 和 mqtt_sub.php 文件, mosquitto 的php文檔可以參考:mosquitto的php文檔

mqtt_pub.php

點擊查看代碼
<?php
define('BASE_PATH', __DIR__);

use Swoole\Coroutine;
use Swoole\Runtime;

Runtime::enableCoroutine();

$client = [];

try {

    for ($i = 0; $i < 10; $i++) {
        $client[$i] = new Mosquitto\Client();
        $client[$i]->setCredentials('admin', '123456');
        $client[$i]->setWill('dead', 'I am offline ', 1, false);
        $client[$i]->connect('127.0.0.1', 1883, 60);
    }

    foreach ($client as $key => $value) {
        Coroutine::create(function () use ($key, $value) {
            //usleep(rand(0,9999));
            var_dump($key);
            $value->onConnect(function (int $rc, string $message) {
                var_dump($rc, $message);
            });

            $value->onDisconnect(function (int $rc) {
                var_dump($rc);
            });

            while (true) {
                $value->loop();
                $value->publish('myMqtt', (string)$key, 1);
                $value->loop();
                usleep(rand(100, 999));
            }
        });
    }

} catch (\Mosquitto\Exception $exception) {
    var_dump('err', $exception->getMessage());
}

 

mqtt_sub.php

點擊查看代碼
<?php
define('BASE_PATH', __DIR__);

try {
    $client = new Mosquitto\Client();
    $client->setCredentials('admin', '123456');
    $client->connect('127.0.0.1', 1883, 60);

    $client->onConnect(function (int $rc, string $message) {
        var_dump($rc, $message);
    });

    $client->onDisconnect(function (int $rc) {
        var_dump($rc);
    });

    $client->subscribe('myMqtt', 2);
    $client->subscribe('dead', 1);

    $client->onMessage(function ($data) {
        /** @var Mosquitto\Message $data */
        var_dump($data->payload);
    });

    $client->loopForever();
}catch (\Mosquitto\Exception $exception) {
    var_dump('err',$exception->getMessage());
}

 

接下來是PHP環境搭建,這里我采用的 swoole 的官方鏡像,還需要安裝 php 的 mosquitto 擴展,所以這里寫了一份Dockerfile

點擊查看代碼
FROM phpswoole/swoole:4.7.1-php7.4

LABEL maintainer="Taurus12C"

ARG timezone

ENV TIMEZONE=${timezone:-"Asia/Shanghai"}

RUN ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
    && echo "${TIMEZONE}" > /etc/timezone \
    && sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list \
    && apt-get clean \
    && apt-get update -y \
    && apt-get upgrade -y \
    && apt-get install libmosquitto-dev -y \
    && pecl install Mosquitto-alpha \
    && docker-php-ext-enable mosquitto

WORKDIR /opt/www

COPY . /opt/www

 

接下來就是使用上面的鏡像文件啟動一個容器,這里可以用 docker build 之后再 docker run,或者直接使用 docker-compose 我這里使用的后者

點擊查看代碼
version: "3.5"
services:
  demo:
    build: .
    restart: always
    container_name: demo
    networks:
      - test
    volumes:
      - ./:/opt/www
networks:
  test:
    external: true
    #我這里用了一個名為test的docker網絡,注意自己使用的時候要先執行 docker network create test 一下

 

然后直接啟動容器,打開兩個 CLI 窗口進入容器執行php代碼,查看效果

點擊查看代碼
docker-compose up -d

 

第一個 CLI 窗口

點擊查看代碼
# 進入容器
docker exec -it [容器id] sh

# 執行訂閱代碼文件
php mqtt_sub.php

 

第二個 CLI 窗口

點擊查看代碼
# 進入容器
docker exec -it [容器id] sh

# 執行訂閱代碼文件
php mqtt_pub.php

 

效果如下所示:

     

 

大功告成!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM