本節將詳細介紹如何使用Spring Cloud Stream。它涵蓋了建立和運行流應用程序等主題。html
Spring Cloud Stream是構建消息驅動的微服務應用程序的框架。Spring Cloud Stream基於Spring Boot創建獨立的生產級Spring應用程序,並使用Spring Integration提供與消息代理的鏈接。它提供了來自幾家供應商的中間件的意見配置,介紹了持久發佈訂閱語義,消費者組和分區的概念。spring
您能夠將@EnableBinding
註釋添加到應用程序,以便當即鏈接到消息代理,而且能夠將@StreamListener
添加到方法中,以使其接收流處理的事件。如下是接收外部消息的簡單接收器應用程序。架構
@SpringBootApplication @EnableBinding(Sink.class) public class VoteRecordingSinkApplication { public static void main(String[] args) { SpringApplication.run(VoteRecordingSinkApplication.class, args); } @StreamListener(Sink.INPUT) public void processVote(Vote vote) { votingService.recordVote(vote); } }
@EnableBinding
註釋須要一個或多個接口做爲參數(在這種狀況下,該參數是單個Sink
接口)。接口聲明輸入和/或輸出通道。Spring Cloud Stream提供了接口Source
,Sink
和Processor
; 您還能夠定義本身的界面。框架
如下是Sink
接口的定義:微服務
public interface Sink { String INPUT = "input"; @Input(Sink.INPUT) SubscribableChannel input(); }
@Input
註釋標識輸入通道,經過該輸入通道接收到的消息進入應用程序; @Output
註釋標識輸出通道,發佈的消息將經過該通道離開應用程序。@Input
和@Output
註釋可使用頻道名稱做爲參數; 若是未提供名稱,將使用註釋方法的名稱。測試
Spring Cloud Stream將爲您建立一個界面的實現。您能夠在應用程序中經過自動鏈接來使用它,以下面的測試用例示例。this
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = VoteRecordingSinkApplication.class) @WebAppConfiguration @DirtiesContext public class StreamApplicationTests { @Autowired private Sink sink; @Test public void contextLoads() { assertNotNull(this.sink.input()); } }
從如今開始,我這邊會將近期研發的springcloud微服務雲架構的搭建過程和精髓記錄下來,幫助更多有興趣研發spring cloud框架的朋友,但願能夠幫助更多的好學者。你們來一塊兒探討spring cloud架構的搭建過程及如何運用於企業項目。源碼來源spa