• SpringBoot整合RabbitMQ


    生产者

    • 1.创建生产者SpringBoot工程
    • 2.引入依赖坐标
      • org.springframework.boot
      • spring-boot-starter-amqp
    • 3.编写yml配置,基本信息配置
    • 4.定义交换机,队列以及绑定关系的配置类
    • 5.注入RabbitTemplate,调用方法,完成消息发送

    1.创建生产者工程

     

     2.导入依赖

    1. org.springframework.boot
    2. spring-boot-starter
    3. org.springframework.boot
    4. spring-boot-devtools
    5. runtime
    6. true
    7. org.projectlombok
    8. lombok
    9. true
    10. org.springframework.boot
    11. spring-boot-starter-test
    12. test
    13. org.springframework.boot
    14. spring-boot-starter-amqp
    15. org.springframework.boot
    16. spring-boot-maven-plugin
    17. org.projectlombok
    18. lombok

     3.编写yml配置

    1. spring:
    2. rabbitmq:
    3. host: 43.143.246.208
    4. port: 5672
    5. username: root
    6. password: root
    7. virtual-host: /itcast

     4.定义交换机,队列以及绑定关系的配置类

    1. @Configuration
    2. public class RabbitmqConfig {
    3. public static final String EXCHANGE_NAME = "boot_topic_exchange";
    4. public static final String QUEUE_NAME = "boot_queue";
    5. //1.交换机
    6. @Bean("bootExchange")
    7. public Exchange bootExchange(){
    8. return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    9. }
    10. //2.Queue 队列
    11. @Bean("bootQueue")
    12. public Queue bootQueue(){
    13. return (Queue) QueueBuilder.durable(QUEUE_NAME).build();
    14. }
    15. //3. 队列和交互机绑定关系 Binding
    16. /*
    17. 1. 知道哪个队列
    18. 2. 知道哪个交换机
    19. 3. routing key
    20. */
    21. @Bean
    22. public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
    23. return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    24. }
    25. }

    5.注入RabbitTemplate,调用方法,完成消息发送

    1. @SpringBootTest
    2. class MqsendApplicationTests {
    3. //1.注入RabbitTemplate
    4. @Autowired
    5. private RabbitTemplate rabbitTemplate;
    6. @Test
    7. public void testSend(){
    8. rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_NAME,"boot.haha","boot mq hello~~~");
    9. }
    10. }

    6.运行结果 

     

    消费者 

    • 1.创建消费者SpringBoot工程2.引入start,依赖坐标
      • org.springframework.boot
      • spring-boot-starter-amqp
    • 3.编写yml配置,基本信息配置
    • 4.定义监听类,使用@RabbitListener注解完成队列监听。
       

    1.2.3同上 

    4.定义监听类,使用@RabbitListener注解完成队列监听

    1. @Component
    2. public class RabbimtMQListener {
    3. @RabbitListener(queues = "boot_queue")
    4. public void ListenerQueue(Message message){
    5. //System.out.println(message);
    6. System.out.println(new String(message.getBody()));
    7. }
    8. }

    启动 

     

    运行结果 

    小结

    • SpringBoot提供了快速整合RabbitMQ的方式
    • 基本信息在yml中配置,队列交互机以及绑定关系在配置类中使用Bean的方式配置
    • 生产端直接注入RabbitTemplate完成消息发送
    • 消费端直接使用@RabbitListener完成消息接收
  • 相关阅读:
    教程分享 | 如何获取港口网(全球船舶点和路径)的数据
    孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
    新一代构建工具Vite-xyphf
    uniapp开发小程序-pc端小程序下载文件
    Six Tips of Lots of Useful Phrases
    【C++】从认识using namespace std开始进入C++的学习
    java(通过反射操作对象)
    JDK 9-17 新特性介绍
    ADB☀️二、安装与测试一个APK
    1. Spring Boot 3 入门学习教程之开发第一个 Spring Boot 应用程序
  • 原文地址:https://blog.csdn.net/m0_62639288/article/details/132907556