RabbitMQ 09 トピックモード

テーマモード

テーマモードの構造図:

トピック モードは実際にはファジー マッチング モードであり、ファジー マッチングの形式で転送routingKeyでき。

*または を使用して次のことを表現できます#

  • *:どんな言葉でも。
  • #:0語以上。
  1. 構成クラスを定義します。

    import org.springframework.amqp.core.Binding;
    import org.springframework.amqp.core.BindingBuilder;
    import org.springframework.amqp.core.Exchange;
    import org.springframework.amqp.core.ExchangeBuilder;
    import org.springframework.amqp.core.Queue;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * RabbitMQ配置类
     */
    @Configuration
    public class RabbitMqConfig {
        
        /**
         * 定义交换机,可以很多个
         * @return 交换机对象
         */
        @Bean
        public Exchange topicExchange(){
            return ExchangeBuilder.topicExchange("amq.topic").build();
        }
    
        /**
         * 定义消息队列
         * @return 消息队列对象
         */
        @Bean
        public Queue topicQueue(){
            return new Queue("topicQueue");
        }
        
        /**
         * 定义绑定关系
         * @return 绑定关系
         */
        @Bean
        public Binding binding(@Qualifier("topicExchange") Exchange exchange,
                               @Qualifier("topicQueue") Queue queue){
            // 将定义的交换机和队列进行绑定
            return BindingBuilder
                    // 绑定队列
                    .bind(queue)
                    // 到交换机
                    .to(exchange)
                    // 使用自定义的routingKey
                    .with("*.test.#")
                    // 不设置参数
                    .noargs();
        }
    }
  2. 消費者を定義します。

    import org.springframework.amqp.rabbit.annotation.Queue;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    /**
     * 主题队列监听器
     */
    @Component
    public class TopicListener {
    
        /**
         * 监听主题队列消息
         */
        @RabbitListener(queuesToDeclare = {@Queue("topicQueue")})
        public void receiver(String message) {
            System.out.println("主题队列接收到消息:" + message);
        }
        
    }
  3. プロデューサーを定義します。

    import org.junit.jupiter.api.Test;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class RabbitMqSpringBootTests {
    
        /**
         * RabbitTemplate封装了大量的RabbitMQ操作,已经由Starter提供,因此直接注入使用即可
         */
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        /**
         * 生产者
         */
        @Test
        void producer()  {
    
            rabbitTemplate.convertAndSend("amq.topic", "test.1", "Hello World 1");
            rabbitTemplate.convertAndSend("amq.topic", "1.test", "Hello World 2");
            rabbitTemplate.convertAndSend("amq.topic", "1.test.1", "Hello World 3");
        }
    
    }
  4. サービスを開始します。

    この時点でキューが作成されました。

    バインディング関係が設定されました。

    プロデューサーを起動してメッセージを送信します。

    ご覧のとおり、最初のメッセージの routingKey() は、test.1不一致のため*.test.#キューで受信されませんでした。

ここで使用されているデフォルトのテーマスイッチに加えて、次のamq.rabbitmq.traceようなスイッチもあります。

これは、プロデューサーとコンシューマーによるメッセージ キューの使用を記録および追跡するために使用されるスイッチです。内部スイッチであるため、ここでは説明しません。


おすすめ

転載: blog.csdn.net/qq_37770674/article/details/129980941