克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Stuart

介绍

Stuart 是一个基于 Eclipse Vert.xApache Ignite 实现的 MQTT 协议服务器,具备以下特性:

  1. 支持 MQTT 3.1 和 MQTT 3.1.1
  2. 支持 QoS0,QoS1 和 QoS2
  3. 支持订阅包含通配符(+/#)的主题
  4. 支持 transient session 和 persistent session
  5. 支持 persistent session 的消息持久化
  6. 支持 retained 和 will 消息
  7. 支持服务器集群
  8. 支持基于用户名/密码的连接认证管理
  9. 支持基于用户名、IP地址和主题的访问控制列表
  10. 支持 Web 管理界面
  11. 支持 MQTT over WebSocket

安装教程

  1. 运行环境
    JDK:Oracle JDK 8 或 OpenJDK 8
    操作系统:Linux、Mac OS X(10.6以上)、Windows(XP以上)、Windows Server(2008以上)
  2. 下载 release 版本,使用如下命令直接运行:java -jar stuart-0.1.0-fat.jar
    运行后会在 stuart-0.1.0-fat.jar 文件的同级目录下创建 storage 和 log 两个目录,在未使用 --cfg 指定配置文件时,Stuart 将使用 jar 包中自带的 cfg.properties 进行启动
    如果想查看启动命令的具体参数,可以使用如下命令:java -jar stuart-0.1.0-fat.jar -h
  3. 通过 maven 编译,使用 git clone https://gitee.com/x87/stuart.gitgit clone https://github.com/xyangwang/stuart.git 命令,或者在 Stuart 项目页面直接下载源码,使用自己熟悉的 IDE 导入工程进行二次开发(需安装 lombok 插件),并使用 mvn install 命令编译打包。由于新增的 MQTT over WebSocket 功能修改了依赖包 vertx-mqtt 中的代码,如果需要自己编译,请同时到 https://gitee.com/x87/vertx-mqtt.githttps://github.com/xyangwang/vertx-mqtt.git 下载修改的依赖包,并 checkout 3.6 分支代码
  4. 成功启动后,就可以使用 mosquitto_submosquitto_pub 命令或者其他 Client Library 进行开发测试

系统架构

  1. 系统存储

系统使用 Apache Ignite 原生持久化功能对信息进行存储
存储包括:节点信息、监听器信息、连接信息、会话信息、路由信息、Will 消息、Retain 消息、管理员信息、连接用户信息和访问控制信息。
节点信息、监听器信息和连接信息使用纯内存模式存储,其他信息采用持久化模式存储。

路由信息包括两部分:
A. topic 到 node 与 client 的映射关系;
B. wildcard topic 的前缀树结构。

针对持久化和非持久化两种 Session,分别采用两套不同的存储结构:
A. 对于非持久化 Session,基于这类 Session 的消息不需要持久化,所以使用 On-Heap 内存对消息进行存储;
B. 对于持久化 Session, 则使用 Apache Ignite 的 IgniteCache 和 IgniteQueue 进行存储。

  1. 节点架构

系统主要分成五大服务模块:
A. Cache Service:为其他模块提供持久化服务;
B. Auth Service:提供连接用户的认证,订阅/发布的访问控制,以及针对 local 模式下相关信息的管理配置;
C. Session Service:管理连接到服务的 Session;
D. Verticle Service:管理各类 Verticle 和 Service,Verticle 是各 Listener 的具体实现,为各类客户端提供连接访问;
E. Metrics Service:针对 MQTT 协议的包/消息数/消息有效载荷字节数,以及 Session 运行信息的收集和统计。
备注:由于 Restful API 暂时没有实现,所以上图中这部分用虚线标识。

  1. 集群架构

Stuart 目前可以使用两种方式组建集群:
A. 静态 IP 列表;
B. ZooKeeper 服务发现。

系统先通过上述方式将 Apache Ignite 组成集群,然后再使用 Eclipse Vert.x 通过 Apache Ignite 组成集群,这样就可以使用 Eclipse Vert.x 提供的高效的 Event Bus 在节点间进行通讯。

配置文件

# instance configuration
# 服务器实例唯一标识
instance.id=stuart@127.0.0.1
# 服务器启动监听地址
instance.listen-address=0.0.0.0
# 服务器运行统计信息更新时间间隔
instance.metrics-period-ms=60000
# 存储路径
instance.storage.dir=./storage
# 存储写同步模式,包括:primary_sync 和 full_sync
instance.storage-write-sync-mode=primary_sync
# 预写日志模式,包括:fsync、log_only 和 background
instance.storage.wal-mode=log_only
# background 预写日志模式下缓冲区刷新到磁盘的频率
instance.storage.wal-flush-frequency-ms=2000
# 日志路径
instance.log.dir=./log
# 日志级别
instance.log.level=info

# port configuration
# MQTT 端口
mqtt.port=1883
# SSL MQTT 端口
mqtt.ssl-port=8883
# MQTT over WebSocket 端口
websocket.port=8080
# MQTT over WebSocket path
websocket.path=/mqtt
# MQTT over SSL WebSocket 端口
websocket.ssl-port=8083
# MQTT over SSL WebSocket path
websocket.ssl-path=/mqtt
# Web 端口
http.port=18083

# max connections configuration
# MQTT 最大连接数
mqtt.max-connections=102400
# SSL MQTT 最大连接数
mqtt.ssl-max-connections=1024
# MQTT over WebSocket 最大连接数
websocket.max-connections=64
# MQTT over SSL WebSocket 最大连接数
websocket.ssl-max-connections=64

# mqtt configuration
# mqtt packet max size (fixed header + variable header + payload)
# 客户端 ID 最大长度
mqtt.client.max-len=1024
# 客户端连接超时时间
mqtt.client.connect-timeout-s=30
# 客户端限制超时时间
mqtt.client.idle-timeout-s=30
# MQTT Packet 最大限制(fixed header + variable header + payload)
mqtt.packet.max-size-kb=64
# 保存 retained 消息的最大数量
mqtt.retain.max-capacity=1000000
# retained 消息 payload 最大限制
mqtt.retain.max-payload-kb=64
# retained 消息过期时间,`0`标识没有过期限制
mqtt.retain.expiry-interval-s=0
# 是否开启 SSL
mqtt.ssl-enable=false
# SSL Key 路径
mqtt.ssl-key-path=./server-key.pem
# SSL Cert 路径
mqtt.ssl-cert-path=./server-cert.pem
# 是否开启节点和会话的监控
mqtt.metrics-enable=true

# session configuration
# 发送消息时,是否对 QoS 进行升级处理,默认做降级处理,即消息与主题两者的 QoS 取较小者
session.upgrade-qos=false
# 服务器端会话等待 QoS2 消息 PUBREL 指令的最大容量
session.await-rel.max-capacity=1000
# 服务器端会话等待 QoS2 消息 PUBREL 指令的过期时间
session.await-rel.expiry-interval-s=20
# 服务器端会话队列最大容量
session.queue.max-capacity=1000
# 服务器端会话队列是否存储 QoS0 消息
session.queue.store-qos0=false
# 服务器端会话飞行窗口最大容量
session.inflight.max-capacity=32
# 服务器端会话飞行窗口中消息的过期时间
session.inflight.expiry-interval-s=20
# 服务器端会话飞行窗口中消息的重试次数
session.inflight.max-retries=3

# authentication and authorization basic configuration
# system use md5(auth.aes-key) value as AES Algorithm's Key
# 连接用户加解密秘钥
auth.aes-key=1234567890
# 是否允许匿名链接访问
auth.allow-anonymous=false
# 是否允许不匹配访问控制列表的访问
auth.acl-allow-nomatch=true
# 认证鉴权方式,包括:local、redis、mysql、postgre 和 mongo
# 只有在 local 模式下,才可以使用 Web 管理界面中的“连接用户”和“访问控制”界面
auth.mode=local

# authentication and authorization redis configuration
auth.redis.host=127.0.0.1
auth.redis.port=6379
auth.redis.pass=
auth.redis.select=0
auth.redis.user-key-prefix=stuart:auth_user:
auth.redis.passwd-field=password
auth.redis.acl.user-key-prefix=stuart:acl_user:
auth.redis.acl.ipaddr-key-prefix=stuart:acl_ipaddr:
auth.redis.acl.client-key-prefix=stuart:acl_client:
auth.redis.acl.all-key-prefix=stuart:acl_all:

# authentication and authorization mysql/postgresql server configuration
auth.rdb.host=127.0.0.1
auth.rdb.port=3306
auth.rdb.username=root
auth.rdb.password=123456
auth.rdb.database=stuart
auth.rdb.charset=UTF-8
auth.rdb.max-pool-size=10
auth.rdb.query-timeout-ms=10000

# authentication and authorization mongodb configuration
auth.mongo.host=127.0.0.1
auth.mongo.port=27017
auth.mongo.db-name=stuart
auth.mongo.username=root
auth.mongo.password=123456
auth.mongo.auth-source=stuart
auth.mongo.auth-mechanism=SCRAM-SHA-1
auth.mongo.max-pool-size=100
auth.mongo.min-pool-size=10
auth.mongo.max-idle-time-ms=0
auth.mongo.max-life-time-ms=0
auth.mongo.wait-queue-multiple=500
auth.mongo.wait-queue-timeout-ms=120000
auth.mongo.maintenance-frequency-ms=1000
auth.mongo.maintenance-initial-delay-ms=0
auth.mongo.connect-timeout-ms=10000
auth.mongo.socket-timeout-ms=0
auth.mongo.user=stuart_user
auth.mongo.user.username-field=username
auth.mongo.user.password-field=password
auth.mongo.acl=stuart_acl
auth.mongo.acl.target-field=target
auth.mongo.acl.type-field=type
auth.mongo.acl.seq-field=seq
auth.mongo.acl.topics-field=topics
auth.mongo.acl.topic-field=topic
auth.mongo.acl.authority-field=authority

# vert.x configuration
# 允许多个启动多个 MQTT Verticle
vertx.multi-instances-enable=false
# MQTT Verticle 数量
vertx.multi-instances=1
# 工作线程池大小
vertx.worker-pool-size=2
# 开启文件缓存,提高 Web 管理界面访问速度,建议在开发时设置为 false
vertx.file-caching.enabled=true
# Web 管理界面 Session 超时时间
vertx.http.session-timeout-ms=3600000

# cluster configuration
# cluster storage backups(if mode is standalone, backups = 0)
# 集群模式,包括:standalone、vmip 和 zookeeper
cluster.mode=standalone
# 集群持久化备份数量
cluster.storage-backups=2
# 集群拓扑重新平衡触发时间间隔
cluster.blt-rebalance-time-ms=300000

# cluster vmip configuration
# 静态 IP 地址列表
vmip.addresses=127.0.0.1:47500..47509

# cluster zookeeper configuration
# session timeout must be bigger than zookeeper tickTime * syncLimit
zookeeper.connect-string=127.0.0.1:2181
zookeeper.root-path=/stuart
zookeeper.join-timeout-ms=10000
zookeeper.session-timeout-ms=20000
zookeeper.reconnect.enable=true

备注:关于 Apache Ignite 的WAL模式可参考这里

Roadmap

  1. 针对 transient session 的 Off-Heap 优化
  2. Restful API
  3. Unit test
  4. Benchmark
  5. Remove JQuery
  6. Remember me
  7. ...

Web 管理界面

  1. 在成功启动后,可以使用 Stuart 提供的 Web 管理界面对服务器进行监控和管理
  2. 管理界面访问地址 http://127.0.0.1:18083
  3. 初始化用户名/密码:admin/stuart
  4. 部分管理界面如下所示: 登录界面 控制台 连接 会话 主题 订阅 连接用户 访问控制 监听器 WebSocket 系统管理员
  5. 备注:由于 Restful API 功能暂未开发,所以这部分只是静态界面

License

Copyright 2019 Yang Wang

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

Stuart 是一个基于 Eclipse Vert.x 和 Apache Ignite 实现的 MQTT 协议服务器。 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化