【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();
    }
相关推荐
里欧跑得慢5 小时前
Flutter 三方库 ethereum 鸿蒙分布式区块链数字资产上链钱包适配突破:接通 JSON-RPC 加密管线深入打通智能合约闭环实现高价值数字加密交互-适配鸿蒙 HarmonyOS ohos
分布式·flutter·harmonyos
2501_933329558 小时前
技术深度拆解:Infoseek舆情系统的全链路架构与核心实现
开发语言·人工智能·分布式·架构
辣机小司10 小时前
【生产级 Kafka (KRaft) 双中心容灾演练:MirrorMaker 2.0 (MM2) 核心参数配置与回切踩坑指南】
分布式·kafka·集群同步·kafka双集群
softshow102612 小时前
SpringCloud Redis与分布式
redis·分布式·spring cloud
学渣y12 小时前
git分布式版本控制系统
分布式·git·elasticsearch
天天进步201513 小时前
源码级优化:Graphiti 的并发处理与分布式记忆存储架构
人工智能·分布式·架构
BPM_宏天低代码15 小时前
宏天CRM系统的消息中心:基于RabbitMQ的实践
分布式·rabbitmq
2501_9333295516 小时前
企业级舆情监测系统技术选型指南:Infoseek AI中台架构解析与实践评估
人工智能·分布式·重构·架构
chunyublog16 小时前
HBase 2.4.18 分布式集群搭建教程(适配 Hadoop 3.3.4 + ZooKeeper 3.5.6)
hadoop·分布式·hbase
乐维_lwops1 天前
Zabbix分布式监控体系架构设计与实战优化
分布式·zabbix