0%

RabbitMQ-与SpringBoot整合

RabbitMQ-与SpringBoot整合

RabbitMQ-与SpringBoot整合

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

开发步骤

生产者工程:

  1. 导入依赖
  2. application.yml文件配置RabbitMQ相关信息;
  3. 在生产者工程中编写配置类,用于创建交换机和队列,并进行绑定
  4. 注入RabbitTemplate对象,通过RabbitTemplate对象发送消息到交换机

消费者工程:

  1. 导入依赖
  2. application.yml文件配置RabbitMQ相关信息
  3. 创建消息处理类,用于接收队列中的消息并进行处理

生产者工程:

  1. 创建生产者工程 new module

  2. 导入maven

    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
    <?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>

    <!-- rabbit -->
    <groupId>com.xiaoruiit</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>
  3. 启动类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package com.xiaoruiit;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    /**
    * @author hxr
    * @Classname ProducerApplication
    * @Description ToDo
    */
    @SpringBootApplication
    public class ProducerApplication {
    public static void main(String[] args) {
    SpringApplication.run(ProducerApplication.class);//SpringApplication 不是SpringBootApplication
    }
    }
  4. 编写yml配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #配置rabbit服务器信息
    # 地址、端口号、账号、密码
    spring:
    rabbitmq:
    host: 192.168.3.3
    port: 5672
    username: xiaorui
    password: xiaorui
    virtual-host: learn
  5. 编写配置类

    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
    package com.xiaoruiit.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;
    /**
    * @author hxr
    * @Classname RabbitMQConfig
    * @Description ToDo
    */
    @Configuration
    public class RabbitMQConfig {
    //交换机名称
    public static final String ITEM_TOPIC_EXCHANGE = "xiaorui_topic_exchange";
    //队列名称
    public static final String ITEM_QUEUE = "xiaorui_queue";

    //声明交换机
    @Bean("xiaoruiTopicExchange")
    public Exchange topicExchange(){
    return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build();
    }

    //声明队列
    @Bean("xiaoruiQueue")
    public Queue xiaoruiQueue(){
    return QueueBuilder.durable(ITEM_QUEUE).build();
    }

    //绑定队列和交换机
    @Bean
    public Binding xiaoruiQueueExchange(@Qualifier("xiaoruiQueue") Queue queue, @Qualifier("xiaoruiTopicExchange") Exchange exchange){
    return BindingBuilder.bind(queue).to(exchange).with("xiaorui.#").noargs();
    }

    }
  6. 测试类

    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
    package com.xiaoruiit.rabbitmq;

    import com.xiaoruiit.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;

    /**
    * @author hxr
    * @Classname ProducerTest
    * @Description ToDo
    */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ProducerTest {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void test(){
    rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "xiaorui.insert", "新增,routing key 为xiaorui.insert");
    rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "xiaorui.update", "修改,routing key 为xiaorui.update");
    rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "xiaorui.delete", "删除,routing key 为xiaorui.delete");
    }
    }
  7. 结果

    image-20200906221641322

消费者工程

  1. 创建工程

  2. 导入依赖

    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
    <?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.xiaoruiit</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>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    </dependencies>


    </project>
  3. 启动类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package com.xiaoruiit;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    /**
    * @author hxr
    * @Classname ConsumerApplication
    * @Description ToDo
    */
    @SpringBootApplication
    public class ConsumerApplication {
    public static void main(String[] args) {
    SpringApplication.run(ConsumerApplication.class);
    }
    }
  4. yml配置

    1
    2
    3
    4
    5
    6
    7
    spring:
    rabbitmq:
    host: 192.168.3.3
    port: 5672
    username: xiaorui
    password: xiaorui
    virtual-host: learn
  5. 消费者监听处理类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package com.xiaoruiit.com.xiaoruiit.rabbitmq.listener;

    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;

    /**
    * @author hxr
    * @Classname Consumer
    * @Description ToDo
    */
    @Component
    public class ConsumerListener {
    /**
    * 监听某个队列的消息
    * @param message 接收到的消息
    */
    @RabbitListener(queues = "xiaorui_queue")
    public void myListener1(String message){
    System.out.println("消费者接收到的消息为:" + message);
    }

    }
  6. 控制台

    1
    2
    3
    消费者接收到的消息为:新增,routing key 为xiaorui.insert
    消费者接收到的消息为:修改,routing key 为xiaorui.update
    消费者接收到的消息为:删除,routing key 为xiaorui.delete
  7. rabbitmq控制台

image-20200906223020442