【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 小时前
群控系统服务端开发模式-应用开发-个人资料
分布式·php·群控系统
funnyZpC7 小时前
quartz集群增强版🎉
java·分布式·开源·集群·定时任务
明达技术7 小时前
工业4.0时代下的分布式IO模块
分布式
天冬忘忧9 小时前
Spark 程序开发与提交:本地与集群模式全解析
大数据·分布式·spark
一叶飘零_sweeeet10 小时前
Dubbo 构建高效分布式服务架构
分布式·架构·dubbo
孤蓬&听雨11 小时前
RabbitMQ自动发送消息工具(自动化测试RabbitMQ)
分布式·测试工具·自动化·rabbitmq·自动发送消息
呼啦啦啦啦啦啦啦啦11 小时前
RabbitMQ 七种工作模式介绍
分布式·rabbitmq
qq_2037694911 小时前
win11安装最新rabbitmq
分布式·rabbitmq·ruby
Amagi.11 小时前
RabbitMQ的解耦、异步、削峰是什么?
分布式·rabbitmq