SpringCloud combat service in response to the decline tutorial series (Chapter IV)

Connect one:

SpringCloud combat service in response to the decline tutorial series (the first chapter)

SpringCloud combat service in response to the decline series of tutorials (Chapter II)

SpringCloud combat service in response to the decline tutorial series (Chapter III)

1.1.4 Reactor framework introduced

If you want to integrate Reactor framework code, you need to add the following Maven dependence, respectively, and introduced into Reactor core functionality for the associated tools to support testing.

<dependency>
    <groupid>io.projectreactor</groupid>
    <cartifactid>reactor-cores/artifactid>
</dependency>
<dependency>
   <groupid>io.projectreactor</groupid>
   <cartifactid>reactor-tests/artifactid>
   <scope>test</scope>
</dependency>

 

Reactor frame has its specific design in the realization of the responsive flow specification. This section first describes the sequence Reactor asynchronous data frame, and then introduce Flux Mono two core components.

1. Reactor asynchronous data sequence

When a developing Reactor responsive applications, regardless of operator use, will receive asynchronous data a sequence shown in FIG.

SpringCloud combat service in response to the decline tutorial series (Chapter IV)

onNext x 0..N [onError |onComplete]

Calling the above formula comprising three different types of methods, notification messages are processed under different scenarios.

  • onNext (): normal notification message containing element.

  • onCompleted (): end of the sequence notification message, can not.

  • onError (): the sequence notification message error, there may be no.

According responsive flow specification, when the notification message is generated, these three methods asynchronous sequence corresponding subscriber is called. If the sequence is not wrong, then onerror () method is never called; if you do not call onComplete () method, you get an infinite sequence of asynchronous. Typically, asynchronous infinite sequence should only be used for testing and other special scenarios.

2.Flux components

Flux representative of asynchronous sequence 0 to N elements, as shown, are three kinds of messages for notification sequence Flux.

SpringCloud combat service in response to the decline tutorial series (Chapter IV)

The following sample code shows how to use the components in Fux specific projects. If we understand the micro-service architecture based Hystrix I service fallback (Fallback) mechanism, they should know the code getordersfallbacko is a typical fallback function, we constructed a method by FIux.fromlterableo  Flux<Order>object returned as a fallback function value.

About service fallback mechanism will be specifically described in the following sections

private Flux<order> getordersfallback(){
    List<order> fallbacklist = new Arraylist<>();
    Order order= new Order();
    order.setid("orderinvalidid");
    order.setaccountid("Invalidid");
    order.setitem("Order list is not available");
    order.setcreatetime (new Date();
    fallbacklist.add (order);
    return Flux.fromiterable (fallbacklist);
}

 

The following examples more readily understood that, as seen from the annotation @ Getmapping method name is located, which is the Controller endpoint, returns a list of objects for the Order. Here to return to the Order List by the same Flux< Order>render objects.

@getmapping("/vl/orders")
public Flux<Order> getorderlist(){
    Flux<order> orders= orderservice. getorders();
    return orders;
}

 

3.Mono 组件

In the Reactor, Mono represents 0 or asynchronous sequence comprises an element, as shown, the sequence may also contain the same three types of Fux message notification.

Please note, Mono can also be used to indicate an empty asynchronous sequence that no elements, containing only the concept of the end of the sequence (similar to Java in the Runnable). We can Mono<void>represent an empty asynchronous sequence.

SpringCloud combat service in response to the decline tutorial series (Chapter IV)

Like FIux components by service fallback to demonstrate the use of Mono components, sample code below.

private Mono<order> getorderfallback(){
    Order order = new Order();
    order.setid("orderinvalidid");
    order.setaccountid("Invalidid");
    order.setitem("Order list is not available");
    order.setcreatetime(new Date());
    return Mono.just(order);
}

 

这里首构建一个 Order对象,然后通过 Mono.just()方法返回一个Mono对象。

Controller层组件也是一样的,通过d获取Mono<Order>对象的端点示例如下。

@tapping("/vl/orders/{id}")
public Mono<order> getorder(@Pathvariable String id){
    Mono<order> order orderservice.getorderbyid(id);
    return order;
}

 

Compared Mono, Flux is a more versatile responsive components, so the richer for FIux of operations than Mono. On the other hand, can be converted each other FIux and Mono. For example, the two sequences Mono Flux combined to get a sequence, while the sequence FIux a counting operation is obtained Mono object.

Guess you like

Origin www.cnblogs.com/javazhiyin/p/11423057.html