|
对于最新稳定版本,请使用Spring Framework 7.0.1! |
外部经纪人
简单经纪人非常适合入门,但只支持部分 STOMP 命令(不支持确认、接收及其他一些功能), 依赖简单的消息发送循环,不适合进行聚类。 作为替代方案,你可以升级你的应用程序,使用功能齐全的 消息中介。
以下示例配置使得功能齐全的代理成为可能:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig 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");
}
}
以下示例展示了前例的XML配置等价物:
<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继电器消息处理器它通过将消息转发到外部消息代理来处理消息。
为此,它会与代理建立TCP连接,将所有消息转发给代理,
然后通过
WebSocket 会话。本质上,它充当一个“中继”,转发消息
双向都是。
加io.projectreactor.netty:reactor-netty和io.netty:netty-all依赖于你的项目以管理TCP连接。 |
此外,应用组件(如 HTTP 请求处理方法, 商务服务及其他服务)也可以如描述向代理中继发送消息 在发送消息时,用于向订阅的WebSocket客户端广播消息。
实际上,代理中继实现了稳健且可扩展的消息广播。