• SpringBoot 整合 RabbitMQ


    1. 创建 SpringBoot 工程

    由于有的 Idea 不选择插线无法创建 Spring Boot 项目,这里我们先随便选一个插件,大家也可以根据需求选择~~

    把版本改为 2.7.14

    引入这两个依赖:

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-amqpartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-testartifactId>
    8. <scope>testscope>
    9. dependency>

    配置 application.yml文件

    2. 编写代码

    生产者 :

    Config 类 : RabbitMQConfig

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

    测试类: RabbitMQConfigTests

    1. package com.lqf.rabbitmq.springbootrabbitmq;
    2. import com.lqf.rabbitmq.springbootrabbitmq.config.RabbitMQConfig;
    3. import org.junit.Test;
    4. import org.junit.runner.RunWith;
    5. import org.springframework.amqp.rabbit.core.RabbitTemplate;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.boot.test.context.SpringBootTest;
    8. import org.springframework.test.context.junit4.SpringRunner;
    9. @SpringBootTest
    10. @RunWith(SpringRunner.class)
    11. public class RabbitMQConfigTests {
    12. //1.注入RabbitTemplate
    13. @Autowired
    14. private RabbitTemplate rabbitTemplate;
    15. @Test
    16. public void testSend(){
    17. rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","boot mq hello~~~");
    18. }
    19. }

    结果

    当我们启动 测试类 之后就可以发现我们的 rabbitmq 界面里的 Exchange 里多了一个

    Queue 中多了一个消息:

    消费者:

    消费者的创建与生产者一样

    Component 类 : 

    1. package com.example.rabbitmq;
    2. import org.springframework.amqp.core.Message;
    3. import org.springframework.amqp.rabbit.annotation.RabbitListener;
    4. import org.springframework.stereotype.Component;
    5. @Component
    6. public class RabbitMQListener {
    7. @RabbitListener(queues = "boot_queue")
    8. public void ListenerQueue(Message message) {
    9. System.out.println("Message : ");
    10. System.out.println(message);
    11. }
    12. }

    结果

    启动核心类:

    之后我们就收到消息了

  • 相关阅读:
    Java连接Redis并操作Redis中的常见数据类型
    DW大学生网页作业制作设计 中华饮食文化(HTML+CSS+JavaScript) Web前端大作业
    Vue:组件缓存
    基于音频指纹的听歌识曲系统
    vue项目中如何关闭eslint
    Spring Cloud 面试题
    提升格局,淡定看待常态化波动
    从零开始Blazor Server(15)--总结
    浅理解java中的泛型
    遗留代码处理技巧与案例演示
  • 原文地址:https://blog.csdn.net/m0_64247824/article/details/132635964