2024-05-09    2024-05-25    252 字  1 分钟

一、创建备份交换机

1、创建备份交换机

注意:备份交换机一定要选择fanout类型,因为原交换机转入备份交换机时并不会指定路由键

image-20231203231926247

image-20231202183701454

2、创建备份交换机要绑定的队列

①创建队列

image-20231202183906676

image-20231202183949674

②绑定交换机

注意:这里是要和备份交换机绑定

image-20231203232801504

3、针对备份队列创建消费端监听器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
    public static final String EXCHANGE_DIRECT_BACKUP = "exchange.direct.order.backup";
    public static final String QUEUE_NAME_BACKUP  = "queue.order.backup";

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = QUEUE_NAME_BACKUP, durable = "true"),
            exchange = @Exchange(value = EXCHANGE_DIRECT_BACKUP),
            key = {""}
    ))
    public void processMessageBackup(String dateString,
                                     Message message,
                                     Channel channel) {
        log.info("BackUp: " + dateString);
    }

二、设定备份关系

1、原交换机删除

·

image-20231202184840124

2、重新创建原交换机

image-20231202185211633

image-20231202185342087

3、原交换机重新绑定原队列

image-20231202190111581

image-20231202185955138

image-20231202190036520

三、测试

  • 启动消费者端
  • 发送消息,但是路由键不对,于是转入备份交换机