Connecting to a Broker

STOMP 代理中继维护了一条到代理的单一 “system” TCP 连接。此连接仅用于来自服务器端应用程序的消息,不用于接收消息。您可以为该连接配置 STOMP 凭据(即 STOMP 帧的 loginpasscode 标头)。这在 XML 名称空间和 Java 配置中都以 systemLoginsystemPasscode 属性公开,其默认值为 guestguest

A STOMP broker relay maintains a single “system” TCP connection to the broker. This connection is used for messages originating from the server-side application only, not for receiving messages. You can configure the STOMP credentials (that is, the STOMP frame login and passcode headers) for this connection. This is exposed in both the XML namespace and Java configuration as the systemLogin and systemPasscode properties with default values of guest and guest.

STOMP 代理中继还为每个已连接的 WebSocket 客户端创建一个单独的 TCP 连接。您可以配置用于代表客户端创建的所有 TCP 连接的 STOMP 凭据。这在 XML 名称空间和 Java 配置中都以 clientLoginclientPasscode 属性公开,其默认值为 guestguest

The STOMP broker relay also creates a separate TCP connection for every connected WebSocket client. You can configure the STOMP credentials that are used for all TCP connections created on behalf of clients. This is exposed in both the XML namespace and Java configuration as the clientLogin and clientPasscode properties with default values of guest and guest.

STOMP 代理中继始终在它代表客户端转发到代理的每个 CONNECT 帧上设置 loginpasscode 标头。因此,WebSocket 客户端不必设置这些标头。它们会被忽略。正如 Authentication 部分中所解释的,WebSocket 客户端应依赖于 HTTP 身份验证来保护 WebSocket 终端并建立客户端身份。

The STOMP broker relay always sets the login and passcode headers on every CONNECT frame that it forwards to the broker on behalf of clients. Therefore, WebSocket clients need not set those headers. They are ignored. As the Authentication section explains, WebSocket clients should instead rely on HTTP authentication to protect the WebSocket endpoint and establish the client identity.

STOMP 代理中继还通过 “system” TCP 连接向消息代理发送和接收心跳。您可以配置发送和接收心跳的时间间隔(默认情况下分别为 10 秒)。如果与代理的连接断开,代理中继将继续尝试重新连接,每 5 秒一次,直至成功为止。

The STOMP broker relay also sends and receives heartbeats to and from the message broker over the “system” TCP connection. You can configure the intervals for sending and receiving heartbeats (10 seconds each by default). If connectivity to the broker is lost, the broker relay continues to try to reconnect, every 5 seconds, until it succeeds.

任何 Spring bean 都可以实现 ApplicationListener<BrokerAvailabilityEvent> 来在与代理的 “system” 连接断开和重新建立时接收通知。例如,广播股票报价的股票报价服务可以在没有活动的 “system” 连接时停止尝试发送消息。

Any Spring bean can implement ApplicationListener<BrokerAvailabilityEvent> to receive notifications when the “system” connection to the broker is lost and re-established. For example, a Stock Quote service that broadcasts stock quotes can stop trying to send messages when there is no active “system” connection.

默认情况下,STOMP 代理中继始终连接,并在连接断开时根据需要重新连接到同一主机和端口。如果您希望在每次尝试连接时提供多个地址,您可以配置地址提供程序,而不是固定的主机和端口。以下示例展示了如何执行此操作:

By default, the STOMP broker relay always connects, and reconnects as needed if connectivity is lost, to the same host and port. If you wish to supply multiple addresses, on each attempt to connect, you can configure a supplier of addresses, instead of a fixed host and port. The following example shows how to do that:

  • Java

  • Kotlin

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

	// ...

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

	private ReactorNettyTcpClient<byte[]> createTcpClient() {
		return new ReactorNettyTcpClient<>(
				client -> client.remoteAddress(() -> new InetSocketAddress(0)),
				new StompReactorNettyCodec());
	}
}
@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfiguration : WebSocketMessageBrokerConfigurer {

	// ...

	override fun configureMessageBroker(registry: MessageBrokerRegistry) {
		registry.enableStompBrokerRelay("/queue/", "/topic/").setTcpClient(createTcpClient())
		registry.setApplicationDestinationPrefixes("/app")
	}

	private fun createTcpClient(): ReactorNettyTcpClient<ByteArray> {
		return ReactorNettyTcpClient({ it.addressSupplier { InetSocketAddress(0) } }, StompReactorNettyCodec())
	}
}

您还可以使用 virtualHost 属性配置 STOMP 代理中继。该属性的值会被设为每个 CONNECT 帧的 host 标头,并且可能很有用(例如,在实际建立 TCP 连接的主机与提供基于云的 STOMP 服务的主机不同的云环境中)。

You can also configure the STOMP broker relay with a virtualHost property. The value of this property is set as the host header of every CONNECT frame and can be useful (for example, in a cloud environment where the actual host to which the TCP connection is established differs from the host that provides the cloud-based STOMP service).