1. 下载Docker镜像
复制代码
1docker pull emqx/emqx
2. 后台运行镜像
复制代码
1docker run -dit --name emqx -p 18083:18083 -p 1883:1883 -p 8083:8083 -p 8084:8084 emqx/emqx:latest
3. 进入emqx的docker 命令
复制代码
1docker exec -it emqx /bin/sh
4.访问emqt的web管理页面
-
http://127.0.0.1:18083
-
#账号: admin
-
#密码: public
5.端口
-
1883:MQTT 协议端口
-
8883:MQTT/SSL 端口
-
8083:MQTT/WebSocket 端口
-
8080:HTTP API 端口
-
18083:Dashboard 管理控制台端口
6.python 链接mqtt 创建发布端
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51# python 3.6 import random import time from paho.mqtt import client as mqtt_client broker = '127.0.0.1' port = 1883 topic = "林中静月下仙" # generate client ID with pub prefix randomly client_id = f'python-mqtt-{random.randint(0, 1000)}' def connect_mqtt(): def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected to MQTT Broker!") else: print("Failed to connect, return code %dn", rc) client = mqtt_client.Client(client_id) client.on_connect = on_connect client.connect(broker, port) return client def publish(client): msg_count = 0 while True: time.sleep(1) msg = f"messages: {msg_count}" result = client.publish(topic, msg) # result: [0, 1] status = result[0] if status == 0: print(f"Send `{msg}` to topic `{topic}`") else: print(f"Failed to send message to topic {topic}") msg_count += 1 def run(): client = connect_mqtt() client.loop_start() publish(client) if __name__ == '__main__': run()
复制代码
1
2
3
4
5Connected to MQTT Broker! Send `messages: 0` to topic `林中静月下仙` Send `messages: 1` to topic `林中静月下仙` Send `messages: 2` to topic `林中静月下仙` Send `messages: 3` to topic `林中静月下仙`
7.python 链接mqtt 创建订阅端
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43# python3.6 import random from paho.mqtt import client as mqtt_client broker = '127.0.0.1' port = 1883 topic = "林中静月下仙" # generate client ID with pub prefix randomly client_id = f'python-mqtt-{random.randint(0, 100)}' def connect_mqtt() -> mqtt_client: def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected to MQTT Broker!") else: print("Failed to connect, return code %dn", rc) client = mqtt_client.Client(client_id) client.on_connect = on_connect client.connect(broker, port) return client def subscribe(client: mqtt_client): def on_message(client, userdata, msg): print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic") client.subscribe(topic) client.on_message = on_message def run(): client = connect_mqtt() subscribe(client) client.loop_forever() if __name__ == '__main__': run()
复制代码
1
2
3
4
5Connected to MQTT Broker! Received `messages: 12` from `林中静月下仙` topic Received `messages: 13` from `林中静月下仙` topic Received `messages: 14` from `林中静月下仙` topic Received `messages: 15` from `林中静月下仙` topic
最后
以上就是年轻乌冬面最近收集整理的关于Docker安装Emqx的全部内容,更多相关Docker安装Emqx内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复