跳到主要内容

Spring Cloud Stream 参考文档

DeepSeek V3 中英对照 Spring Cloud Stream Reference Documentation

前言

本节将详细介绍如何使用 Spring Cloud Stream。内容包括创建和运行流应用程序等主题。

介绍 Spring Cloud Stream

Spring Cloud Stream 是一个用于构建消息驱动微服务应用的框架。Spring Cloud Stream 基于 Spring Boot 构建,用于创建独立的、生产级的 Spring 应用程序,并使用 Spring Integration 提供与消息代理的连接。它为多个供应商的中间件提供了约定俗成的配置,引入了持久化发布-订阅语义、消费者组和分区的概念。

通过将 spring-cloud-stream 依赖项添加到应用程序的类路径中,您可以立即连接到由提供的 spring-cloud-stream binder 暴露的消息代理(稍后会详细介绍),并且您可以实现您的功能需求,该功能需求由 java.util.function.Function 运行(基于传入的消息)。

以下清单展示了一个简单的例子:

@SpringBootApplication
public class SampleApplication {

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

@Bean
public Function<String, String> uppercase() {
return value -> value.toUpperCase();
}
}
java

以下列表展示了相应的测试:

@SpringBootTest(classes =  SampleApplication.class)
@EnableTestBinder
class BootTestStreamApplicationTests {

@Autowired
private InputDestination input;

@Autowired
private OutputDestination output;

@Test
void contextLoads() {
input.send(new GenericMessage<byte[]>("hello".getBytes()));
assertThat(output.receive().getPayload()).isEqualTo("HELLO".getBytes());
}
}
java

主要概念

Spring Cloud Stream 提供了许多抽象和原语,简化了编写消息驱动的微服务应用程序的过程。本参考手册的其余部分提供了更多详细信息。

章节总结