RabbitMQ (four): RabbitMq message routing

Message routing ways

  • Direct: Direct mode
  • Topic: forwarding mode
  • Fanout: Broadcast mode

1. Topic mode

The topic can use wildcards mode represented bingKey: '*' indicates a match word, '#' indicates no match or more words. Whereby a message queue may be implemented to receive a plurality of routes.

  • Create a queue: Note that the queue name is used for queue routing key
    @Bean(name = "topic_queue1")
    public Queue topic_queue_1() {
        return new Queue("topic.queue.1");
    }
    @Bean(name = "topic_queue2")
    public Queue topic_queue_2() {
        return new Queue("topic.queue.2");
    }
  • Creating TopicExchange
    @Bean
    public TopicExchange exchange() {
        return new TopicExchange("topic_exchange");
    }
  • Create a binding
    @Bean
    public Binding bindingExchangeMessage1(@Qualifier("topic_queue1") Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("topic.queue.1");
    }
    @Bean
    public Binding bindingExchangeMessage2(@Qualifier("topic_queue2") Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("topic.queue.*");
    }

When set to queue routing, with which you can specify additional routing key, then the queue will not only receive a uploaded to the data set in the beginning of the creation of routing key, you can also receive a new key bindings to upload messages.

  • Sending a message
    to specify the exchange when sending messages and routingKey
 template.convertAndSend("topic_exchange", "topic.queue.test", message + "_topic_exchange_test");

  • Receive messages: monitor queue
    @RabbitListener(queues = "topic.queue.1")
    public void process2(String message) {
        logger.info("topic.queue.1_" + message);
    }

    @RabbitListener(queues = "topic.queue.2")
    public void process3(String message) {
        logger.info("topic.queue.2_" + message);
    }

2. fanout mode

In broadcast mode, it does not matter routing key. FanoutExchange passes the message to the queue list good exchange bound up.

  • Create a queue
    @Bean(name = "fanout_queue")
    public Queue fanout_queue() {
        return new Queue("fanout.queue.1");
    }
    @Bean(name = "fanout_queue2")
    public Queue fanout_queue_2() {
        return new Queue("fanout.queue.2");
    }
    @Bean(name = "fanout_queue3")
    public Queue fanout_queue_3() {
        return new Queue("fanout.queue.3");
    }
  • Creating FanoutExchange
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanout_exchange");
    }
  • Create binding: the need to be bound to the broadcast queue up fanout exchange
    @Bean
    public Binding fanoutBindingExchangeMessage1(@Qualifier("fanout_queue") Queue queue, FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }
    @Bean
    public Binding fanoutBindingExchangeMessage2(@Qualifier("fanout_queue2") Queue queue, FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }
    @Bean
    public Binding fanoutBindingExchangeMessage3(@Qualifier("fanout_queue3") Queue queue, FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }
  • Send a message
 template.convertAndSend("fanout_exchange", "", message + "_fanout_exchange");

Reproduced in: https: //www.jianshu.com/p/ab63a6f969e5

Guess you like

Origin blog.csdn.net/weixin_33802505/article/details/91138553