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