前言

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

Spring Cloud Stream 简介

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

通过向应用程序的类路径添加依赖项,可以立即获得连接 到由提供的 Binder 公开的消息代理(稍后会详细介绍),您可以实现您的功能 requirement,它由 .spring-cloud-streamspring-cloud-streamjava.util.function.FunctionSpring中文文档

以下列表显示了一个快速示例:Spring中文文档

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

以下列表显示了相应的测试:Spring中文文档

@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 提供了许多抽象和原语,简化了消息驱动的微服务应用程序的编写。 本参考手册的其余部分提供了更多详细信息。Spring中文文档