十七、Rust集成MQTT Client

1、信息整理

目前了解到的Rust MQTT项目有:

  • bytebeamio/rumqtt

  • ntex-rs/ntex-mqttcrates/ntex-mqtt

    • 258 star、504 commits、Contributors 15
    • ntex、MQTT Client/Server、v5 and v3.1.1 protocols
  • eclipse/paho.mqtt.rust

    • 463 star、368 commits、
    • 异步、SSL/TLS、cargo add paho-mqtt = "0.12"
    • 但看自述文档所述,用于 musl linux 环境,有代码侵入。
    rust 复制代码
    fn is_musl() -> bool {
        std::env::var("CARGO_CFG_TARGET_ENV").unwrap() == "musl"
    }
  • rmqtt/rmqtt:355 star,871 commits

    • 基于 ( tokio、ntex、ntex-mqtt )
    • 一个纯服务端,遍观项目,貌似不能做 client 使用。自身的测试,使用 paho.mqtt.testing 项目完成。
    • 分布式集群(raft),TLS,WebSocket,WebSocket-TLS,x86/Arm
    • 单个服务节点上处理百万级别的并发客户端。集群提供相同的连接量、吞吐量,应该只是解决高可用问题。

2、开始集成

本次集成作为 Client 使用,选择了最前面的 bytebeamio/rumqtt

  • boot/mqtt.rs
rust 复制代码
use std::sync::Arc;
use std::time::Duration;

use once_cell::sync::OnceCell;
use rumqttc::{AsyncClient, MqttOptions};
use serde::{Deserialize, Serialize};

pub static MQTT_CLIENT: OnceCell<Arc<AsyncClient>> = OnceCell::new();

#[derive(Debug, Default, Validate, Serialize, Deserialize)]
pub struct MqttMessage {
    pub addr: Option<String>,
    #[validate(range(min = 0, max = 1))]
    pub action: u8,
}

pub async fn start() {
    let mut mqtt_options = MqttOptions::new("rumqtt-async", "192.168.1.110", 1883);
    mqtt_options.set_keep_alive(Duration::from_secs(5));
    let (client, mut event_loop) = AsyncClient::new(mqtt_options, 100);
    MQTT_CLIENT.get_or_init(|| { Arc::new(client) });

    tokio::spawn(async move {
        loop { while let Ok(notification) = event_loop.poll().await {}; }
    });

    log::info!("this point run ...");
}
  • boot/mod.rs
rust 复制代码
pub mod mqtt;

pub async fn start() {
    // xxx others initialization
    mqtt::start().await;
}
  • modules/mod.rs
rust 复制代码
pub mod switch;

pub mod handler {
    use actix_web::dev::HttpServiceFactory;
    use actix_web::web;

    use crate::module::{switch};

    pub fn api_routes() -> impl HttpServiceFactory {
        web::scope("")
            .service(switch::api::index)
    }
}
  • modules/switch/api.rs
rust 复制代码
use rumqttc::QoS::AtLeastOnce;
use validator::Validate;

use crate::boot::mqtt::{MQTT_CLIENT, MqttMessage};

#[post("/switch/{id}")]
pub async fn index(Path(id): Path<String>, mut msg: Json<MqttMessage>) -> impl Responder {
    let ok = vec!["Hello World!", "Hello World!"];

    if let Err(e) = msg.validate() { return HttpResponse::BadRequest().json(e); }
    msg.addr = Some(id);
    let json = serde_json::to_string(&msg).unwrap();
    MQTT_CLIENT.get().unwrap().publish("/hello", AtLeastOnce, false, json).await.unwrap();

    HttpResponse::Ok().json(ok)
}
rust 复制代码
use actix_web::{App, HttpServer, middleware};
use booking::{boot, module};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    boot::start().await;
    HttpServer::new(move || App::new()
        // TODO other initialization
        .service(module::handler::api_routes())
        // other routes
    ).bind("0.0.0.0:8080")?.run().await
}

代码贴完,剩下真是没啥可说的,拜了个 bye ~

相关推荐
程序员爱钓鱼10 分钟前
Go语言实战案例 — 项目实战篇:简易博客系统(支持评论)
前端·后端·go
追逐时光者7 小时前
精选 4 款基于 .NET 开源、功能强大的 Windows 系统优化工具
后端·.net
TF男孩7 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
AAA修煤气灶刘哥8 小时前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
AAA修煤气灶刘哥8 小时前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
你的人类朋友9 小时前
什么是API签名?
前端·后端·安全
昵称为空C11 小时前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
架构师沉默11 小时前
设计多租户 SaaS 系统,如何做到数据隔离 & 资源配额?
java·后端·架构
RoyLin12 小时前
TypeScript设计模式:适配器模式
前端·后端·node.js
该用户已不存在12 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust