利用springboot整合mq

本文介绍了如何使用Springboot集成RabbitMQ,创建生产者发送消息到队列,并配置消费者监听队列接收消息。首先,创建Springboot项目,添加依赖,然后配置application.yml文件设置RabbitMQ连接信息。接着,定义配置类创建交换机、队列并进行绑定。在测试类中验证生产者发送功能,最后编写消费者监听类以接收消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主流的框架sprngboot,具体操作如下:

  1. 此处为生产者,创建一个springboot项目,导入依赖

 <groupId>org.example</groupId>
    <artifactId>springboot-rabbitmq-producers</artifactId>
    <version>1.0-SNAPSHOT</version>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.8</version>
</parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
  1. 配置application.yml文件

spring:
  rabbitmq:
    host: 为ip地址
    port: 5672
    username: guest
    password: guest
    virtual-host: /
  1. 创建rabbitConfig配置类,配置类中实现创建一个交换机,创建队列以及绑定交换机和队列

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

@Configuration
public class RabbitmqConfig {

    //交换机
    //交换机名称
    public static final String EXCHANGE_NAME="boot_topic_exchange";
    //队列名称
    public  static final String QUEUE_NAME="boot_queue";
    @Bean("bootExchange")
    public Exchange bootExchange(){
       return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }
    //队列
    @Bean("bootQueue")
    public Queue bootQueue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }
    //队列和交换机的绑定
    //知道那个队列
    //知道那个交换机
    //routing key
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,@Qualifier("bootExchange")Exchange exchange ){
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }
}
  1. 创建一个测试类,来检验生产者

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

@SpringBootTest
@RunWith(SpringRunner.class)
public class test {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
   public void test(){
        rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_NAME,"boot.haha","huazi666666");

    }
}

通过可视化平台

得到发送的消息

下面来编写消费者,和生产者区别在于,消费者是一个监听的接口

创建消费者监听类

package com.huazi;

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

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

}

消费者得到信息如下

以上便是springboot整合mq过程