RabbitMQ消息队列(五):SpringBoot整合之路由模式

RabbitMQ消息队列(五):SpringBoot整合之路由模式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-idiuVKhq-1660216266842)(E:\typora\image\image-20220811183652501.png)]

路由模式是在订阅模式的基础上加上了路由键key,交换机只分发消息到绑定在交换机上面指定路由键的队列。

1. 环境准备

参考:https://blog.csdn.net/fatestranger/article/details/126284433?spm=1001.2014.3001.5501

交换机&队列:

public class Constants {

  public static final String DIRECT_EXCHANGE_NAME = "amq.direct";
  public static final String TV_DIRECT_QUEUE_NAME = "TV-direct-queue";
  public static final String PHONE_DIRECT_QUEUE_NAME = "phone-direct-queue";
  public static final String PAPER_DIRECT_QUEUE_NAME = "paper-direct-queue";
  
}

2. 生产者

配置交换机和绑定的队列:

RabbitMQRoutingConfig

@Configuration
public class RabbitMQRoutingConfig {

  //1、声明注册direct式交换机
  @Bean
  public DirectExchange directExchange() {
    return new DirectExchange(DIRECT_EXCHANGE_NAME, true, false);
  }

  /*
  2. 声明队列
  */
  @Bean
  public Queue TVDirectQueue() {
   /*
     Queue构造函数参数说明
     1. 队列名
     2. 是否持久化 true:持久化 false:不持久化
    */
    return new Queue(TV_DIRECT_QUEUE_NAME, true);
  }

  @Bean
  public Queue phoneDirectQueue() {
   /*
     Queue构造函数参数说明
     1. 队列名
     2. 是否持久化 true:持久化 false:不持久化
    */
    return new Queue(PHONE_DIRECT_QUEUE_NAME, true);
  }

  //
  @Bean
  public Queue paperDirectQueue() {
   /*
     Queue构造函数参数说明
     1. 队列名
     2. 是否持久化 true:持久化 false:不持久化
    */
    return new Queue(PAPER_DIRECT_QUEUE_NAME, true);
  }

  //3、完成交换机和队列的绑定关系
  @Bean
  public Binding TVDirectBinding() {
    return BindingBuilder.bind(TVDirectQueue()).to(directExchange()).with("TV");
  }

  @Bean
  public Binding phoneDirectBinding() {
    return BindingBuilder.bind(phoneDirectQueue()).to(directExchange()).with("phone");
  }

  @Bean
  public Binding paperDirectBinding() {
    return BindingBuilder.bind(paperDirectQueue()).to(directExchange()).with("paper");
  }


}

  //3、完成交换机和队列的绑定关系
  @Bean
  public Binding TVDirectBinding() {
    return BindingBuilder.bind(TVFanoutQueue()).to(directExchange()).with("TV");
  }

  @Bean
  public Binding phoneDirectBinding() {
    return BindingBuilder.bind(phoneFanoutQueue()).to(directExchange()).with("phone");
  }

  @Bean
  public Binding paperDirectBinding() {
    return BindingBuilder.bind(paperFanoutQueue()).to(directExchange()).with("paper");
  }


}

DirectController

@RestController
@Slf4j
public class DirectController {

  @Autowired
  private RabbitTemplate rabbitTemplate;

  @GetMapping("/directQueue")
  public String sendMsg(@RequestParam String message, @RequestParam String routeKey) {

    log.info("message:{}, routeKey: {}", message, routeKey);

    rabbitTemplate.convertAndSend(DIRECT_EXCHANGE_NAME, routeKey, message);
    //返回消息
    return "OK!";
  }
}

3.消费者

三个消费者:

RoutingReceiver

@Component
@Slf4j
public class RoutingReceiver {

  @Autowired
  private MqReceiveService mqReceiveService;

  @RabbitListener(queues = "TV-direct-queue")
  public void processTV(String msg) {
    log.info("队列:TV-direct-queue,接收msg: {} ", msg);

    //消息消费存储
    MqReceive mqReceive = new MqReceive();
    mqReceive.setType("direct");
    mqReceive.setReceive("TV-direct-queue");
    mqReceive.setContent(msg);
    mqReceiveService.insetMsg(mqReceive);
  }

  @RabbitListener(queues = "phone-direct-queue")
  public void processPhone(String msg) {
    log.info("队列:phone-direct-queue,接收msg: {} ", msg);

    //消息消费存储
    MqReceive mqReceive = new MqReceive();
    mqReceive.setType("direct");
    mqReceive.setReceive("phone-direct-queue");
    mqReceive.setContent(msg);
    mqReceiveService.insetMsg(mqReceive);
  }

  @RabbitListener(queues = "paper-direct-queue")
  public void processPaper(String msg) {
    log.info("队列:paper-direct-queue,接收msg: {} ", msg);

    //消息消费存储
    MqReceive mqReceive = new MqReceive();
    mqReceive.setType("direct");
    mqReceive.setReceive("paper-direct-queue");
    mqReceive.setContent(msg);
    mqReceiveService.insetMsg(mqReceive);
  }

}

测试:
http://localhost:8888/directQueue?message=direct&routeKey=TV
在这里插入图片描述
http://localhost:8888/directQueue?message=direct&routeKey=phone
在这里插入图片描述
http://localhost:8888/directQueue?message=direct&routeKey=paper
在这里插入图片描述

4. 总结

Routing 模式要求队列在绑定交换机时要指定 routing key,消息会转发到符合routing key 的队列。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值