【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();
    }
相关推荐
亲爱的非洲野猪24 分钟前
Kafka消息积压的多维度解决方案:超越简单扩容的完整策略
java·分布式·中间件·kafka
活跃家族32 分钟前
分布式压测
分布式
前端世界2 小时前
HarmonyOS开发实战:鸿蒙分布式生态构建与多设备协同发布全流程详解
分布式·华为·harmonyos
DavidSoCool3 小时前
RabbitMQ使用topic Exchange实现微服务分组订阅
分布式·微服务·rabbitmq
掘金-我是哪吒4 小时前
分布式微服务系统架构第158集:JavaPlus技术文档平台日更-JVM基础知识
jvm·分布式·微服务·架构·系统架构
东窗西篱梦4 小时前
Redis集群部署指南:高可用与分布式实践
数据库·redis·分布式
Acrel_Fanny4 小时前
Acrel-1000系列分布式光伏监控系统在湖北荆门一马光彩大市场屋顶光伏发电项目中应用
分布式
xufwind4 小时前
spark standlone 集群离线安装
大数据·分布式·spark
半新半旧5 小时前
Redis集群和 zookeeper 实现分布式锁的优势和劣势
redis·分布式·zookeeper
亲爱的非洲野猪6 小时前
Kafka “假死“现象深度解析与解决方案
分布式·kafka