External Broker

简单代理对于上手来说很棒,但只支持部分 STOMP 命令(它不支持确认、收条和一些其他功能),依赖于一个简单的消息发送循环,并且不适合集群。作为替代方案,你可以升级你的应用程序来使用全功能消息代理。

The simple broker is great for getting started but supports only a subset of STOMP commands (it does not support acks, receipts, and some other features), relies on a simple message-sending loop, and is not suitable for clustering. As an alternative, you can upgrade your applications to use a full-featured message broker.

请参阅你所选择的留言代理的 STOMP 文档(例如 RabbitMQActiveMQ等),安装代理,并通过启用了 STOMP 支持的方式运行它。然后,你可以在 Spring 配置中启用 STOMP 代理中继(而不是简单中继)。

See the STOMP documentation for your message broker of choice (such as RabbitMQ, ActiveMQ, and others), install the broker, and run it with STOMP support enabled. Then you can enable the STOMP broker relay (instead of the simple broker) in the Spring configuration.

以下示例配置启用了一个全功能代理:

The following example configuration enables a full-featured broker:

  • Java

  • Kotlin

  • Xml

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

	@Override
	public void registerStompEndpoints(StompEndpointRegistry registry) {
		registry.addEndpoint("/portfolio").withSockJS();
	}

	@Override
	public void configureMessageBroker(MessageBrokerRegistry registry) {
		registry.enableStompBrokerRelay("/topic", "/queue");
		registry.setApplicationDestinationPrefixes("/app");
	}

}
@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfiguration : WebSocketMessageBrokerConfigurer {

	override fun registerStompEndpoints(registry: StompEndpointRegistry) {
		registry.addEndpoint("/portfolio").withSockJS()
	}

	override fun configureMessageBroker(registry: MessageBrokerRegistry) {
		registry.enableStompBrokerRelay("/topic", "/queue")
		registry.setApplicationDestinationPrefixes("/app")
	}
}
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:websocket="http://www.springframework.org/schema/websocket"
	   xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/websocket
		https://www.springframework.org/schema/websocket/spring-websocket.xsd">

	<websocket:message-broker application-destination-prefix="/app">
		<websocket:stomp-endpoint path="/portfolio">
			<websocket:sockjs />
		</websocket:stomp-endpoint>
		<websocket:stomp-broker-relay prefix="/topic,/queue" />
	</websocket:message-broker>
</beans>

上述配置中的 STOMP 代理中继是 Spring MessageHandler,它通过将消息转发到外部消息代理的方式处理消息。为此,它会建立到代理的 TCP 连接,将所有消息转发到代理,然后通过 WebSocket 会话将从代理接收的所有消息转发到客户端。从本质上讲,它充当一个双向转发消息的 “relay”。

The STOMP broker relay in the preceding configuration is a Spring MessageHandler that handles messages by forwarding them to an external message broker. To do so, it establishes TCP connections to the broker, forwards all messages to it, and then forwards all messages received from the broker to clients through their WebSocket sessions. Essentially, it acts as a “relay” that forwards messages in both directions.

io.projectreactor.netty:reactor-nettyio.netty:netty-all 依赖项添加到您的项目中以进行 TCP 连接管理。

Add io.projectreactor.netty:reactor-netty and io.netty:netty-all dependencies to your project for TCP connection management.

此外,应用程序组件(例如 HTTP 请求处理方法、业务服务等)也可以像 Sending Messages中所述将消息发送到代理中继,以向已订阅的 WebSocket 客户端广播消息。

Furthermore, application components (such as HTTP request handling methods, business services, and others) can also send messages to the broker relay, as described in Sending Messages, to broadcast messages to subscribed WebSocket clients.

实际上,代理转发器实现了强大且可扩展的消息广播。

In effect, the broker relay enables robust and scalable message broadcasting.