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!");  
        };  
    }  
}
相关推荐
小强库计算机毕业设计5 小时前
基于 Spring Boot + Vue3 的校园管理系统
java·spring boot·后端·项目实战·管理系统·技术分享·校园管理系统实战
云烟成雨TD8 小时前
Micrometer 系列【63】统一观测:基于 Spring Boot 的生产级演示案例 | 基于 OTLP 集成 Prometheus + Jaeger
spring boot·云原生·prometheus
csdn2015_8 小时前
springboot +mybatis 查询myql开启程序级别缓存
spring boot·缓存·mybatis
chuan.bai8 小时前
Java RAG 实战(第 8 篇):Spring Boot RAG 查询 API
java·spring boot·贪心算法
vx-程序开发9 小时前
springboot旅游推介平台---附源码24175
java·spring boot·python·spring cloud·eclipse·django·idea
小蒜学长9 小时前
“喵汪联盟”宠物领养系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·宠物
xbgRS12 小时前
springboot的自动装配
java·spring boot
嘻哈∠※19 小时前
0061基于 SpringBoot 的投稿与稿件处理系统设计与实现
java·spring boot·后端
城管不管1 天前
重生——第九次面试2026.8.13某车一面
分布式·ai·面试·职场和发展·rabbitmq
apgk11 天前
基于Canal实现mysql数据同步到消息队列(RabbitMQ/Kafka)详细操作教程
docker·kafka·rabbitmq