Spring Boot 消息队列(以RabbitMQ为例)

文章目录

      • [RabbitMQ 简介与安装](#RabbitMQ 简介与安装)
        • [1. RabbitMQ 简介](#1. RabbitMQ 简介)
        • [2. RabbitMQ 安装](#2. RabbitMQ 安装)
      • [Spring Boot 集成 RabbitMQ](#Spring Boot 集成 RabbitMQ)
        • [1. 创建 Spring Boot 项目](#1. 创建 Spring Boot 项目)
        • [2. 配置 RabbitMQ](#2. 配置 RabbitMQ)
        • [3. 定义消息队列和交换机](#3. 定义消息队列和交换机)
        • [4. 发送消息](#4. 发送消息)
        • [5. 接收消息](#5. 接收消息)
        • [6. 测试消息发送和接收](#6. 测试消息发送和接收)

RabbitMQ 简介与安装

1. RabbitMQ 简介

RabbitMQ 是一个开源的消息代理和队列服务器,基于 AMQP(高级消息队列协议)实现。它具有以下特点:

  • 可靠性:支持持久化、传输确认、发布确认等机制,确保消息不丢失。
  • 灵活性:支持多种消息模型,如点对点、发布 - 订阅等。
  • 分布式:可以通过集群和镜像队列来实现高可用性和扩展性。
  • 多语言支持:支持多种编程语言,如 Java、Python、C# 等。
2. RabbitMQ 安装

以下以在 Linux(Ubuntu)系统上安装 RabbitMQ 为例:

步骤 1:安装 Erlang

RabbitMQ 是用 Erlang 语言编写的,因此需要先安装 Erlang。

bash 复制代码
# 添加 Erlang 仓库
wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb
sudo dpkg -i erlang-solutions_2.0_all.deb
sudo apt-get update

# 安装 Erlang
sudo apt-get install esl-erlang

步骤 2:安装 RabbitMQ

bash 复制代码
# 添加 RabbitMQ 仓库
echo "deb https://dl.bintray.com/rabbitmq/debian $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list
wget -O- https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc | sudo apt-key add -
wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -
sudo apt-get update

# 安装 RabbitMQ
sudo apt-get install rabbitmq-server

步骤 3:启动 RabbitMQ 服务

bash 复制代码
sudo systemctl start rabbitmq-server

步骤 4:设置开机自启

bash 复制代码
sudo systemctl enable rabbitmq-server

步骤 5:启用管理界面

bash 复制代码
sudo rabbitmq-plugins enable rabbitmq_management

访问 http://localhost:15672,使用默认用户名 guest 和密码 guest 登录管理界面。

Spring Boot 集成 RabbitMQ

1. 创建 Spring Boot 项目

可以使用 Spring Initializr(https://start.spring.io/)创建一个新的 Spring Boot 项目,添加以下依赖:

  • Spring Web
  • Spring AMQP
2. 配置 RabbitMQ

application.propertiesapplication.yml 中配置 RabbitMQ 连接信息:

application.properties

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

application.yml

yaml 复制代码
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
3. 定义消息队列和交换机

创建一个配置类来定义队列、交换机和绑定关系:

java 复制代码
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    public static final String QUEUE_NAME = "myQueue";
    public static final String EXCHANGE_NAME = "myExchange";
    public static final String ROUTING_KEY = "myRoutingKey";

    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, true);
    }

    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(EXCHANGE_NAME);
    }

    @Bean
    public Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
}
4. 发送消息

创建一个消息发送者类:

java 复制代码
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(String message) {
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY, message);
    }
}
5. 接收消息

创建一个消息接收者类:

java 复制代码
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class MessageReceiver {

    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}
6. 测试消息发送和接收

创建一个控制器来测试消息发送:

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {

    @Autowired
    private MessageSender messageSender;

    @GetMapping("/send")
    public String sendMessage(@RequestParam String message) {
        messageSender.sendMessage(message);
        return "Message sent: " + message;
    }
}

启动 Spring Boot 应用程序,访问 http://localhost:8080/send?message=Hello,RabbitMQ,在控制台可以看到消息接收的输出。

通过以上步骤,就完成了 Spring Boot 与 RabbitMQ 的集成,实现了消息的发送和接收。4

相关推荐
m0_740043738 分钟前
SpringBoot05-配置文件-热加载/日志框架slf4j/接口文档工具Swagger/Knife4j
java·spring boot·后端·log4j
招风的黑耳1 小时前
我用SpringBoot撸了一个智慧水务监控平台
java·spring boot·后端
大佐不会说日语~1 小时前
Spring AI Alibaba 的 ChatClient 工具注册与 Function Calling 实践
人工智能·spring boot·python·spring·封装·spring ai
Miss_Chenzr1 小时前
Springboot优卖电商系统s7zmj(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
程序员游老板1 小时前
基于SpringBoot3+vue3的爱心陪诊平台
java·spring boot·毕业设计·软件工程·课程设计·信息与通信
期待のcode1 小时前
Springboot核心构建插件
java·spring boot·后端
Miss_Chenzr2 小时前
Springboot旅游景区管理系统9fu3n(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·旅游
五阿哥永琪2 小时前
Spring Boot 中自定义线程池的正确使用姿势:定义、注入与最佳实践
spring boot·后端·python
canonical_entropy3 小时前
Nop入门:增加DSL模型解析器
spring boot·后端·架构
计算机毕设VX:Fegn08955 小时前
计算机毕业设计|基于springboot + vue图书借阅管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计