https://docs.spring.io/spring-boot/docs/3.2.0/reference/htmlsingle/#messaging.amqp
高级消息队列协议(AMQP)是一种面向消息的中间件的平台中立、线路级协议。Spring AMQP 项目将核心 Spring 概念应用于基于 AMQP 的消息解决方案的开发。Spring Boot 通过 RabbitMQ 为使用 AMQP 提供了多种便利,包括 “Starter” spring-boot-starter-amqp
。
RabbitMQ 支持
RabbitMQ 是一个基于 AMQP 协议的轻量级、可靠、可扩展和可移植的消息代理。Spring 使用 RabbitMQ 通过 AMQP 协议进行通信。
RabbitMQ 的配置由 Spring 中的外部配置属性 spring.rabbitmq.*
控制。例如,可能会在 application.properties
文件中声明以下部分:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=secret
或者,也可以使用 addresses
属性来配置相同的连接:
spring.rabbitmq.addresses=amqp://admin:secret@localhost
注意:当以这种方式指定地址时,host
和 port
属性将被忽略。如果地址使用 amqps
协议,则会自动启用 SSL 支持。
要配置 Spring AMQP 使用的 RabbitMQ ConnectionFactory
的低级细节,请定义一个 ConnectionFactoryCustomizer
bean。
如果上下文中存在 ConnectionNameStrategy
bean,它将被自动用于为自动配置的 CachingConnectionFactory
创建的连接命名。
要对 RabbitTemplate
进行应用程序范围的附加自定义,请使用 RabbitTemplateCustomizer
bean。
发送消息
Spring 的 AmqpTemplate
和 AmqpAdmin
会自动配置,可以直接将它们自动装配到您自己的 bean 中,如下所示:
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
private final AmqpAdmin amqpAdmin;
private final AmqpTemplate amqpTemplate;
public MyBean(AmqpAdmin amqpAdmin, AmqpTemplate amqpTemplate) {
this.amqpAdmin = amqpAdmin;
this.amqpTemplate = amqpTemplate;
}
public void someMethod() {
this.amqpAdmin.getQueueInfo("someQueue");
}
public void someOtherMethod() {
this.amqpTemplate.convertAndSend("hello");
}
}
注意:RabbitMessagingTemplate
也可以以类似的方式注入。如果定义了 MessageConverter
bean,它会自动与自动配置的 AmqpTemplate
相关联。
如果必要,任何定义为 bean 的 org.springframework.amqp.core.Queue
都会自动用于在 RabbitMQ 实例上声明相应的队列。
要重试操作,可以在 AmqpTemplate
上启用重试(例如,在代理连接丢失的情况下):
spring.rabbitmq.template.retry.enabled=true
spring.rabbitmq.template.retry.initial-interval=2s
默认情况下,重试是禁用的。还可以通过声明一个 RabbitRetryTemplateCustomizer
bean 来以编程方式自定义 RetryTemplate
。
如果需要创建更多的 RabbitTemplate
实例,或者想要覆盖默认值,Spring Boot 提供了一个 RabbitTemplateConfigurer
bean,可以使用它来初始化一个与自动配置中使用的工厂具有相同设置的 RabbitTemplate
。
发送消息到流
要发送消息到特定的流,请指定流的名称,如下所示:
spring.rabbitmq.stream.name=my-stream
如果定义了 MessageConverter
、StreamMessageConverter
或 ProducerCustomizer
bean,它会自动与自动配置的 RabbitStreamTemplate
相关联。
如果需要创建更多的 RabbitStreamTemplate
实例,或者想要覆盖默认值,Spring Boot 提供了一个 RabbitStreamTemplateConfigurer
bean,可以使用它来初始化一个与自动配置中使用的工厂具有相同设置的 RabbitStreamTemplate
。
接收消息
当存在 Rabbit 基础设施时,任何 bean 都可以使用 @RabbitListener
注解来创建一个监听端点。如果没有定义 RabbitListenerContainerFactory
,则会自动配置一个默认的 SimpleRabbitListenerContainerFactory
,并且可以使用 spring.rabbitmq.listener.type
属性切换到直接容器。如果定义了 MessageConverter
或 MessageRecoverer
bean,它会自动与默认工厂相关联。
以下示例组件在 someQueue
队列上创建了一个监听端点:
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
@RabbitListener(queues = "someQueue")
public void processMessage(String content) {
// ...
}
}
如果需要创建更多的 RabbitListenerContainerFactory
实例,或者想要覆盖默认值,Spring Boot 提供了 SimpleRabbitListenerContainerFactoryConfigurer
和 DirectRabbitListenerContainerFactoryConfigurer
,可以使用它们来初始化与自动配置中使用的工厂具有相同设置的 SimpleRabbitListenerContainerFactory
和 DirectRabbitListenerContainerFactory
。
提示:选择哪种容器类型并不重要。这两个 bean 是由自动配置暴露的。
例如,以下配置类暴露了另一个使用特定 MessageConverter
的工厂:
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class MyRabbitConfiguration {
@Bean
public SimpleRabbitListenerContainerFactory myFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
ConnectionFactory connectionFactory = getCustomConnectionFactory();
configurer.configure(factory, connectionFactory);
factory.setMessageConverter(new MyMessageConverter());
return factory;
}
private ConnectionFactory getCustomConnectionFactory() {
return ...
}
}
然后,可以在任何带有 @RabbitListener
注解的方法中使用该工厂,如下所示:
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
@RabbitListener(queues = "someQueue", containerFactory = "myFactory")
public void processMessage(String content) {
// ...
}
}
可以通过启用重试来处理监听器抛出异常的情况。默认情况下,使用 RejectAndDontRequeueRecoverer
,但可以定义自己的 MessageRecoverer
。当重试用尽时,消息将被拒绝,并且如果代理配置为这样做,则消息将被丢弃或路由到死信交换机(a dead-letter exchange )。默认情况下,重试是禁用的。还可以通过声明一个 RabbitRetryTemplateCustomizer
bean 来以编程方式自定义 RetryTemplate
。
注意:默认情况下,如果重试被禁用且监听器抛出异常,则无限次地重试传递。可以通过两种方式来修改此行为:将 defaultRequeueRejected
属性设置为 false
,以便不尝试重新传递,或者抛出 AmqpRejectAndDontRequeueException
以指示应拒绝该消息。后者是在启用重试且达到最大传递尝试次数时使用的机制。