Modo tema RabbitMQ 09

modo tema

Diagrama de estructura del modo temático:

El modo de tema es en realidad un modo de coincidencia difusa, que routingKeyse reenviar en forma de coincidencia difusa.

Puedes usar *o #para expresar:

  • *: Cualquier palabra.
  • #: 0 o más palabras.
  1. Definir clases de configuración.

    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. Definir consumidores.

    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. Definir productor.

    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. Inicie el servicio.

    En este punto se ha creado la cola.

    La relación vinculante ha sido establecida.

    Inicie el productor y envíe mensajes.

    Como puede ver, la cola no recibió la clave de enrutamiento () del primer mensaje test.1debido a una falta de coincidencia .*.test.#

Además del cambio de tema predeterminado que se usa aquí, también hay un amq.rabbitmq.tracecambio llamado:

Este es un conmutador que se utiliza para ayudar a registrar y rastrear el uso de colas de mensajes por parte de productores y consumidores. Es un conmutador interno y no se demostrará aquí.


Supongo que te gusta

Origin blog.csdn.net/qq_37770674/article/details/129980941
Recomendado
Clasificación