目录
RabbitMQ官网
一、AMQP与Sping AMQP
RabbitMQ此处编写的是Java的原始的API,我们可以通过Spring的AMQP来简化我们对RabbitMQ的开发使用:
Spring AMQP实现了一套统一的Java AMQP标准,我们在使用Spring AMQP的API时,不需要关心底层实现,Spring已经帮助我们调用了AMQP协议,简化我们的开发。 Spring AMQP的默认实现是Spring-Rabbit。
1.1. SpringAMQP常用类
SpringAMQP提供的声明类,用来声明队列、交换机及其绑定关系:
Queue:用于声明队列,可以用工厂类QueueBuilder构建
Exchange:用于声明交换机,可以用工厂类ExchangeBuilder构建
Binding:用于声明队列和交换机的绑定关系,可以用工厂类BindingBuilder构建
我们在实际项目中也可以通过new的方式来创建Queue、Exchange和Binding
1.2. SpringAMQP常用注解
SpringAMQP还提供了基于@RabbitListener注解来声明队列和交换机的方式:
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "direct.queue1"),
exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
key = {"red", "blue"}
))
public void listenDirectQueue1(String msg) {
System.out.println("消费者1接收到Direct消息:【"+msg+"】");
}
1.3. 实际电商项目示例代码
支付服务的代码里,通过支付成功向订单服务发送消息修改订单状态:
通过@RabbitListener注解声明一个消费者的业务代码,接收支付成功消息,修改订单状态为已支付:
二、 案例代码演示
2.1. 案例需求
1. 利用控制台创建队列simple.queue
2. 在publisher服务中,利用SpringAMQP直接向simple.queue发送消息
3. 在consumer服务中,利用SpringAMQP编写消费者,监听simple.queue队列
2.2. 实现代码
2.2.1. 创建SpringBoot工程
完整的工程目录结构及代码文件如下:
2.2.2. 父工程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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mq-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mq-demo</name>
<description>mq-demo</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- spring amqp依赖 -->
<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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2.3. 生产者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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>publisher</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>publisher</name>
<description>publisher</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2.3. 生产者配置文件
spring:
rabbitmq:
# 主机
host: 127.0.0.1
# 端口
port: 5672
# 默认用户密码是guest guest 如果需要自己创建新用户,参看我的上一章节内容
username: Wangzhexiao
password: Wangzhexiao
# 默认的虚拟主机是/ 如果需要自己创建虚拟主机,参看我的上一章节内容
virtual-host: /hangzhou
2.2.4. 生产者核心代码
package com.example.publisher;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class PublisherApplicationTests {
@Resource
private RabbitTemplate rabbitTemplate;
@Test
void sendMessage() {
// 队列名称
String queueName = "simple.queue";
// 消息内容
String message = "人生苦短,持续不断地努力拼搏,迎难而上!";
rabbitTemplate.convertAndSend(queueName, message);
}
}
2.2.5. 消费者RabbitMQConfig
队列、交换机及绑定关系,通常是在消费者这一侧配置,生产者不关心这些配置:
package com.example.consumer;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
@Bean
Queue myQueue() {
// 使用 QueueBuilder 创建一个持久化队列
return QueueBuilder.durable("simple.queue").build();
}
}
2.2.6. 消费者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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer</name>
<description>consumer</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2.7. 消费者配置文件
spring:
rabbitmq:
# 主机
host: 127.0.0.1
# 端口
port: 5672
# 默认用户密码是guest guest 如果需要自己创建新用户,参看我的上一章节内容
username: Wangzhexiao
password: Wangzhexiao
# 默认的虚拟主机是/ 如果需要自己创建虚拟主机,参看我的上一章节内容
virtual-host: /hangzhou
2.2.8. 消费者核心代码
SpringAMQP提供声明式的消息监听,我们只需要通过注解在方法上声明要监听的队列名称,将来SpringAMQP就会把消息传递给当前方法:
package com.example.consumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class SimpleListener {
@RabbitListener(queues = "simple.queue")
public void listener(String message) {
log.info("人生是个不断攀登的过程:{}", message);
}
}
2.3. 运行效果