springboot整合RabbitMQ

1.Spring-AMQP概念

  • 什么是Spring-AMQP
    • Spring框架的AMQP消息解决方案,提供模板化的发送和接收消息的抽象层,提供基于消息驱动的POJO的消息监听等
    • 提供不依赖于任何特定的AMQP代理实现或客户端库通用的抽象,最终用户代码将很容易实现更易替换、添加和删除AMQP,因为它可以只针对抽象层来开发
    • 总之就是提高我们的框架整合消息队列的效率,springboot为更方便开发RabbitMQ推出了starter
    • 我们使用spring-boot-starter-amqp进行开发

2.springboot整合RabbitMQ实战

  • 创建web项目,引入依赖

    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-amqpartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    
  • yml配置文件

    spring:
      rabbitmq:
        host: 192.168.101.128
        port: 5672
        virtual-host: /dev
        password: Gen123
        username: admin
    
  • RabbitMQConfig文件

    package com.gen.config;
    
    import org.springframework.amqp.core.*;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class RabbitMQConfig {
    
        public static final String EXCHANGE_NAME = "order_exchange";
        public static final String QUEUE_NAME = "order_queue";
    
        /**
         * 交换机
         *
         * @return
         */
        @Bean
        public Exchange orderExchange() {
            return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
        }
    
        /**
         * 队列
         *
         * @return
         */
        @Bean
        public Queue orderQueue() {
            return QueueBuilder.durable(QUEUE_NAME).build();
        }
    
        /**
         * 交换机与队列进行绑定
         *
         * @param orderQueue
         * @param orderExchange
         * @return
         */
        @Bean
        public Binding orderBinding(Queue orderQueue, Exchange orderExchange) {
            return BindingBuilder.bind(orderQueue).to(orderExchange).with("order.#").noargs();
        }
    }
    
  • 消息生产者-测试类

    package com.gen;
    
    import com.gen.config.RabbitMQConfig;
    import org.junit.jupiter.api.Test;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class GenRabbitmqApplicationTests {
    
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        @Test
        void send() {
            this.rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "order.new", "您有新订单");
        }
    }
    
  • 消息消费者

    package com.gen.listener;
    
    import com.gen.config.RabbitMQConfig;
    import org.springframework.amqp.core.Message;
    import org.springframework.amqp.rabbit.annotation.RabbitHandler;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    @Component
    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public class OrderMQListener {
    
        /**
         * RabbitHandler会自动匹配消息类型(消息自动确认)
         *
         * @param msg
         * @param message
         */
        @RabbitHandler
        public void orderConsumer(String msg, Message message) {
            System.out.println(msg);
            System.out.println(message);
        }
    }
    

你可能感兴趣的:(RabbitMQ,springboot,spring,boot,rabbitmq)