springboot集成RabbitMQ

springboot集成RabbitMQ

  • [1. 添加 Maven 依赖](#1. 添加 Maven 依赖)
  • [2. 配置 RabbitMQ](#2. 配置 RabbitMQ)
  • [3. 创建消息生产者](#3. 创建消息生产者)
  • [4. 创建消息消费者](#4. 创建消息消费者)
  • [5. 运行和测试](#5. 运行和测试)

1. 添加 Maven 依赖

首先,你需要在你的 pom.xml 文件中添加 Spring Boot RabbitMQ Starter 的依赖。

xml 复制代码
<dependencies>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-amqp</artifactId>  
    </dependency>    
</dependencies>

2. 配置 RabbitMQ

在 application.properties 或 application.yml 文件中添加 RabbitMQ 的配置信息。

java 复制代码
spring.rabbitmq.host=localhost  
spring.rabbitmq.port=5672  
spring.rabbitmq.username=guest  
spring.rabbitmq.password=guest

3. 创建消息生产者

创建一个服务类来发送消息到 RabbitMQ 队列。

java 复制代码
import org.springframework.amqp.rabbit.core.RabbitTemplate;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
@Service  
public class MessageProducerService {  
  
    @Autowired  
    private RabbitTemplate rabbitTemplate;  
  
    public void sendMessage(String queueName, String message) {  
        rabbitTemplate.convertAndSend(queueName, message);  
    }  
}

4. 创建消息消费者

创建一个类来监听 RabbitMQ 队列并处理接收到的消息。

java 复制代码
import org.springframework.amqp.rabbit.annotation.RabbitListener;  
import org.springframework.stereotype.Component;  
  
@Component  
public class MessageConsumerService {  
  
    @RabbitListener(queues = "myQueue")  
    public void receiveMessage(String message) {  
        System.out.println("Received message: " + message);  
    }  
}

5. 运行和测试

创建一个简单的 REST API 或使用 Spring Boot 的命令行运行器来测试 RabbitMQ 的集成。

java 复制代码
import org.springframework.boot.CommandLineRunner;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.context.annotation.Bean;  
  
@SpringBootApplication  
public class RabbitmqApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(RabbitmqApplication.class, args);  
    }  
  
    @Bean  
    public CommandLineRunner run(MessageProducerService producer) {  
        return args -> {  
            producer.sendMessage("myQueue", "Hello RabbitMQ!");  
        };  
    }  
}
相关推荐
计算机-秋大田28 分钟前
基于微信小程序的电子竞技信息交流平台设计与实现(LW+源码+讲解)
spring boot·后端·微信小程序·小程序·课程设计
customer084 小时前
【开源免费】基于SpringBoot+Vue.JS景区民宿预约系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
精通HelloWorld!8 小时前
使用HttpClient和HttpRequest发送HTTP请求
java·spring boot·网络协议·spring·http
拾忆,想起9 小时前
如何选择Spring AOP的动态代理?JDK与CGLIB的适用场景
spring boot·后端·spring·spring cloud·微服务
customer0811 小时前
【开源免费】基于SpringBoot+Vue.JS美食推荐商城(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
一 乐12 小时前
基于微信小程序的酒店管理系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·微信小程序·酒店管理系统
小万编程17 小时前
【2025最新计算机毕业设计】基于SpringBoot+Vue家政呵护到家护理服务平台(高质量源码,可定制,提供文档,免费部署到本地)
java·vue.js·spring boot·计算机毕业设计·java毕业设计·web毕业设计
XYu1230121 小时前
Spring Boot 热部署实现指南
java·ide·spring boot·intellij-idea
是小崔啊1 天前
Spring Boot - 数据库集成07 - 数据库连接池
数据库·spring boot·oracle
细心的莽夫1 天前
SpringBoot 基础(Spring)
spring boot·后端·spring