我有 spring 的那些配置和一个全功能的 stomp 代理(ActiveMQ):
@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
private static Logger LOG = org.slf4j.LoggerFactory.getLogger(WebsocketConfig.class);
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableStompBrokerRelay("/topic/", "/queue/");
config.setApplicationDestinationPrefixes("/app");
config.setUserDestinationPrefix("/user");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/socket").withSockJS();
}
}
天真地,我虽然 spring 使用了我当前的 ActiveMQ 配置,但实际上它试图连接到位于 localhost 中的具有默认 stomp 端口的服务器。我发现可以通过键入以下内容来更改此配置:
config.enableStompBrokerRelay("/topic/", "/queue/")
.setRelayHost("activeMQHOST")
.setRelayPort(9999);
很好,但目前我有两个代理的故障转移设置(master/flave 和共享文件系统)。 如何为 stomp broker 中继配置这样的设置?
如果不可能,我想到了以下解决方案:
第二个选项是可取的吗?
以下是具有两个代理的故障转移设置的示例配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
private static Logger LOG = org.slf4j.LoggerFactory.getLogger(WebsocketConfig.class);
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
StompBrokerRelayRegistration relayRegistration = config.enableStompBrokerRelay("/topic/", "/queue/");
relayRegistration.setRelayHost("tcp://activeMQHOST1:61616,tcp://activeMQHOST2:61616");
relayRegistration.setClientLogin("USERNAME");
relayRegistration.setClientPasscode("PASSWORD");
relayRegistration.setSystemLogin("USERNAME");
relayRegistration.setSystemPasscode("PASSWORD");
config.setApplicationDestinationPrefixes("/app");
config.setUserDestinationPrefix("/user");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/socket").withSockJS();
}
}
在这里您可以使用 setRelayHost 方法获取两个代理地址的逗号分隔列表,setClientLogin、setClientPasscode、setSystemLogin 和 setSystemPasscode 方法指定客户端和系统的登录凭据。