• SpringBoot整合RabbitMQ(最新笔记)


    SpringBoot整合RabbitMQ

    1.生产者SpringBootProducer

    1.2 创建工程并导入依赖

    我们使用的springboot版本为2.5.6,其他都是根据spring-boot-starter-parent自动选择版本

    引入以下工程即可

    • spring-boot-starter-test 用于测试
    • junit 用于单元测试
    • spring-boot-starter-amqp SpringBoot和RabbitMQ的整合方案
    
    <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.0modelVersion>
    
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.5.6version>
            <relativePath/>
        parent>
    
        <artifactId>springboot-producerartifactId>
        <version>1.0-SNAPSHOTversion>
    
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-amqpartifactId>
            dependency>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    1.2 创建配置文件并配置

    SpringBoot配置文件名称为application.yml

    需要配置的内容如下:

    # 配置RabbitMQ的基本信息
    spring:
      rabbitmq:
        # 地址
        host: 192.168.52.128
        # 端口
        port: 5672
        # 用户名
        username: admin
        # 密码
        password: admin
        # 虚拟机
        virtual-host: /test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1.3 创建项目启动类

    @SpringBootApplication
    public class ProducerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ProducerApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    1.4 创建RabbitMQ配置类

    @Configuration
    public class RabbitMQConfig {
    	// 配置代码都写在这里
    }
    
    • 1
    • 2
    • 3
    • 4

    (1)设置默认的交换机的名称和队列名称

    /**
     * 默认测试的交换机机名称
     * springboot_topic_exchange
     */
    public static final String EXCHANGE_NAME = "springboot_topic_exchange";
    
    /**
     * 默认的队列名称
     * springboot_root_queue
     */
    public static final String QUEUE_NAME = "springboot_root_queue";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    (2)创建通配符类型的交换机

    /**
     * 创建交换机
     *
     * @return 交换机
     */
    @Bean("bootExchange")
    public Exchange bootExchange() {
        // 创建一个通配符的交换机
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    这里需要在bean上加上名称(虽然如果没有时会使用方法名,但是严谨),便于之后交换机和队列绑定操作。

    除了通配符交换机外,还支持广播型交换机定向型交换机

    • 广播型交换机
    @Bean("fanoutExchange")
    public Exchange fanoutExchange() {
        return ExchangeBuilder.fanoutExchange("fanout_exchange").durable(true).build();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 定向型交换机
    @Bean("directExchange")
    public Exchange directExchange() {
        return ExchangeBuilder.directExchange("direct_exchange").durable(true).build();
    }
    
    • 1
    • 2
    • 3
    • 4

    (3)创建一个队列

    /**
     * 创建队列
     *
     * @return 队列
     */
    @Bean("bootQueue")
    public Queue bootQueue() {
        return QueueBuilder.durable(QUEUE_NAME).build();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (4)绑定交换机和队列

    /**
     * 绑定队列和交换机
     * 主要:队列、交换机、routing key
     *
     * @return 绑定关系
     */
    @Bean
    public Binding bindingQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (5)完整配置类

    @Configuration
    public class RabbitMQConfig {
    
        /**
         * 默认测试的交换机机名称
         * springboot_topic_exchange
         */
        public static final String EXCHANGE_NAME = "springboot_topic_exchange";
    
        /**
         * 默认的队列名称
         * springboot_root_queue
         */
        public static final String QUEUE_NAME = "springboot_root_queue";
    
        /**
         * 创建交换机
         *
         * @return 交换机
         */
        @Bean("bootExchange")
        public Exchange bootExchange() {
            // 创建一个通配符的交换机
            return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
        }
        
    
        /**
         * 创建队列
         *
         * @return 队列
         */
        @Bean("bootQueue")
        public Queue bootQueue() {
            return QueueBuilder.durable(QUEUE_NAME).build();
        }
    
        /**
         * 绑定队列和交换机
         * 主要:队列、交换机、routing key
         *
         * @return 绑定关系
         */
        @Bean
        public Binding bindingQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange) {
            return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    1.5 测试发送消息

    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class ProducerTest {
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        @Test
        public void testSend() {
            rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "boot.name", "Spring Boot RabbitMQ");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.消费者SpringBootConsumer

    2.1 创建工程并导入依赖

    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.5.6version>
            <relativePath/> 
        parent>
        <groupId>com.examplegroupId>
        <artifactId>springboot-consumerartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>springboot-consumername>
        <description>springboot-consumerdescription>
        <properties>
            <java.version>1.8java.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-amqpartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>org.springframework.amqpgroupId>
                <artifactId>spring-rabbit-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    PS:以上依赖是由springboot工程创建完成,和之前手动创建没有本质差别

    2.2 创建配置文件并配置

    SpringBoot配置文件名称为application.yml

    需要配置的内容如下:

    # 配置RabbitMQ的基本信息
    spring:
      rabbitmq:
        # 地址
        host: 192.168.52.128
        # 端口
        port: 5672
        # 用户名
        username: admin
        # 密码
        password: admin
        # 虚拟机
        virtual-host: /test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.3 创建项目启动类

    @SpringBootApplication
    public class ConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.3 创建监听器

    @Component
    public class RabbitMQListener {
    
        @RabbitListener(queues = "springboot_root_queue")
        public void listenerQueue(Message message) {
            System.out.println("RabbitMQListener:" + new String(message.getBody()));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    @RabbitListener表示当前方法监听对应的队列,并且支持多队列。

    2.4 run

    控制台如下:

    RabbitMQListener:Spring Boot RabbitMQ

  • 相关阅读:
    【机器学习】机器学习创建算法第3篇:K-近邻算法,学习目标【附代码文档】
    suricata 流管理
    CCF CSP认证历年题目自练Day28
    HTML5 游戏开发实战 | 黑白棋
    实验2:Numpy手写多层神经网络
    java-php-python-ssm学校旧书交易网站计算机毕业设计
    【MATLAB第77期】基于MATLAB代理模型算法的降维/特征排序/数据处理回归/分类问题MATLAB代码实现【更新中】
    Kubernetes(k8s)资源管理
    【Linux私房菜】—— 使用虚拟机时的几个小技巧
    npm 执行命令时报错npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve
  • 原文地址:https://blog.csdn.net/zhongjianboy/article/details/130747596