Install MQTT via Docker Compose

1. File and directory description

1. Files and directories during MQTT installation

After the EMQX installation is complete, some directories will be created to store running files and configuration files, store data and record logs.

The locations of files and directories obtained by different installation methods are different, as follows:

insert image description here
注意:

  • When the compressed package is decompressed and installed, the directory is relative to the directory where the software is located;
  • Docker 容器使用压缩包解压安装的方式,软件安装于 /opt/emqx 目录中
  • The data, log, and plugins directories can be set through configuration files. It is recommended to mount the data directory to a high-performance disk for better performance. But for nodes belonging to the same cluster, the configuration of the data directory should be the same.

2. File and directory description

Here are some of the directories, the files and subfolders contained in them.

insert image description here
注意:
The configuration items of EMQX are stored in the etc and data/configs directories. The main difference between the two is that the etc directory stores read-only configuration files, and the configuration submitted by users through the Dashboard and REST API will be saved in the data/configs directory, and supports Hot update at runtime.

EMQX reads these configurations and merges them into Erlang native configuration file format, so that these configurations can be applied at runtime.

Configuration file manual: https://www.emqx.io/docs/zh/v5.1/configuration/configuration.html

2. Install MQTT through Docker Compose

1. Download the installation package of emqx-5.1.6

insert image description here

Later, when starting through Docker Compose, an error is found, and it will prompt us that some files are missing. Therefore, we copy the files needed in the downloaded compressed package to our mapped data volume in advance. The files are in /opt/emqx/ inside the compressed package.

通过我再三的试错摸索,emqx-5.1.6版本需要提前把下面几个文件复制到我们对应的数据卷中。

└── emqx
    ├── data
    │   └── configs
    ├── etc
        ├── acl.conf
        ├── certs
        │   ├── cacert.pem
        │   ├── cert.pem
        │   ├── client-cert.pem
        │   ├── client-key.pem
        │   ├── key.pem
        │   └── README
        ├── emqx.conf
        └── vm.args

2. Prepare files and directories

1) Create an mqtt_emqx5 folder and the directory where the data volume is mounted in any directory.

[root@centos7 ~]# mkdir /usr/local/src/mqtt_emqx5
[root@centos7 ~]# cd /usr/local/src/mqtt_emqx5
# 数据卷
[root@centos7 mqtt_emqx5]# mkdir -p ./emqx/data
[root@centos7 mqtt_emqx5]# mkdir -p ./emqx/etc
[root@centos7 mqtt_emqx5]# mkdir -p ./emqx/log

2) Copy the files mentioned in the compressed package above to our data volume directory

# 复制完之后,给挂载目录赋予权限
[root@centos7 mqtt_emqx5]# chmod 777 -R ./emqx/

Create and write docker-compose.yaml

3)创建并编写docker-compose.yaml

[root@centos7 mqtt_emqx5]# cat docker-compose.yaml
version: '3.7'

services:
  emqx5_s1:
    image: "emqx/emqx:5.1.6"
    container_name: "emqx5_s1"
    environment:
     - "EMQX_NAME=emqx5_s1"
     - "EMQX_HOST=127.0.0.1"
    ports:
     - 1883:1883
     - 8083:8083
     - 8084:8084
     - 8883:8883
     - 18083:18083
    volumes:
     - $PWD/emqx/etc:/opt/emqx/etc
     - $PWD/emqx/data:/opt/emqx/data
     - $PWD/emqx/log:/opt/emqx/log
    #restart: always
[root@centos7 mqtt_emqx5]#

Description of each port of the MQTT service enabled by EMQX by default:

  • 1883: MQTT protocol port
  • 8883: MQTT/SSL port
  • 8083: MQTT/WebSocket port
  • 8084: MQTT/WebSocket/SSL port
  • 8080: HTTP API port
  • 18083: Dashboard Management Console Port

The contents of the final mqtt_emqx5 folder are as follows:

[root@centos7 mqtt_emqx5]# tree
.
├── docker-compose.yaml
└── emqx
    ├── data
    │   └── configs
    ├── etc
    │   ├── acl.conf
    │   ├── certs
    │   │   ├── cacert.pem
    │   │   ├── cert.pem
    │   │   ├── client-cert.pem
    │   │   ├── client-key.pem
    │   │   ├── key.pem
    │   │   └── README
    │   ├── emqx.conf
    │   └── vm.args
    └── log

6 directories, 10 files

3. Start MQTT

  • The containers started with docker-compose up are all in the foreground, and the console will print the output information of all containers at the same time, which is convenient for debugging. When the command is stopped by Ctrl-C, the container will also stop.
  • Using the docker-compose up -d command will start and run the container in the background, which is generally recommended.

一般进入到 docker-compose.yml的上下文目录中,执行命令:

  • Start the project: docker-compose up -d
  • Stop the project: docker-compose down
[root@centos7 mqtt_emqx5]# docker-compose up
Starting emqx5_s1 ... done
Attaching to emqx5_s1
emqx5_s1    | WARNING: Default (insecure) Erlang cookie is in use.
emqx5_s1    | WARNING: Configure node.cookie in /opt/emqx/etc/emqx.conf or override from environment variable EMQX_NODE__COOKIE
emqx5_s1    | WARNING: NOTE: Use the same cookie for all nodes in the cluster.
emqx5_s1    | EMQX_RPC__PORT_DISCOVERY [rpc.port_discovery]: manual
emqx5_s1    | EMQX_NODE__NAME [node.name]: [email protected]
emqx5_s1    | Listener ssl:default on 0.0.0.0:8883 started.
emqx5_s1    | Listener tcp:default on 0.0.0.0:1883 started.
emqx5_s1    | Listener ws:default on 0.0.0.0:8083 started.
emqx5_s1    | Listener wss:default on 0.0.0.0:8084 started.
emqx5_s1    | Listener http:dashboard on :18083 started.
emqx5_s1    | EMQX 5.1.6 is running now!

You can access the ip:18083 port through the browser (the default user name is admin, the password is public), and a management console webpage will open after successful login. The main interface is as follows:

insert image description here

3. MQTTX client

Official download, fool-like installation: https://mqttx.app/zh

1. Establish a connection

Create a new MQTT server connection.

insert image description here

2. Subscribe to topics

insert image description here

The MQTT protocol forwards messages according to topics. Topics are used to identify and distinguish different messages, which is the basis of MQTT message routing.

主题通过 / 来区分层级,类似于 URL 路径。MQTT 主题支持以下两种通配符:+ 和 #。

  • +: Indicates a single-level wildcard, for example, a/+ matches a/x or a/y.
  • #: Indicates multi-level wildcards, for example, a/# matches a/x, a/b/c/d.

注意:Wildcard topics can only be used for subscription, not for publishing.

For more details about MQTT topics, please refer to the article Understanding MQTT Topics and Wildcards Through Cases: https://www.emqx.com/zh/blog/advanced-features-of-mqtt-topics

For more usage of the MQTTX client, see the official documentation.

– If you are hungry for knowledge, be humble if you are foolish.

Guess you like

Origin blog.csdn.net/qq_42402854/article/details/132645459