Pub/Subモデルのmqttプロトコルの動作確認
イメージ
※Publisherがbrokerにデータを登録し、接続されているSubscriberにデータを流す。
ブローカのDockerfile
Dockerfile-broker
FROM ubuntu:20.04
RUN apt-get update
RUN apt-get install -y mosquitto
# ブローカの起動
CMD ["mosquitto", "-c", "/etc/mosquitto/mosquitto.conf"]
各クライアントのDockerfile
Dockerfile-client
FROM ubuntu:18.04
RUN apt-get update
RUN apt-get install -y mosquitto-clients
docker-compose
docker-compose.yml
version: "3.7"
services:
# ブローカ
broker:
build:
context: .
dockerfile: Dockerfile-broker
# publisher
pub:
build:
context: .
dockerfile: Dockerfile-client
tty: true
# subscriber1
sub1:
build:
context: .
dockerfile: Dockerfile-client
tty: true
# subscriber2
sub2:
build:
context: .
dockerfile: Dockerfile-client
tty: true
起動
docker-compose up
下記コマンドを実行してコンテナに入る
subscriber側で要求をする
mosquitto_sub -h broker -t sample-topics
publisher側でデータ送信
mosquitto_pub -h broker -t sample-topics -m "send to subscriber"