【RabbitMQ】03-交换机

1. 交换机

2. Fanout交换机

广播。生产者向exchange发消息

java 复制代码
@SpringBootTest
public class SpringAmqpTest {
    @Autowired
    public RabbitTemplate rabbitTemplate;

    @Test
    void testSimple() {
        String exchangName = "hmall.fabout";
        rabbitTemplate.convertAndSend(exchangName, null,"hello, w");
    }
}

消费者监听exchange绑定的队列即可。

java 复制代码
@Component
@Slf4j
public class SpringAmqbListener {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = "simple.queue")
    public void Lister(String msg) {
        log.info(msg);
    }

    @RabbitListener(queues = "fanout.q1")
    public void Lister1(String msg) {
        System.out.println("q1:" + msg);
    }

    @RabbitListener(queues = "fanout.q2")
    public void Lister2(String msg) {
        System.out.println("q2:" + msg);
    }
}

3. Direct 直连交换机

生产者,需要指定exchange和key

java 复制代码
@SpringBootTest
public class SpringAmqpTest {
    @Autowired
    public RabbitTemplate rabbitTemplate;

    @Test
    void testSimple() {
        String exchangName = "hmall.direct";
        rabbitTemplate.convertAndSend(exchangName, "yellow", "hello, w");
    }
}

消费者指定对列

java 复制代码
@Component
@Slf4j
public class SpringAmqbListener {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = "simple.queue")
    public void Lister(String msg) {
        log.info(msg);
    }

    @RabbitListener(queues = "direct.q1")
    public void Lister1(String msg) {
        System.out.println("q1:" + msg);
    }

    @RabbitListener(queues = "direct.q2")
    public void Lister2(String msg) {
        System.out.println("q2:" + msg);
    }

    @RabbitListener(queues = "direct.q3")
    public void Lister3(String msg) {
        System.out.println("q3" + msg);
    }
}

4. Topic交换机

生产者

java 复制代码
@SpringBootTest
public class SpringAmqpTest {
    @Autowired
    public RabbitTemplate rabbitTemplate;

    @Test
    void testSimple() {
        String exchangName = "hmall.topic";
        rabbitTemplate.convertAndSend(exchangName, "china.news", "hello, w");
    }
}

消费者

java 复制代码
    @RabbitListener(queues = "topic.q1")
    public void topicLister1(String msg) {
        System.out.println("q1:" + msg);
    }

    @RabbitListener(queues = "topic.q2")
    public void topicLister2(String msg) {
        System.out.println("q2:" + msg);
    }

5. 代码生成交换机

基于注解绑定,在消费者的@Component中写。

java 复制代码
 	@RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void topicLister1(String msg) {
        System.out.println("q1:" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void topicLister2(String msg) {
        System.out.println("q2:" + msg);
    }

6. 修改默认序列化器

  1. 依赖
java 复制代码
        <!--jackson-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
  1. 注册为Bean
java 复制代码
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
相关推荐
珠***格2 小时前
Ⅱ型边缘网关|易部署、易扩容、易改造
大数据·人工智能·分布式·能源·边缘计算
无心水2 小时前
17、本地多模态|Qwen-VL离线私有化提取敏感PDF完全指南
人工智能·分布式·架构·openclaw·hermes
Solis程序员4 小时前
分布式 SingleFlight:从单机请求合并到集群级远程调用去重
分布式
填满你的记忆4 小时前
Kafka 面试题 Top40
分布式·kafka
oqX0Cazj24 小时前
Go-Zero数据库事务实战:本地事务+失败自动回滚+生产避坑+简单分布式事务方案
数据库·分布式·golang
团象科技5 小时前
出海技术团队分布式落地调研 海外云团队协作开发实操记录
分布式
段一凡-华北理工大学5 小时前
工业领域的Hadoop架构学习~系列文章22:Hadoop生态展望 - 面向未来的技术演进
大数据·人工智能·hadoop·分布式·学习·架构·高炉炼铁
snow@li5 小时前
RabbitMQ:详解(2026版)/ 基于 AMQP 协议的消息中间件
分布式·rabbitmq
北京阿尔泰科技厂家5 小时前
长距离分布式采集的新选择——NET9770系列以太网同步数据采集卡技术应用解析
分布式·以太网·传感器·信号采集·数据采集卡·自动化控制·工业测试测量
七夜zippoe5 小时前
DolphinDB分布式计算:MapReduce模
大数据·分布式·mapreduce·dolphindb·计算