Spring Cloud Stream 参考文档

前言

本节将更详细地介绍如何使用 Spring Cloud Stream。 它涵盖了创建和运行流应用程序等主题。

Spring Cloud Stream 简介

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

通过将 spring-cloud-stream 依赖项添加到应用程序的类路径中,您可以立即连接到由提供的 spring-cloud-stream 绑定器(稍后将详细介绍)公开的消息代理,并且可以实现您的功能需求,该需求由 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();
	}
}

以下清单显示了相应的测试:

@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());
	}
}

主要概念

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