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 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 提供了許多抽象化和基本元件,簡化了訊息驅動微服務應用程式的編寫。本參考手冊的其餘部分提供了更多詳細資訊。