Spring Cloud Stream Tutorial (i) Introduction to spring Cloud stream

Source: Internet
Author: User

Spring Cloud Stream is the framework for building a message-driven microservices application. Spring Cloud Stream builds a separate production-level spring application based on spring boot and uses spring integration to provide a connection to the message broker. It provides a view configuration of middleware from several vendors, and describes the concept of persistent publish subscription semantics, consumer groups, and partitioning.

You can add @enablebinding comments to your application so that you can immediately connect to the message agent, and you may add @streamlistener to the method so that it receives the events that the stream handles. The following is a simple receiver application that receives external messages.

@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 annotations require one or more interfaces as parameters (in this case, the parameter is a single sink interface). The interface declares the input and/or output channels. Spring Cloud Stream provides interfaces Source,sink and processor; You can also define your own interface.

The following is the definition of the sink interface:

public interface Sink {  String INPUT = "input";  @Input(Sink.INPUT)  SubscribableChannel input();}

  

@Input note identifies the input channel through which messages received by the input channel enter the application; @Output comment Identifies the output channel from which the published message will leave the application. @Input and @output annotations can use the channel name as the parameter; If no name is provided, the name of the annotation method is used.

Spring Cloud Stream will create an implementation of an interface for you. You can use it in your application through automatic connections, such as the following sample test cases.

@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = VoteRecordingSinkApplication.class)@WebAppConfiguration@DirtiesContextpublic class StreamApplicationTests {  @Autowired  private Sink sink;  @Test  public void contextLoads() {    assertNotNull(this.sink.input());  }}

Spring Cloud Stream Tutorial (i) Introduction to spring Cloud stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.