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