我是靠谱客的博主 年轻乌冬面,最近开发中收集的这篇文章主要介绍Docker安装Emqx,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1. 下载Docker镜像

docker pull emqx/emqx

2. 后台运行镜像

docker run -dit --name emqx -p 18083:18083 -p 1883:1883 -p 8083:8083 -p 8084:8084 emqx/emqx:latest

3. 进入emqx的docker 命令

docker exec -it  emqx /bin/sh

4.访问emqt的web管理页面

  1. http://127.0.0.1:18083

  2. #账号: admin

  3. #密码: public

5.端口

  1. 1883:MQTT 协议端口

  2. 8883:MQTT/SSL 端口

  3. 8083:MQTT/WebSocket 端口

  4. 8080:HTTP API 端口

  5. 18083:Dashboard 管理控制台端口

6.python 链接mqtt 创建发布端

# 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()
Connected 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 创建订阅端

# 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()
Connected 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所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部