(Thirty-nine) java version of spring cloud micro Services Architecture b2b2c e-commerce platform - Introduction Stream

Spring Cloud Stream is a framework for building micro-service application message driven. Spring Cloud Stream establishment of an independent production-level application based on Spring Spring Boot, and use Spring Integration provides connectivity to the message broker. It provides advice from several vendors of middleware configuration, introduced the concept of durable publish-subscribe semantics, consumer groups and partitions.

You can add a comment to @EnableBinding application, for immediate connection to the message broker, and can be added to @StreamListener method so that it receives the event stream processing. The following is a simple application receiver receives an external message.

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

Note @EnableBinding requires one or more interfaces as parameters (in this case, the parameter is a single Sink interface). The interface declaration input and / or output channels. Spring Cloud Stream provides an interface Source, Sink and Processor; you can also define your own interface.

The following is the definition of Sink interface:

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

Note @Input input channel identifier, received via the input channel into the application message; --output comments identifying output channels, announcement application will leave through the channel. @Input and @Output annotation can be used as a channel name argument; If no name is provided, the method will use the name annotation.

Spring Cloud Stream will implement an interface you create. You can automatically connect to use it as an example by the following test application.

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

From now on, I will be here to build R & D process and the essence of the recent spring cloud micro-cloud infrastructure services recorded, to help more interested spring cloud development framework friends, we come together to explore the process of building spring cloud architecture and how used in enterprise projects.

Guess you like

Origin blog.csdn.net/vvx0206/article/details/93967324