美文网首页个人学习
RabbitMQ之六整合SpringBoot

RabbitMQ之六整合SpringBoot

作者: Java及SpringBoot | 来源:发表于2020-02-21 13:59 被阅读0次

    个人专题目录

    1. Spring Boot整合RabbitMQ

    1.1 简介

    在Spring项目中,可以使用Spring-Rabbit去操作RabbitMQ
    https://github.com/spring-projects/spring-amqp

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

    一般在开发过程中

    生产者工程:

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

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

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

    消费者工程:

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

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

    1.2. 搭建生产者工程

    添加依赖

    修改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.xubh</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>
    

    启动类

    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

    1)配置文件

    创建application.yml,内容如下:

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

    2)绑定交换机和队列

    创建RabbitMQ队列与交换机绑定的配置类

    @Configuration
    public class RabbitMQConfig {
        //交换机名称
        public static final String ITEM_TOPIC_EXCHANGE = "item_topic_exchange";
        //队列名称
        public static final String ITEM_QUEUE = "item_queue";
    
        //声明交换机
        @Bean("itemTopicExchange")
        public Exchange topicExchange(){
            return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build();
        }
    
        //声明队列
        @Bean("itemQueue")
        public Queue itemQueue(){
            return QueueBuilder.durable(ITEM_QUEUE).build();
        }
    
        //绑定队列和交换机
        @Bean
        public Binding itemQueueExchange(@Qualifier("itemQueue") Queue queue,
                                         @Qualifier("itemTopicExchange") Exchange exchange){
            return BindingBuilder.bind(queue).to(exchange).with("item.#").noargs();
        }
    
    }
    

    1.3 搭建消费者工程

    消息监听处理类

    编写消息监听器

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

    1.4. 测试

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

    @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中控制台查看是否接收到对应消息。

    另外;也可以在RabbitMQ的管理控制台中查看到交换机与队列的绑定.

    相关文章

      网友评论

        本文标题:RabbitMQ之六整合SpringBoot

        本文链接:https://www.haomeiwen.com/subject/czscqhtx.html