• 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. }

    结果

    启动核心类:

    之后我们就收到消息了

  • 相关阅读:
    Fiora一款二次元的Web多人在线网络聊天系统
    总体最小二乘法(TLS)
    实验五 JR指令设计实验【计算机组成原理】
    chrome extension无法获取window对象
    微机原理与接口技术复习题
    动态gif图片如何操作?简单三步前年公司操作
    Spring:@Autowired @Resource @Value的区别
    (手撕)快速排序 ----->c语言实现 +图解
    Chapter 2 Gradient Descent
    【软件测试】一份合格的软件测试简历长什么样?
  • 原文地址:https://blog.csdn.net/m0_64247824/article/details/132635964