gpt4 book ai didi

java - 使用 RabbitMQ 和分布式微服务配置 spring WebSocketMessageBroker

转载 作者:行者123 更新时间:2023-11-30 08:41:58 24 4
gpt4 key购买 nike

我正在尝试跨分布式微服务将 RabbitMq 与 spring WebSocketMessageBroker 结合使用。

我正在使用的设置是

enter image description here

在 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 the amq.topic exchange with the routing key <name>.

For SUBSCRIBE frames, an autodeleted, non-durable queue is created and bound to the amq.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com