RabbitMQ消息队列(六):SpringBoot整合之通配符模式

RabbitMQ消息队列(六):SpringBoot整合之通配符模式

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

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

1. 环境准备

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

交换机&队列:

public class TopicConstants {

  public static final String TOPIC_EXCHANGE_NAME = "amq.topic";
  public static final String TV_TOPIC_QUEUE_NAME = "topic.TV";
  public static final String PHONE_TOPIC_QUEUE_NAME = "topic.TV.phone";
  public static final String PAPER_TOPIC_QUEUE_NAME = "topic.TV.phone.paper";

}

2. 生产者

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

RabbitMQRoutingConfig

@Configuration
public class RabbitMQRoutingConfig {

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

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

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

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

  //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-fanout-queue")
  public void processTV(String msg) {
    log.info("队列:TV-fanout-queue,接收msg: {} ", msg);

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

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

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

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

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

}

测试:
http://localhost:8888/topicQueue?message=topic&routeKey=topic.1
在这里插入图片描述
http://localhost:8888/topicQueue?message=topic&routeKey=topic.TV.1
在这里插入图片描述
http://localhost:8888/topicQueue?message=topic&routeKey=topic.1.1
在这里插入图片描述

4. 模式总结

  1. 简单模式 HelloWorld 一个生产者、一个消费者,不需要设置交换机(使用默认的交换机);
  2. 工作队列模式 Work Queue 一个生产者、多个消费者(竞争关系),不需要设置交换机(使用默认的交换机);
  3. 发布订阅模式 Publish/subscribe 需要设置类型为 fanout 的交换机,并且交换机和队列进行绑定,当发送消息到交换机后,交换机会将消息发送到绑定的队列;
  4. 路由模式 Routing 需要设置类型为 direct 的交换机,交换机和队列进行绑定,并且指定 routing key,当发送消息到交换机后,交换机会根据 routingkey 将消息发送到对应的队列;
  5. 通配符模式 Topic 需要设置类型为 topic 的交换机,交换机和队列进行绑定,并且指定通配符方式的 routing key,当发送消息到交换机后,交换机会根据 routing key 将消息发送到对应的队列。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值