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

标签 java spring spring-integration spring-websocket

我正在尝试将 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/

相关文章:

java - 为什么 do-while 循环在这个 Java 程序中没有按预期运行?

java - 在 Java 中将 ArrayList 作为泛型参数传递

java - 制作 Spring Bean 列表

java - 是否可以为 JLabel 中的 ImageIcon 制作选择工具?

java - 使用 IN 子句对 arraylist 变量进行 SQL 查询

java - 在哪里可以找到 Spring 3 IoC JavaConfig(注释)教程?

java - 环境之间的时区差异

java - Spring:解码 XML 字符串时出现 OXM 错误(castor)

apache-kafka - Spring 集成和 Kafka : How to filter messages based on message header

spring - 如何将 “new line”放入Spring表达式语言中?