我正在尝试将 RabbitMq 与 Spring WebSocketMessageBroker
一起使用,跨分布式微服务。
我正在使用的设置是
在 WebSocketMessageBroker 中,我使用以下配置(取自文档):
@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/push").setAllowedOrigins("*").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableStompBrokerRelay("/queue/", "/topic/", "/app");
registry.setApplicationDestinationPrefixes("/app");
registry.setPathMatcher(new AntPathMatcher("."));
}
}
根据此配置,MyMicroservice
应发布到哪个交换/队列,以便将消息代理到 Stomp 服务?
我尝试了以下方法(在发布方面 - 在 MyMicroservice 内)
@Configuration
@SpringBootApplication
@EnableRabbit
public class Config {
public static final String WEB_QUEUE = "/topic/myNotificationTopic";
public static final String WEB_EXCHANGE = "web.exchange";
@Bean
Queue webQueue() {
return new Queue(WEB_QUEUE, false);
}
@Bean
TopicExchange webExchange() {
return new TopicExchange(WEB_EXCHANGE);
}
@Bean
Binding binding(Queue webQueue, TopicExchange webExchange) {
return BindingBuilder.bind(webQueue).to(webExchange).with(WEB_QUEUE);
}
}
@Component
public class ExamplePublisher {
@Autowired
private AmqpTemplate amqpTemplate;
public void sendMessage() {
amqpTemplate.convertAndSend(Config.WEB_QUEUE, "Hello, world");
}
}
但是,该消息似乎并未通过 WebSocket 连接进行代理。
明确地说,我的问题是:
- 这种类型的分布式配置是否受开箱即用支持?
- 根据示例配置,服务可以发布到的 RabbitMq 主题与 Stomp 消息代理代理之间有什么关系?
最佳答案
您的解释并不清楚为什么要使用 Spring AMQP 进行 STOMP 交互,尽管无论如何这是可能的。
我建议查看StompClient
支持Spring Messaging ,如果您要直接从 Java 向目标目的地发送 STOMP 消息。
使用 Spring AMQP(或只是 AMQP 协议(protocol)),您应该遵循一些 RabbitMQ STOMP 适配器 rules :
Topic Destinations
For simple topic destinations which deliver a copy of each message to all active subscribers, destinations of the form
/topic/<name>
can be used. Topic destinations support all the routing patterns of AMQP topic exchanges.Messages sent to a topic destination that has no active subscribers are simply discarded.
AMQP 0-9-1 Semantics
For
SEND
frames, the message is sent to theamq.topic
exchange with the routing key<name>
.For
SUBSCRIBE
frames, an autodeleted, non-durable queue is created and bound to theamq.topic
exchange with routing key<name>
. A subscription is created against the queue.
注意你应该有subscription
无论如何,首先,否则您的消息将在没有订阅者的情况下丢失。
关于java - 使用 RabbitMQ 和分布式微服务配置 spring WebSocketMessageBroker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34765696/