• 【RabbitMQ】——整合SpringBoot


    一、创建SpringBoot工程

    使用idea创建一个普通Maven工程,也可以创建也给SpringBoot工程,两者都可以。
    最终架构图如下, 其中有个一swaggerconfig 配置类,web接口文档工具,可以不用管。
    在这里插入图片描述

    二、pom添加依赖

    采用springboot2.2.4 版本,JDK1.8版本。多添加了一个web模块依赖,可以方便后面通过页面进行发送消息

    
    <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.2.4.RELEASEversion>
            <relativePath/> 
        parent>
        <groupId>com.rabbitmqDemo-bootgroupId>
        <artifactId>rabbitmqDemo-bootartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>rabbitmqDemo-bootname>
        <description>rabbitmqDemo-bootdescription>
        <properties>
            <java.version>1.8java.version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-amqpartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            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
    • 47
    • 48
    • 49
    • 50
    • 51

    三、yml配置文件

    rabbitmq web页面端口为15672,但是代码配置的时候是5672
    username 和password 在之前的教程中已经给出配置方式,可查阅我之前的文章。

    spring:
      rabbitmq:
        host: localhost
        username: admin
        password: qwerasdf
        port: 5672
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    四、主启动类

    主启动类,和普通springboot启动类一样,需要加载包的最上层,如果不熟悉springboot的同学,可以先补充相关知识。

    package com.rabbitmqdemoboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class RabbitmqDemoBootApplication {
    
        public static void main(String[] args) {
            
            SpringApplication.run(RabbitmqDemoBootApplication.class, args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    五、rabbitmq配置类

    1. 该配置文件中声明交换机(exchange)和队列(queue),然后通过routingkey 将交换机和队列进行绑定。
    2. 需要在类的上边添加@Configuration标签
    3. 这里采用的路由模式(direct)的交换机
    package com.rabbitmqdemoboot.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;
    
    import java.util.HashMap;
    import java.util.Map;
    /**
     * rabbitmq配置文件类
     */
    @Configuration
    public class RabbitmqConfig {
        /**
         * 声明普通交换机X
         *
         * @return 交换机X
         */
        @Bean("xExchange")
        public DirectExchange xExchange() {
            return new DirectExchange(RabbitmqConstant.EXCHANGE_NORMAL_X);
        }
    
        /**
         * 声明队列 qa
         * TTL 10s
         *
         * @return 交换机qa
         */
        @Bean("qaQueue")
        public Queue qaQueue() {
            Map<String, Object> arguments = new HashMap<>();
            //设置死信交换机
            arguments.put("x-dead-letter-exchange", RabbitmqConstant.EXCHANGE_DEAD_Y);
            //设置死信routingKey
            arguments.put("x-dead-letter-routing-key", RabbitmqConstant.ROUTING_KEY_DEAD_YD);
            //设置TTL,单位ms
            arguments.put("x-message-ttl", 10 * 1000);
            return QueueBuilder
                    .durable(RabbitmqConstant.QUEUE_NORMAL_QA)
                    .withArguments(arguments)
                    .build();
        }
        /**
         * 绑定routingKey
         * QA Binding X
         */
        @Bean
        public Binding queueQABindingX(@Qualifier("qaQueue") Queue qaQueue, @Qualifier("xExchange") DirectExchange xExchange) {
            return BindingBuilder.bind(qaQueue).to(xExchange).with(RabbitmqConstant.ROUTING_KEY_NORMAL_XA);
        }
    }
    
    
    • 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
    • 50
    • 51
    • 52
    • 53

    六、发送消息

    发送消息采用 RabbitTemplate 工具进行发送消息

    package com.rabbitmqdemoboot.Controller;
    
    import com.rabbitmqdemoboot.config.RabbitmqConstant;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Date;
    
    /**
     * 发送延迟消息
     */
    @RestController
    @RequestMapping("/ttl")
    public class SendMsgController {
    
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        @GetMapping("/sendMsg/{message}")
        public String sendMsg(@PathVariable String message) {
            rabbitTemplate.convertAndSend(RabbitmqConstant.EXCHANGE_NORMAL_X, RabbitmqConstant.ROUTING_KEY_NORMAL_XA, "10s TTL queue" + message);
            return "success";
        }
    
       
    }
    
    
    • 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

    七、接收消息

    接收器采用监听器进行监听

    package com.rabbitmqdemoboot.consumer;
    
    
    import com.rabbitmq.client.Channel;
    import com.rabbitmqdemoboot.config.RabbitmqConstant;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.amqp.core.Message;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    
    import java.util.Date;
    
    @Slf4j
    @Component
    public class DeadLetterQueueConsumer {
    
        @RabbitListener(queues = RabbitmqConstant.QUEUE_DEAD_QA)
        public void receive(Message message, Channel channel) {
            String s = new String(message.getBody());
            log.info("current time : {} , receive message  : {}", new Date(), s);
    
        }
    }
    
    
    • 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
  • 相关阅读:
    java毕业设计家庭理财记账系统(附源码、数据库)
    java面试题-spring与mybatis框架面试题
    【ARM】中断的处理
    Open Interpreter:OpenAI Code Interpreter的开源实现|本地化|可联网
    1334. 阈值距离内邻居最少的城市/Floyd 【leetcode】
    【高项】- 进度管理论文
    数据挖掘 | Count数据去除批次效应后不是整数甚至还出现负值导致无法进行差异分析怎么办?
    北邮22级信通院数电:Verilog-FPGA(10)第十周实验 实现移位寄存器74LS595(仿真方法验证)
    05_openstack之Neutron网络管理
    使用Redis发布订阅模式实现 Session共享
  • 原文地址:https://blog.csdn.net/qq_42000631/article/details/126378276