【问题标题】:Configuring spring WebSocketMessageBroker with RabbitMQ and distributed microservices使用 RabbitMQ 和分布式微服务配置 spring WebSocketMessageBroker
【发布时间】:2016-01-13 11:37:16
【问题描述】:

我正在尝试将 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 消息代理代理之间的关系是什么?

【问题讨论】:

    标签: java spring spring-integration spring-websocket


    【解决方案1】:

    您的解释不清楚为什么要使用 Spring AMQP 进行 STOMP 交互,尽管无论如何都是可能的。

    如果您要直接从 Java 将 STOMP 消息发送到目标目的地,我建议您查看 Spring Messaging 中的 StompClient 支持。

    使用 Spring AMQP(或只是 AMQP 协议),您应该遵循一些 RabbitMQ STOMP Adapter rules

    主题目的地

    对于将每条消息的副本传递给所有活动订阅者的简单主题目标,可以使用/topic/<name> 形式的目标。主题目的地支持 AMQP 主题交换的所有路由模式。

    发送到没有活动订阅者的主题目的地的消息将被简单地丢弃。

    AMQP 0-9-1 语义

    对于SEND 帧,消息被发送到amq.topic 交换,路由键为<name>

    对于SUBSCRIBE 帧,会创建一个自动删除的非持久队列,并使用路由键<name> 绑定到amq.topic 交换。针对队列创建订阅。

    请注意,无论如何您都应该先拥有subscription,否则您的消息将在没有订阅者的情况下丢失。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-08
      • 2015-09-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2019-04-14
      • 2020-02-10
      • 2016-12-20
      相关资源
      最近更新 更多