欢迎访问WDPHP素材源码!今天是2024年04月28日 星期日,熬夜对身体不好,早点休息吧!
您好,游客 [ 马上登录 | 注册帐号 | 微信登录 | QQ登录]
当前位置:首页 > 教程 > 其他教程 > 

SpringBoot整合消息队列RabbitMQ的方法是什么
栏目分类:其他教程    发布日期:2023-06-27    浏览次数:302次     收藏

本篇内容介绍了“SpringBoot整合消息队列RabbitMQ的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

    简介

    在Spring项目中,可以使用Spring-Rabbit去操作RabbitMQ

    尤其是在spring boot项目中只需要引入对应的amqp启动器依赖即可,方便的使用RabbitTemplate发送消息,使用注解接收消息。

    一般在开发过程中:

    生产者工程:

    • application.yml文件配置RabbitMQ相关信息;

    • 在生产者工程中编写配置类,用于创建交换机和队列,并进行绑定

    • 注入RabbitTemplate对象,通过RabbitTemplate对象发送消息到交换机

    消费者工程:

    • application.yml文件配置RabbitMQ相关信息

    • 创建消息处理类,用于接收队列中的消息并进行处理

    生产端

    1. 创建生产者SpringBoot工程(maven)
    2. 引入start,依赖坐标
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>

    3. 编写yml配置,基本信息配置
    4. 定义交换机,队列以及绑定关系的配置类
    5. 注入RabbitTemplate,调用方法,完成消息发送

    添加依赖

    修改pom.xml文件内容为如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
        </parent>
        <groupId>com.itheima</groupId>
        <artifactId>springboot-rabbitmq-producer</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
        </dependencies>
    </project>

    启动类

    package com.itheima.rabbitmq;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    public class ProducerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ProducerApplication.class);
        }
    }

    配置RabbitMQ

    配置文件

    创建application.yml,内容如下:

    spring:
    rabbitmq:
    host: localhost
    port: 5672
    virtual-host: /itcast
    username: heima
    password: heima

    绑定交换机和队列

    创建RabbitMQ队列与交换机绑定的配置类com.itheima.rabbitmq.config.RabbitMQConfig

    package com.itheima.rahhitmq.config;
    import org.springframework.amqp.core.*;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    @Configuration /// 配置类
    public class RabbitMQConfig {
        public static final String EXCHAGE_NAME = "boot_topic_exchange";
        public static final String QUEUE_NAME = "boot_queue";
        // 交换机
        @Bean("bootExchange")
        public Exchange bootExchange(){
            // 构建交换机对象
            return ExchangeBuilder.topicExchange(EXCHAGE_NAME).durable(true).build();
        }
        //Queue 队列
        @Bean("bootQueue")
        public Queue bootQueue(){
            return QueueBuilder.durable(QUEUE_NAME).build();
        }
        //队列和交换机的关系 Binding
        /**
         * 1 知道那个队列
         * 2 知道那个交换机
         * 3 routingKey
         */
        @Bean
        public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
            return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
        }
    }

    搭建消费者工程

    创建工程

    生产端

    1. 创建生产者SpringBoot工程

    2. 引入start,依赖坐标

    org.springframework.boot

    spring-boot-starter-amqp

    编写yml配置,基本信息配置
    定义交换机,队列以及绑定关系的配置类
    注入RabbitTemplate,调用方法,完成消息发送

    添加依赖

    修改pom.xml文件内容为如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
        </parent>
        <groupId>com.itheima</groupId>
        <artifactId>springboot-rabbitmq-consumer</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
            </dependency>
        </dependencies>
    </project>

    启动类

    package com.itheima.rabbitmq;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    public class ConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class);
        }
    }

    配置RabbitMQ

    创建application.yml,内容如下:

    spring:
    rabbitmq:
    host: localhost
    port: 5672
    virtual-host: /itcast
    username: heima
    password: heima

    消息监听处理类

    编写消息监听器com.itheima.rabbitmq.listener.MyListener

    package com.itheima.rabbitmq.listener;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    @Component
    public class MyListener {
        /**
         * 监听某个队列的消息
         * @param message 接收到的消息
         */
        @RabbitListener(queues = "item_queue")
        public void myListener1(String message){
            System.out.println("消费者接收到的消息为:" + message);
        }
    }

    测试

    在生产者工程springboot-rabbitmq-producer中创建测试类,发送消息:

    package com.itheima.rabbitmq;
    import com.itheima.rabbitmq.config.RabbitMQConfig;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class RabbitMQTest {
        @Autowired
        private RabbitTemplate rabbitTemplate;
        @Test
        public void test(){
            rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.insert", "商品新增,routing key 为item.insert");
            rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.update", "商品修改,routing key 为item.update");
            rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.delete", "商品删除,routing key 为item.delete");
        }
    }

    先运行上述测试程序(交换机和队列才能先被声明和绑定),然后启动消费者;在消费者工程springboot-rabbitmq-consumer中控制台查看是否接收到对应消息。

    SpringBoot提供了快速整合RabbitMQ的方式

    基本信息再yml中配置,队列交互机以及绑定关系在配置类中使用Bean的方式配置

    生产端直接注入RabbitTemplate完成消息发送

    消费端直接使用@RabbitListener完成消息接收

    源码 模板 特效 素材 资源 教程 站长