Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Spring Cloud Stream

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use Spring Cloud Stream". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Spring Cloud Stream is a framework for building message-driven microservice applications. Spring Cloud Stream builds independent production-level Spring applications based on Spring Boot and uses Spring Integration to provide connectivity to message brokers. It provides advice configuration of middleware from several vendors and introduces the concepts of persistent publish and subscription semantics, consumer groups and partitions.

You can add the @ EnableBinding annotation to your application to connect to the message broker immediately, and you can add @ StreamListener to the method so that it receives events handled by the stream. 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);}}

The @ EnableBinding annotation requires one or more interfaces as parameters (in this case, a single Sink interface). The interface declares 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 ();}

The @ Input comment identifies the input channel through which messages received enter the application; the @ Output annotation identifies the output channel through which published messages leave the application. @ Input and @ Output annotations can use the channel name as a parameter; if no name is provided, the name of the annotation method will be used.

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

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

Programming model

Binder

Binder is an abstract concept of Spring Cloud Stream and the glue between applications and message middleware.

At present, Spring Cloud Stream implements binder for Kafka and Rabbit MQ. Through binder, you can easily connect the middleware and dynamically change the destinations of the message (corresponding to the exchanges of the topic,Rabbit MQ of Kafka), all of which can be done through external configuration items. You can even change the type of middleware at will without changing a single line of code.

Publish-Subscribe

Publish and Subscribe of messages are classic event-driven patterns. Spring Cloud Stream's data interaction is also based on this idea. The producer broadcasts the message through some topic (destinations in Spring Cloud Stream). Other microservices trigger business by subscribing to a specific topic to get broadcast messages.

This model greatly reduces the coupling between producers and consumers. Even if new applications are introduced, there is no need to destroy the overall structure of the current system.

Consumer Groups

"Group", a concept in Kafka. The meaning of this grouping concept of Spring Cloud Stream is basically the same as that of Kafka.

It is necessary to dynamically scale the number of the same application in microservices to achieve higher processing power. In this case, the same event prevents repeated consumption, and as long as these applications are placed in the same "group", you can guarantee that the message will be consumed only once by one of the applications.

Message

Message, the so-called message body, is used to carry the transmitted information. Message is divided into two parts, header and payload. Header is the header information, which is used to store some property parameters of the transmission. Payload is used to load data, and any Object object that it can carry can be transferred in binder to specify a different mini type.

You can set the mini type of input input and output output through application.yml

Spring.cloud.stream.bindings..content-type

MessageChannel

In the message pipeline, the producer produces a message to channel, and the consumer consumes a message from channel, so channel can decouple the message component and provide a convenient interception and monitoring function.

Default channel

Input (SubscribableChannel) and output channel (MessageChannel) reference Processor interface

Springcloudstream provides the definition of channels such as customization through the use of interfaces

Public interface OrderChannel {String INPUT = "input_order"; String OUTPUT= "ouput_order"; / * input Note defines the name of the channel. Configure the actual bound topic or subscription group of the channel in yml in the future * @ return * / @ Input (INPUT) SubscribableChannel orderInput (); / * * output note specifies the name of the output channel * @ return * / @ Output (OUTPUT) MessageChannel orderOutput ();}

The following code refers to the future configuration of the channel in yml using the Source Sink Processor interface.

Spring: cloud: stream: bindings: Channel name: destination: mydest, this is the end of the content of "how to use Spring Cloud Stream". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report