跳到主要内容
版本: 4.1

RabbitMQ Web STOMP 插件

概述

Web STOMP 插件使得通过 WebSocket 连接使用 STOMP 成为可能。

此插件的目标是在 Web 应用程序中启用 STOMP 消息传递。

一个类似的插件,Web MQTT 插件,使得通过 WebSockets 使用 MQTT 成为可能。

工作原理

RabbitMQ Web STOMP 插件是 RabbitMQ STOMP 插件 提供的 STOMP 协议实现和 WebSocket 客户端之间的一个极简的“桥梁”。

RabbitMQ Web STOMP 与 RabbitMQ STOMP 插件完全兼容。

启用插件

rabbitmq_web_stomp 插件随 RabbitMQ 一起发布。

要启用插件,请运行 rabbitmq-plugins

rabbitmq-plugins enable rabbitmq_web_stomp

用法

为了在 Web 浏览器环境中使用 STOMP,需要一个 JavaScript STOMP 库。我们已经测试了由 Jeff MesnilJeff Lindsay 开发的 stomp-websocket 库。这个库 包含在 RabbitMQ Web STOMP 示例 中。

WebSocket 端点在 /ws 路径上可用

ws://127.0.0.1:15674/ws

此端点仅适用于支持 WebSocket 的客户端。请注意,需要进行一些配置才能接受二进制消息。

为了使用 WebSocket 从浏览器建立连接,你可以使用如下代码

<!-- include the client library -->
<script src="stomp.js"></script>
<script>
var ws = new WebSocket('ws://127.0.0.1:15674/ws');
var client = Stomp.over(ws);
// ...

一旦你有了 client 对象,你就可以遵循 stomp.js 库公开的 API。下一步通常是与 broker 建立 STOMP 连接

// ...
var on_connect = function() {
console.log('connected');
};
var on_error = function() {
console.log('error');
};
client.connect('guest', 'guest', on_connect, on_error, '/');
// ...

Web STOMP 示例

一些简单的 Web STOMP 示例作为 RabbitMQ Web STOMP 示例 插件提供。要使其运行,请按照该插件的安装说明进行操作并启用该插件

rabbitmq-plugins enable rabbitmq_web_stomp_examples

这些示例将在 http://127.0.0.1:15670/ url 下可用。你将看到两个示例

  • "echo" - 展示如何使用 STOMP 进行简单的消息广播
  • "bunny" - 一个简单的协作画布绘画应用的示例

我们鼓励你查看源代码

配置

当未指定任何配置时,Web STOMP 插件将监听所有接口上的 15674 端口,并具有默认的用户登录名/密码 guest/guest。请注意,默认情况下,此用户仅允许从本地主机连接。我们强烈建议为生产系统创建一个单独的用户。

要更改监听端口,请编辑你的高级配置文件,使其包含一个 tcp_config 部分,其中包含 rabbitmq_web_stomp 应用程序的 port 变量。

例如,将监听端口更改为 12345 的完整配置文件如下所示

web_stomp.tcp.port = 12345

你可以使用 tcp_config 部分来指定你需要的任何 TCP 选项。有关接受的参数的详细信息,请参阅RabbitMQ 网络指南Ranch 文档

TLS (WSS)

该插件支持带有 TLS (WSS) 连接的 WebSockets。请参阅 TLS 指南 以了解有关 RabbitMQ 中 TLS 支持的更多信息。

TLS 配置参数在 web_stomp.ssl 部分中提供

web_stomp.ssl.port       = 15673
web_stomp.ssl.backlog = 1024
web_stomp.ssl.cacertfile = /path/to/ca_certificate.pem
web_stomp.ssl.certfile = /path/to/server_certificate.pem
web_stomp.ssl.keyfile = /path/to/server_key.pem
web_stomp.ssl.password = changeme

TLS 监听端口、服务器证书文件、私钥和 CA 证书捆绑包是强制选项。如果私钥使用密码,则密码也是强制性的。TLS 设置的扩展列表与核心服务器的设置基本相同。此插件接受的完整选项列表可以在 Ranch 文档 中找到。

启用的 TLS 版本和密码套件

可以配置 RabbitMQ 将使用的 TLS 版本和密码套件。请注意,并非所有套件都将在所有系统上可用。

RabbitMQ TLS 指南有 关于 TLS 版本的章节关于密码套件的章节。下面是在 高级配置格式 中的一个示例,该示例配置了密码套件以及插件的其他一些 TLS 选项

web_stomp.ssl.port       = 15673
web_stomp.ssl.backlog = 1024
web_stomp.ssl.certfile = /path/to/server_certificate.pem
web_stomp.ssl.keyfile = /path/to/server_key.pem
web_stomp.ssl.cacertfile = /path/to/ca_certificate_bundle.pem
web_stomp.ssl.password = changeme

web_stomp.ssl.honor_cipher_order = true
web_stomp.ssl.honor_ecc_order = true
web_stomp.ssl.client_renegotiation = false
web_stomp.ssl.secure_renegotiate = true

web_stomp.ssl.versions.1 = tlsv1.2
web_stomp.ssl.versions.2 = tlsv1.1
web_stomp.ssl.ciphers.1 = ECDHE-ECDSA-AES256-GCM-SHA384
web_stomp.ssl.ciphers.2 = ECDHE-RSA-AES256-GCM-SHA384
web_stomp.ssl.ciphers.3 = ECDHE-ECDSA-AES256-SHA384
web_stomp.ssl.ciphers.4 = ECDHE-RSA-AES256-SHA384
web_stomp.ssl.ciphers.5 = ECDH-ECDSA-AES256-GCM-SHA384
web_stomp.ssl.ciphers.6 = ECDH-RSA-AES256-GCM-SHA384
web_stomp.ssl.ciphers.7 = ECDH-ECDSA-AES256-SHA384
web_stomp.ssl.ciphers.8 = ECDH-RSA-AES256-SHA384
web_stomp.ssl.ciphers.9 = DHE-RSA-AES256-GCM-SHA384

TLS (WSS) 故障排除

有关更多信息,请参阅 RabbitMQ TLSTLS 故障排除

基本 HTTP 身份验证

use_http_auth 选项通过允许客户端在 HTTP 授权标头中发送登录名和密码(使用 HTTP Basic Auth)来扩展身份验证。如果存在,将使用这些凭据。否则,将使用默认的 STOMP 凭据。CONNECT 帧中找到的凭据(如果有)将被忽略。

这是一个高级功能,仅通过高级配置文件经典配置格式公开

[
{rabbitmq_web_stomp,
[{use_http_auth, true}]}
].

代理协议

Web STOMP 插件支持 代理协议。此功能默认情况下处于禁用状态,要为客户端激活它

web_stomp.proxy_protocol = true

有关代理协议的更多信息,请参阅网络指南

高级选项

Web STOMP 插件在底层使用 Cowboy HTTP 和 WebSocket 服务器。Cowboy 提供了许多选项,可用于自定义服务器在 WebSocket 连接处理方面的行为。

一些设置是通用的 HTTP 设置,另一些是特定于 WebSockets 的。

内容编码

默认情况下,Web STOMP 插件将期望处理编码为 UTF-8 的消息。如果需要,可以使用 ws_frame 选项将此插件公开的 WebSocket 端点切换到二进制模式

web_stomp.ws_frame = binary

HTTP 选项

可以使用 web_stomp.cowboy_opts.* 键指定通用 HTTP 服务器设置,例如

# connection inactivity timeout
web_stomp.cowboy_opts.idle_timeout = 60000
# max number of pending requests allowed on a connection
web_stomp.cowboy_opts.max_keepalive = 200
# max number of headers in a request
web_stomp.cowboy_opts.max_headers = 100
# max number of empty lines before request body
web_stomp.cowboy_opts.max_empty_lines = 5
# max request line length allowed in requests
web_stomp.cowboy_opts.max_request_line_length

WebSocket 选项

# WebSocket traffic compression is enabled by default
web_stomp.ws_opts.compress = true

# WebSocket connection inactivity timeout
web_stomp.ws_opts.idle_timeout = 60000

web_stomp.ws_opts.max_frame_size = 50000
© . All rights reserved.