• 【一起学Rust | 进阶篇 | RMQTT库】RMQTT消息服务器——安装与集群配置



    RMQTT Broker 简介

    RMQTT 是一款完全开源,高度可伸缩,高可用的分布式 MQTT 消息服务器,适用于 IoT、M2M 和移动应用程序,可以在单个服务节点上处理百万级别的并发客户端。

    RMQTT 目前支持的操作系统:

    • Linux
    • macOS
    • Windows Server

    安装

    安装分为zip解压安装和源码编译安装,我们分开介绍。

    ZIP 压缩包安装(Linux、MacOS、Windows)

    需从 GitHub Release 页面获取相应操作系统的二进制软件包。

    1. GitHub Release 下载zip包。
    $ wget "https://github.com/rmqtt/rmqtt/releases/download/v0.2.3/rmqtt-0.2.3-x86_64-unknown-linux-musl.zip"
    
    • 1
    1. 解压从GitHub Release 下载的zip包。
    $ unzip rmqtt-0.2.3-x86_64-unknown-linux-musl.zip -d /app/
    
    • 1
    1. 修改权限
    $ cd /app/rmqtt
    $ chmod +x bin/rmqttd
    
    • 1
    • 2
    1. 启动服务
    $ cd /app/rmqtt
    $ sh start.sh
    
    • 1
    • 2
    1. 查看服务
    $ netstat -tlnp|grep 1883
    tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN      3312/./bin/rmqttd
    tcp        0      0 0.0.0.0:11883           0.0.0.0:*               LISTEN      3312/./bin/rmqttd
    
    • 1
    • 2
    • 3

    创建static集群

    基于RAFT分布式一致性算法的集群

    1. 准备三个服务节点,将压缩包解压到程序目录,比如:/app/rmqtt
    2. 修改配置文件(rmqtt.toml)
      • 设置节点ID, node.id值设置为:1、2或3
      • 配置RPC服务端监听端口,rpc.server_addr = “0.0.0.0:5363”
      • 服务启动时同时启动"rmqtt-cluster-raft"插件
    $ cd /app/rmqtt
    $ vi etc/rmqtt.toml
    
    ##--------------------------------------------------------------------
    ## Node
    ##--------------------------------------------------------------------
    #Node id
    node.id = 1
    
    ##--------------------------------------------------------------------
    ## RPC
    ##--------------------------------------------------------------------
    rpc.server_addr = "0.0.0.0:5363"
    
    ##--------------------------------------------------------------------
    ## Plugins
    ##--------------------------------------------------------------------
    #Plug in configuration file directory
    plugins.dir = "./etc/plugins"
    #Plug in started by default, when the mqtt server is started
    plugins.default_startups = [
        "rmqtt-cluster-raft"
    #    "rmqtt-auth-http",
    #    "rmqtt-web-hook"
    ]
    
    • 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
    1. 修改插件配置
    $ vi etc/plugins/rmqtt-cluster-raft.toml
    
    ##--------------------------------------------------------------------
    ## rmqtt-cluster-raft
    ##--------------------------------------------------------------------
    
    #grpc message type
    message_type = 198
    #Node GRPC service address list
    node_grpc_addrs = ["1@10.0.2.11:5363", "2@10.0.2.12:5363", "3@10.0.2.13:5363"]
    #Raft peer address list
    raft_peer_addrs = ["1@10.0.2.11:6363", "2@10.0.2.12:6363", "3@10.0.2.13:6363"]
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. 修改权限&启动服务
    $ cd /app/rmqtt
    $ chmod +x bin/rmqttd
    $ sh start.sh
    
    • 1
    • 2
    • 3

    源码编译安装

    安装rust编译环境

    以Centos7为例,如果编译环境已经存在跳过此过程。注意:工具链需要1.56及之后版本,1.59及之后版本如果报连接错误需要升级系统开发环境。

    1. 安装 Rustup

      先打开 Rustup 的官网:https://rustup.rs ,然后根据提示下载或运行命令。

      Linux 下执行:

    $ curl https://sh.rustup.rs -sSf | sh
    
    • 1

    执行source $HOME/.cargo/env 让环境变量生效

    $ source $HOME/.cargo/env
    
    • 1
    1. 配置crate.io镜像

    可以在$HOME/.cargo/下建立一个config文件,加入如下配置:

    $ vi $HOME/.cargo/config
    
    [source.crates-io]
    registry = "https://github.com/rust-lang/crates.io-index"
    replace-with = 'tuna'
    
    [source.tuna]
    registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
    
    [source.ustc]
    registry = "git://mirrors.ustc.edu.cn/crates.io-index"
    
    [source.sjtu]
    registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"
    
    [source.rustcc]
    registry = "git://crates.rustcc.cn/crates.io-index"
    
    [net]
    git-fetch-with-cli = true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    如果tuna也太慢可以使用sjtu或ustc替换重试

    1. 安装openssl开发包

      确保已经安装了openssl的开发包,如果已经安装跳过

      For example, libssl-dev on Ubuntu or openssl-devel on Fedora.

    CentOS:

    $ yum install openssl-devel -y
    
    • 1

    Ubuntu:

    $ apt install pkg-config -y
    $ apt-get install libssl-dev -y
    
    • 1
    • 2
    编译
    1. 获取源码
    $ git clone https://github.com/rmqtt/rmqtt.git
    
    • 1
    1. 切换到最近的 Tag
    $ cd rmqtt
    $ git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
    
    • 1
    • 2
    1. 构建
    $ cargo build --release
    
    • 1
    启动RMQTT Broker
    1. 复制程序和配置文件
    $ mkdir -p /app/rmqtt/bin && mkdir -p /app/rmqtt/etc/plugins
    $ cp target/release/rmqttd /app/rmqtt/bin/
    $ cp rmqtt.toml /app/rmqtt/etc/
    $ cp rmqtt-plugins/*.toml /app/rmqtt/etc/plugins/
    $ cp rmqtt-bin/rmqtt.pem  /app/rmqtt/etc/
    $ cp rmqtt-bin/rmqtt.key  /app/rmqtt/etc/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 修改配置(rmqtt.toml)
    • 将plugins.dir = “rmqtt-plugins/” 改为 plugins.dir = “/app/rmqtt/etc/plugins”
    • 根据需要打开插件,如果启用插件,可在/etc/plugins/下修改插件配置
    • 如果需要启动TLS,可修改listener.tls.external配置
    vi /app/rmqtt/etc/rmqtt.toml
    
    ##--------------------------------------------------------------------
    ## Plugins
    ##--------------------------------------------------------------------
    #Plug in configuration file directory
    plugins.dir = "/app/rmqtt/etc/plugins"
    #Plug in started by default, when the mqtt server is started
    plugins.default_startups = [
        #    "rmqtt-cluster-raft"
        #    "rmqtt-cluster-broadcast",
        #    "rmqtt-auth-http",
        #    "rmqtt-web-hook"
    ]
    
    
    
    ##--------------------------------------------------------------------
    ## MQTT/TLS - External TLS Listener for MQTT Protocol
    listener.tls.external.addr = "0.0.0.0:8883"
    listener.tls.external.cert = "/app/rmqtt/etc/rmqtt.pem"
    listener.tls.external.key = "/app/rmqtt/etc/rmqtt.key"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    1. 启动服务
    $ cd /app/rmqtt
    ./bin/rmqttd "./etc/rmqtt.toml"
    
    • 1
    • 2
    解决编译失败问题

    如果使用1.59版及之后工具链,可能会存在依赖库版本太低导致链接失败问题,解决办法:

    • 使用1.58版工具链
    #安装1.58版本工具链
    $ rustup install 1.58
    
    #将当前工具链切换到1.58
    $ rustup default 1.58
    
    #重新编译
    $ cargo build --release
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    【Linux】生产者消费者模型
    基于PHP的玩偶玩具商城网站设计
    The Foundry Nuke 15视频后期合成和特效制作Mac软件
    Azure Synapse Analytics 性能优化指南(3)——使用具体化视图优化性能(下)
    提升组件库通用能力 - NutUI 在线主题定制功能探索
    帮助命令---学习Linux命令的第一步
    2022-6-29 最大二叉树,根据前序和后序遍历构造二叉树,将子数组重新排序得到同一个二叉查找树的方案数
    189. 轮转数组
    【开发者小技巧】6个实用的vscode快捷键
    nestjs listen EADDRINUSE: address already in use :::3000
  • 原文地址:https://blog.csdn.net/weixin_47754149/article/details/126027092