• RabbitMQ常见的交换机类型


    RabbitMQ安装

    pom.xml里导入相关的依赖:

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-amqpartifactId>
    4. dependency>

    application.properties配置文件

    1. spring.rabbitmq.host=192.168.152.155
    2. spring.rabbitmq.port=5672
    3. spring.rabbitmq.virtual-host=/
    4. #开启发送端确认 生产者Publisher 到服务器Broker
    5. spring.rabbitmq.publisher-confirms=true
    6. #开启发送端消息抵达队列的确认
    7. spring.rabbitmq.publisher-returns=true
    8. #只要抵达队列,以异步发送优先回调我们这个returnConfirm
    9. spring.rabbitmq.template.mandatory=true

    Direct exchange(直通交换机)

            消息中的路由键(routing key)如果和 Binding中的binding key 一致,交换器就将消息发到对应的队列中。路由键与队列名完全匹配,如果一个队列绑定到交换机要求路由键为“dog”,则只转发routing key 标记为“dog”的消息,不会转发 “dog.puppy”,也不会转发“dog.guard” 等等。它是 完全匹配、单播的模式
      接着我们先使用下 direct exchange(直连型交换机),创建 DirectRabbitConfig.java
    1. package com.beijing.gulimall.product.rabbitmq;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.amqp.core.Binding;
    4. import org.springframework.amqp.core.DirectExchange;
    5. import org.springframework.amqp.core.Queue;
    6. import org.springframework.context.annotation.Bean;
    7. import org.springframework.context.annotation.Configuration;
    8. @Slf4j
    9. @Configuration
    10. public class DirectRabbitConfig {
    11. //创建队列
    12. @Bean
    13. public Queue TestDirectQueue() {
    14. //public Queue(String name, boolean durable, boolean exclusive, boolean autoDelete)
    15. // durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
    16. // exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
    17. // autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
    18. log.info("Queue[{}]创建成功", "TestDirectQueue");
    19. return new Queue("TestDirectQueue", true, false, false);
    20. }
    21. //创建交换机
    22. @Bean
    23. DirectExchange TestDirectExchange() {
    24. log.info("Exchange[{}]创建成功", "TestDirectExchange");
    25. return new DirectExchange("TestDirectExchange", true, false);
    26. }
    27. //创建绑定关系
    28. @Bean
    29. Binding TestBindingDirect() {
    30. // public Binding(String destination, DestinationType destinationType, String exchange, String routingKey,
    31. // Map arguments) {
    32. log.info("Binding[{}]创建成功", "TestBindingDirect");
    33. return new Binding("TestDirectQueue", Binding.DestinationType.QUEUE, "TestDirectExchange", "direct.test", null);
    34. }
    35. }

    然后写个简单的接口进行消息推送 SendMessageController.java

    1. package com.beijing.gulimall.product.rabbitmq;
    2. import com.alibaba.fastjson.JSONObject;
    3. import org.springframework.amqp.rabbit.core.RabbitTemplate;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.RestController;
    7. import java.time.LocalDateTime;
    8. import java.time.format.DateTimeFormatter;
    9. import java.util.HashMap;
    10. import java.util.Map;
    11. import java.util.UUID;
    12. @RestController
    13. public class SendMessageController {
    14. @Autowired
    15. RabbitTemplate rabbitTemplate;
    16. @RequestMapping("/hello")
    17. public void testRabbitMQ() {
    18. String messageId = String.valueOf(UUID.randomUUID());
    19. String messageData = "test message, hello!";
    20. String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    21. Map map = new HashMap<>();
    22. map.put("messageId", messageId);
    23. map.put("messageData", messageData);
    24. map.put("createTime", createTime);
    25. //将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
    26. rabbitTemplate.convertAndSend("TestDirectExchange", "direct.test", JSONObject.toJSONString(map));
    27. }
    28. }

    Fanout Exchange (扇出交换机)

    每个发到 fanout 类型交换器的消息都会分到所有绑定的队列上去。fanout 交换器不处理路由键,只是简单的将队列绑定到交换器上,每个发送到交换器的消息都会被转发到与该交换器绑定的所有队列上。很像子网 广播 ,每台子网内的主机都获得了一份复制的消息。fanout 类型转发消息是最快的。
    创建FanoutRabbitConfig.class
    1. package com.beijing.gulimall.product.rabbitmq;
    2. import org.springframework.amqp.core.Binding;
    3. import org.springframework.amqp.core.FanoutExchange;
    4. import org.springframework.amqp.core.Queue;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. @Configuration
    8. public class FanoutRabbitConfig {
    9. @Bean
    10. public FanoutExchange testFanoutExchange(){
    11. return new FanoutExchange("TestFanoutExchange",true,false);
    12. }
    13. @Bean
    14. public Queue testFanoutQueueOne(){
    15. return new Queue("TestFanoutQueueOne",true,false,false);
    16. }
    17. @Bean
    18. public Queue testFanoutQueueTwo(){
    19. return new Queue("TestFanoutQueueTwo",true,false,false);
    20. }
    21. @Bean
    22. public Binding createFanoutBindingOne(){
    23. return new Binding("TestFanoutQueueOne", Binding.DestinationType.QUEUE,"TestFanoutExchange","fanout.one231.test",null);
    24. }
    25. @Bean
    26. public Binding createFanoutBindingTwo(){
    27. return new Binding("TestFanoutQueueTwo", Binding.DestinationType.QUEUE,"TestFanoutExchange","fanout123.one.#",null);
    28. }
    29. }

    发送消息 

    1. @RequestMapping("/hello3")
    2. public void testRabbitMQ3() {
    3. String messageId = String.valueOf(UUID.randomUUID());
    4. String messageData = "test message, hello!";
    5. String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    6. Map map = new HashMap<>();
    7. map.put("messageId", messageId);
    8. map.put("messageData", messageData);
    9. map.put("createTime", createTime);
    10. //topic.one.test2
    11. //将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
    12. rabbitTemplate.convertAndSend("TestFanoutExchange","",JSONObject.toJSONString(map));
    13. }

    结果:

    主题交换机(Topic Exchange)

    topic 交换器通过模式匹配分配消息的路由键属性,将路由键和某个模式进行匹配,此时队列需要绑定到一个模式上。它将路由键和绑定键的字符串切分成单词,这些单词之间用点隔开 。它同样也
    会识别两个通配符:符号“ #” 和符号 “* ”。 # 匹配 0 个或多个单词, * 匹配一个单词。
    创建 TopicRabbitConfig.class
    1. package com.beijing.gulimall.product.rabbitmq;
    2. import org.springframework.amqp.core.Binding;
    3. import org.springframework.amqp.core.Queue;
    4. import org.springframework.amqp.core.TopicExchange;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. @Configuration
    8. public class TopicRabbitConfig {
    9. @Bean
    10. public TopicExchange testTopicExchange(){
    11. return new TopicExchange("TestTopicExchange");
    12. }
    13. @Bean
    14. public Queue testTopicQueueOne(){
    15. return new Queue("TestTopicQueueOne",true,false,false);
    16. }
    17. @Bean
    18. public Queue testTopicQueueTwo(){
    19. return new Queue("TestTopicQueueTwo",true,false,false);
    20. }
    21. @Bean
    22. public Binding createBindingOne(){
    23. return new Binding("TestTopicQueueOne", Binding.DestinationType.QUEUE,"TestTopicExchange","topic.one.test",null);
    24. }
    25. @Bean
    26. public Binding createBindingTwo(){
    27. return new Binding("TestTopicQueueTwo", Binding.DestinationType.QUEUE,"TestTopicExchange","topic.one.#",null);
    28. }
    29. }

    发送消息 

    1. @RequestMapping("/hello2")
    2. public void testRabbitMQ2() {
    3. String messageId = String.valueOf(UUID.randomUUID());
    4. String messageData = "test message, hello!";
    5. String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    6. Map map = new HashMap<>();
    7. map.put("messageId", messageId);
    8. map.put("messageData", messageData);
    9. map.put("createTime", createTime);
    10. //topic.one.test2
    11. //将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
    12. rabbitTemplate.convertAndSend("TestTopicExchange", "topic.one.test", JSONObject.toJSONString(map));
    13. }

    结果:

  • 相关阅读:
    Attention 机制是什么?
    Qt开发Android环境配置
    舒服,给Spring贡献一波源码。
    一站式企业协同研发云——云效
    面试抱佛脚
    js游戏集合
    模型UV纹理设置工具
    UML绘制
    STM32Cube +VSCode开发环境搭建
    vscode 版本比较插件 Git History Diff
  • 原文地址:https://blog.csdn.net/weixin_42383680/article/details/133747719