跳到主要内容

快速入门指南(适合急性子)

DeepSeek V3 中英对照 Quick Tour for the impatient

介绍

这是五分钟快速入门 Spring AMQP 的指南。

前提条件:安装并运行 RabbitMQ 代理(https://www.rabbitmq.com/download.html)。然后获取 spring-rabbit JAR 及其所有依赖项——最简单的方法是在构建工具中声明依赖项。例如,对于 Maven,你可以执行类似于以下的操作:

<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>3.2.3</version>
</dependency>
xml

对于 Gradle,你可以执行类似于以下的操作:

compile 'org.springframework.amqp:spring-rabbit:3.2.3'
groovy

兼容性

Spring Framework 的最低依赖版本为 6.1.0。

最低要求的 amqp-client Java 客户端库版本为 5.18.0。

stream-client Java 客户端库的最低版本为 0.12.0。

非常非常快速

本节提供了最快速的入门介绍。

首先,添加以下 import 语句,以便本节后面的示例能够正常工作:

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
java

以下示例使用普通的命令式 Java 来发送和接收消息:

ConnectionFactory connectionFactory = new CachingConnectionFactory();
AmqpAdmin admin = new RabbitAdmin(connectionFactory);
admin.declareQueue(new Queue("myqueue"));
AmqpTemplate template = new RabbitTemplate(connectionFactory);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");
java

需要注意的是,在原生 Java Rabbit 客户端中也有一个 ConnectionFactory。我们在前面的代码中使用了 Spring 的抽象层。它会缓存通道(以及可选的连接)以便重用。我们依赖于代理中的默认交换器(因为在发送时没有指定交换器),以及所有队列通过其名称默认绑定到默认交换器(因此,我们可以在发送时使用队列名称作为路由键)。这些行为在 AMQP 规范中有定义。

使用 XML 配置

以下示例与前面的示例相同,但将资源配置外部化到 XML 中:

ApplicationContext context =
new GenericXmlApplicationContext("classpath:/rabbit-context.xml");
AmqpTemplate template = context.getBean(AmqpTemplate.class);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");
java
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
https://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">

<rabbit:connection-factory id="connectionFactory"/>

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>

<rabbit:admin connection-factory="connectionFactory"/>

<rabbit:queue name="myqueue"/>

</beans>
xml

默认情况下,<rabbit:admin/> 声明会自动查找类型为 QueueExchangeBinding 的 bean,并代表用户将它们声明到代理。因此,在简单的 Java 驱动程序中,您不需要显式地使用该 bean。XML 模式中有许多选项可以配置组件的属性。您可以使用 XML 编辑器的自动完成功能来探索这些选项并查看它们的文档。

使用 Java 配置

以下示例重复了与前面示例相同的示例,但使用 Java 定义了外部配置:

ApplicationContext context =
new AnnotationConfigApplicationContext(RabbitConfiguration.class);
AmqpTemplate template = context.getBean(AmqpTemplate.class);
template.convertAndSend("myqueue", "foo");
String foo = (String) template.receiveAndConvert("myqueue");

........

@Configuration
public class RabbitConfiguration {

@Bean
public CachingConnectionFactory connectionFactory() {
return new CachingConnectionFactory("localhost");
}

@Bean
public RabbitAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}

@Bean
public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(connectionFactory());
}

@Bean
public Queue myQueue() {
return new Queue("myqueue");
}
}
java

使用 Spring Boot 自动配置和异步 POJO 监听器

Spring Boot 会自动配置基础设施的 beans,如下例所示:

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
public ApplicationRunner runner(AmqpTemplate template) {
return args -> template.convertAndSend("myqueue", "foo");
}

@Bean
public Queue myQueue() {
return new Queue("myqueue");
}

@RabbitListener(queues = "myqueue")
public void listen(String in) {
System.out.println(in);
}

}
java