技術メモ

技術メモ

ラフなメモ

MQTTの概要とPub/Sub疎通確認

MQTT とは

概要

  • MQTT = Message Queuing Telemetry Transport
  • TCP/IP による Pub/Sub型データ配信モデルなメッセージキュープロトコル
    • メッセージの再配布が可能
  • HTTP プロトコルと比較するとヘッダサイズが小さく、通信のオーバヘッドが少ない
    • ヘッダサイズが最小で 2 byte
      • HTTP の場合は最小でも約 20 byte
    • シンプルなプロトコルシーケンス
  • MQTT クライアントが 3 種類の QoS を選ぶことができる
    • QoS0 (At Most Once)
      • メッセージが確実に届く保証はない
      • メッセージ配布に失敗をしても再送をしない
    • QoS1 (At Least Once)
      • 必ずメッセージを配布するが、重複する可能性がある
    • QoS2 (Exactly Once)
      • 必ずメッセージを配布して、重複も発生しない
  • MQTT ブローカー(MQサーバ)例

MQTT クライアントからデータハブにデータを送信するまでの一例

f:id:tutuz:20190317212606p:plain

MQTT をインストールして、動かしてみる

インストール

home_oojah_mqtt のレポジトリから mosquitto と mosquitto-clients をダウンロードします。

# wget http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/home:oojah:mqtt.repo -O "/etc/yum.repos.d/Mosquitto.repo"
# yum install epel-release
# yum --enablerepo=epel install libwebsockets
# yum install mosquitto mosquitto-clients

mosquitto-clients をダウンロードするときに依存関係解消の際に mosquitto の取得元レポジトリが重複する場合があるので、 eprl のレポジトリが有効になっていて、かつ、ダウンロードでうまく行かなかった場合は eprl のレポジトリを enabled=0 とすると良いです。以下のような場合です。

yum install mosquitto-clients

# yum install mosquitto-clients
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.fairway.ne.jp
 * epel: www.ftp.ne.jp
 * extras: mirror.fairway.ne.jp
 * updates: mirror.fairway.ne.jp
Resolving Dependencies
--> Running transaction check
---> Package mosquitto-clients.x86_64 0:1.5.5-1.2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================================================================================================================================
 Package                                                                Arch                                                        Version                                                         Repository                                                            Size
===============================================================================================================================================================================================================================================================================
Installing:
 mosquitto-clients                                                      x86_64                                                      1.5.5-1.2                                                       home_oojah_mqtt                                                       38 k

Transaction Summary
===============================================================================================================================================================================================================================================================================
Install  1 Package

Total size: 38 k
Installed size: 95 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test


Transaction check error:
  file /usr/bin/mosquitto_pub from install of mosquitto-clients-1.5.5-1.2.x86_64 conflicts with file from package mosquitto-1.5.5-2.el7.x86_64
  file /usr/bin/mosquitto_sub from install of mosquitto-clients-1.5.5-1.2.x86_64 conflicts with file from package mosquitto-1.5.5-2.el7.x86_64

Error Summary
-------------

[root@kafka-1 ~]# yum remove mosquitto
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package mosquitto.x86_64 0:1.5.5-2.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

動かしてみる

コンソールを 3 つ起動させておきます。3 つのホストで試してみました。

1 つ目 (mosquitto サーバ:192.168.33.31)

# mosquitto
1552825047: mosquitto version 1.5.5 starting
1552825047: Using default config.
1552825047: Opening ipv4 listen socket on port 1883.
1552825047: Opening ipv6 listen socket on port 1883.
1552825264: New connection from 192.168.33.32 on port 1883.
1552825264: New client connected from 192.168.33.32 as mosqsub|4980-kafka-2 (c1, k60).
1552825306: New connection from 192.168.33.33 on port 1883.
1552825306: New client connected from 192.168.33.33 as mosqpub|5327-kafka-3 (c1, k60).
1552825306: Client mosqpub|5327-kafka-3 disconnected.

...

2 つ目 (Subscriber:192.168.33.32)

# mosquitto_sub -d -t orz -h 192.168.33.31
Client mosqsub|4980-kafka-2 sending CONNECT
Client mosqsub|4980-kafka-2 received CONNACK (0)
Client mosqsub|4980-kafka-2 sending SUBSCRIBE (Mid: 1, Topic: orz, QoS: 0)
Client mosqsub|4980-kafka-2 received SUBACK
Subscribed (mid: 1): 0
Client mosqsub|4980-kafka-2 received PUBLISH (d0, q0, r0, m0, 'orz', ... (16 bytes))
Hello mosquitto.
Client mosqsub|4980-kafka-2 sending PINGREQ
Client mosqsub|4980-kafka-2 received PINGRESP

...

3 つ目 (Publisher:192.168.33.33)

# mosquitto_pub -d -t orz -m "Hello mosquitto." -h 192.168.33.31
Client mosqpub|5327-kafka-3 sending CONNECT
Client mosqpub|5327-kafka-3 received CONNACK (0)
Client mosqpub|5327-kafka-3 sending PUBLISH (d0, q0, r0, m1, 'orz', ... (16 bytes))
Client mosqpub|5327-kafka-3 sending DISCONNECT

mosquitto サーバの標準出力の内容から、想定通り mosquitto_pub / mosquitto_sub できていることがわかります。簡単なケースですが、かなりシンプルにメッセージを Publisher から Subscriber に送達することができました。

参考

あとで読みたい