RabbitMQ中Direct交换机的用法

前言:比如我们的支付完成之后需要进行修改支付状态还要完成短信通知用户需要同时并发两条指令我们可以使用direct交换机进行指定两个不同的业务去完成这两件事

比如我们现在有direct.queue1/direct.queue2两个消息队列,一个direct交换机

我们创建完成两个队列之后需要将交换机进行绑定到对应的队列,绑定时我们需要将key写进去,因为要根据不同的key找到指定的队列去完成不同的消息业务

使用java操作

消费者监听代码

复制代码
    //使用direct交换机进行消息发送
    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String msg) throws InterruptedException {
        System.out.println("消费者收到DirectQueue11111111的消息:"+msg);
        Thread.sleep(20);
    }
    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String msg) throws InterruptedException {
        System.out.println("消费者收到DirectQueue2222222222222的消息:"+msg);
        Thread.sleep(200);
    }

发送者代码

复制代码
    //使用Direct交换机进行消息转发
    @Test
    void testDirect() {
        String exchangeName = "hmall.direct";
        String msg = "blue";
        rabbitTemplate.convertAndSend(exchangeName, "blue",msg);
    }
相关推荐
洛豳枭薰10 小时前
分布式事务进阶
分布式
无心水10 小时前
5、微服务快速启航:基于Pig与BladeX构建高可用分布式系统实战
服务器·分布式·后端·spring·微服务·云原生·架构
闲人编程13 小时前
Redis分布式锁实现
redis·分布式·wpf·进程··死锁·readlock
yangyanping2010815 小时前
系统监控Prometheus之监控原理和配置
分布式·架构·prometheus
之歆15 小时前
ZooKeeper 分布式协调服务完全指南
分布式·zookeeper·wpf
马猴烧酒.18 小时前
【面试八股|RabbitMQ】RabbitMQ常见面试题详解笔记
笔记·面试·rabbitmq
之歆20 小时前
DRBD 分布式复制块设备指南
分布式
时艰.21 小时前
分布式 ID 服务实战
java·分布式
黄俊懿2 天前
【架构师从入门到进阶】第一章:架构设计基础——第二节:架构设计原则
分布式·后端·中间件·架构
没有bug.的程序员2 天前
分布式配置深潜:Spring Cloud Config 与 Git 集成内核、版本回滚机制与多环境治理实战指南
java·分布式·git·spring cloud·分布式配置·版本回滚