rabbitmq工作模式详解之路由模式-routing

rabbitmq工作模式详解之路由模式-routing

严格按照消息的路由将消息发送至对应的队列,监听器监听各自的队列消费消息,可以使用direct类型交换机,使用topic类型交换机也可以实现,我这里还是使用topic,因为它功能很强大

在这里插入图片描述

使用topic实现路由模式的时候要注意使用*和#通配符带来的风险,确保消息路由的准确性

package com.gitee.small.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TopicRabbitConfig {

    //绑定键
    public final static String DOG = "topic.dog";
    public final static String CAT = "topic.cat";


    /**
     * Queue构造函数参数说明
     * new Queue(SMS_QUEUE, true);
     * 1. 队列名
     * 2. 是否持久化 true:持久化 false:不持久化
     */


    @Bean
    public Queue firstQueue() {
        return new Queue(TopicRabbitConfig.DOG);
    }

    @Bean
    public Queue secondQueue() {
        return new Queue(TopicRabbitConfig.CAT);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange("topicExchange");
    }


    /**
     * 将firstQueue和topicExchange绑定,而且绑定的键值为topic.dog
     * 这样只要是消息携带的路由键是topic.dog,才会分发到该队列
     */
    @Bean(name = "binding.dog")
    public Binding bindingExchangeMessage() {
        return BindingBuilder.bind(firstQueue()).to(exchange()).with(DOG);
    }

    /**
     * 将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.cat
     */
    @Bean(name = "binding.cat")
    public Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(exchange()).with(CAT);
    }
}

由于我之前一直是在用同一个交换机和队列演示,所以在进行路由模式演示之前要先将topic.cat队列和topic.#路由解绑

手动解绑:登录rabbitmq客户端,到exchanges标签下根据virtual host和交换机名称找到对应交换机,再找到binging信息,点击unbind手动解绑

在这里插入图片描述

rabbitAdmin.removeBinding自动解绑:

RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate);
// 怎么绑定的就怎么解绑
rabbitAdmin.removeBinding(BindingBuilder.bind(new Queue("topic.cat"))
                          .to(new TopicExchange("topicExchange")).with("topic.#"));

消息发送直接选择rabbitTemplate即可

rabbitTemplate.convertAndSend("topicExchange", "topic.dog", "路由模式测试-dog");
rabbitTemplate.convertAndSend("topicExchange", "topic.cat", "路由模式测试-cat");

监听器示例

package com.gitee.small.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RoutingRabbitReceiver {


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.dog"),
            exchange = @Exchange(value = "bindingExchangeMessage", type = ExchangeTypes.TOPIC)
    ))
    public void process(String msg) {
        log.info("dog-收到消息:{}", msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.cat"),
            exchange = @Exchange(value = "bindingExchangeMessage2", type = ExchangeTypes.TOPIC)
    ))
    public void  process2(String msg){
        log.info("cat-收到消息:{}", msg);
    }
}

结果示例

c.g.s.rabbitmq.RoutingRabbitReceiver     : dog-收到消息:路由模式测试-dog
c.g.s.rabbitmq.RoutingRabbitReceiver     : cat-收到消息:路由模式测试-cat
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值