Spring Cloud Stream 參考文件

前言

本節將更詳細地介紹如何使用 Spring Cloud Stream。它涵蓋了建立和執行流應用等主題。

Spring Cloud Stream 簡介

Spring Cloud Stream 是一個用於構建訊息驅動微服務應用的框架。Spring Cloud Stream 基於 Spring Boot 構建獨立的生產級 Spring 應用,並使用 Spring Integration 提供與訊息中介軟體的連線。它提供了對多個供應商的中介軟體的意見式配置,引入了持久化釋出-訂閱語義、消費者組和分割槽等概念。

透過將 spring-cloud-stream 依賴項新增到應用的 classpath 中,你可以立即連線到由提供的 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();
	}
}

以下列表顯示了相應的測試

@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 提供了一系列抽象和原語,簡化了訊息驅動微服務應用的編寫。本參考手冊的其餘部分提供了更多詳細資訊。