SpringBoot整合RabbitMQ详细教程

SpringBoot整合步骤

生产端

消费端

项目骨架

代码实现

生产者与消费者的POM文件时一样的

SpringBoot配置文件application.yml

生产者

启动类 ProducerApplication

配置类 RabbitConfig

测试类 ProducerTest 

消费者

启动类 ConsumerApplication 

监听类 RabbitMQListener

小结


SpringBoot整合步骤

生产端

1. 创建生产者SpringBoot工程

2. 引入start,依赖坐标

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

3. 编写yml配置,基本信息配置

4. 定义交换机,队列以及绑定关系的配置类

5. 注入RabbitTemplate,调用方法,完成消息发送

消费端

1. 创建消费者SpringBoot工程

2. 引入start,依赖坐标

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

3. 编写yml配置,基本信息配置

4. 定义监听类,使用@RabbitListener注解完成队列监听。

项目骨架

代码实现

生产者与消费者的POM文件时一样的

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

    <groupId>com.Harmony</groupId>
    <artifactId>MQ_03_rabbitmqproduct</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

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

SpringBoot配置文件application.yml

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: Harmony
    password: 888888
    virtual-host: /HarmonyOS

生产者

启动类 ProducerApplication

@SpringBootApplication
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class);
    }
}

配置类 RabbitConfig

@Configuration
public class RabbitConfig {

    public static final String EXCHANGE_NAME = "boot_topic_exchange";
    public static final String QUEUE_NAME = "boot_queue";

    // 1.交换机
    @Bean("bootExchange")
    public Exchange bootExchange() {
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    // 2.Queue 队列
    @Bean("bootQueue")
    public Queue bootQueue() {
        return QueueBuilder.durable(QUEUE_NAME).build();
    }

    // 3.队列与交换机相互绑定 Binding
    /*
        1.知道是哪一个队列
        2.知道是哪一个交换机
        3.routing key
     */
    @Bean
    public Binding bingQueueExchange(@Qualifier("bootQueue") Queue queue,@Qualifier("bootExchange") Exchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("boot.#")
                .noargs();
    }
}

注意:

        在定义队列交换机的时候,它们最后是要被注入的。即绑定bingQueueExchange()方法中的参数,所以@Bean()里面是要有名字。而bingQueueExchange()不用被注入,所以配置为Bean就行了。

测试类 ProducerTest 

@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testSend() {
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME,"boot.haha","boot mq hello....");
    }
}

 

消费者

启动类 ConsumerApplication 

@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class);
    }
}

监听类 RabbitMQListener

@Component
public class RabbitMQListener {

    @RabbitListener(queues = "boot_queue")
    public void listenerQueue(Message message) {
        System.out.println(new String(message.getBody()));
    }
}

表示监听的队列为boot_queue,之后有信息过来,就会将消息封装到Message(是AMQP的包)里面。

小结

SpringBoot提供了快速整合RabbitMQ的方式

基本信息再yml中配置,队列交互机以及绑定关系在配置类中使用Bean的方式配置

生产端直接注入RabbitTemplate完成消息发送

消费端直接使用@RabbitListener完成消息接收 

猜你喜欢

转载自blog.csdn.net/weixin_43715214/article/details/125065999