编程知识 cdmana.com

Functional and reactive programming features of spring cloud stream- spring.io

Spring Cloud Stream(SCSt) The functional and reactive expressions of Reactive Programming brings less code 、 Less configuration . however , most important of all , Your code is completely separate , And with SCSt It has nothing to do with the internal structure of .

Although all the functions described below are SCSt The dependencies of Spring Cloud Function(SCF) Of functional, But in understanding SCSt Additional meaning of function in context , You have to be aware of some nuances .

supplier , Function and consumer

type Supplier, Function, or Consumer Any of the bean Or can be mapped to any of them Bean Can be SCST Think of it as a message handler (Function or Consumer), Or as a source of information (Supplier), Depending on the type of function strategy used , Use <function-name>-<in/out>-<index> Naming conventions automatically generate input and output bindings .

Take a look at the following case :

@SpringBootApplication
public class SampleApplication  {
    @Bean
    public Function<String, String> uppercase() {
        return value -> value.toUpperCase();
    }
}

The function here is treated as a message handler , The message handler consumes... From the binding uppercase-in-0, The results are sent to the bound uppercase-out-0. The rest can be used as before . for example

--spring.cloud.stream.bindings.uppercase-in-0.content-type=text/plain.

single click here Learn more about details and configuration options .

Imperative or reactive

Functions can be imperative or reactive . Imperative functions are triggered on each individual event , The reaction function is triggered only once , Pass a reference to Project Reactor Provides an abstraction of the entire event flow ( for example Flux and Mono).

imperative :

@SpringBootApplication
public class SampleApplication  {
    @Bean
    public Function<String, String> uppercase() {
        return value -> value.toUpperCase();
    }
}

Reactivity :

@SpringBootApplication
public class SampleApplication  {
    @Bean
    public Function<Flux<String>, Flux<String>> uppercase() {
        return flux -> flux.map(value -> value.toUpperCase());
    }
}

In addition to providing you with different ( Single case ) Programming style to handle events , Reactive programming also adds value to some use cases , Otherwise, the implementation will be very complicated . Although it's beyond the scope of this article to discuss these use cases or response patterns , But it's still worth mentioning that state management use cases ( For example, window , Aggregation and grouping ) And split flows or use cases . Merging multiple streams , This will be discussed in the next section .

About the binding and naming rules of reactive functions , They are the same as described in the previous section .

Be careful : Although the previous example is just Function As an example , But the same rules apply to Supplier and Consumer. User guide's Spring Cloud Function Support Part and Reactor file Provides more details .

Functionality

Sometimes it's necessary to classify and organize data streams . for example , Consider a classic big data use case , That is to say, processing includes “ Order ” and “ invoice ” Unorganized data of , And you want each one to go into a separate data store . Function here to support ( It has multiple input and output functions ) Support for .

Let's look at an example of such a function ( here Provide complete implementation details ):

@Bean
public Function<Flux<Integer>, Tuple2<Flux<String>, Flux<String>>> organise() {
    return flux -> ...;
}

Whereas Project Reactor yes SCF Core dependencies of , We will use it Tuple library .Tuple Convey to us cardinality And type information, which brings us unique advantages . Both for SCSt All of them are extremely important for us . Cardinality lets us know how many input and output bindings we need to create and bind to the corresponding input and output of the function . Knowing the type information ensures the correct type conversion .

Again , This is the binding name naming convention “ Indexes ” Part of what works , Because in this function , The two output binding names are organise-out-0 and organise-out-1.

Important note : at present , Targeted only Function<TupleN<Flux<?>...>, TupleN<Flux<?>...>> Reactivity functions centered on complex event processing () Support functional Unity , In complex event processing , The evaluation and calculation of event fusion usually need to look at the event flow , Not a single event .

About the latest information , Please read... In the user's Guide “  Functions with multiple input and output parameters ” part

                   

版权声明
本文为[Jiedao jdon]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/04/20210408115659523h.html

Scroll to Top