Project Reactor:将两个 Mono<Integer> 相加而不阻塞

Project Reactor: Sum two Mono<Integer> without blocking

假设我们有:

Mono<Integer> int1 = Mono.just(1)Mono<Integer> int2 = Mono.just(10)。我想在不阻塞的情况下得到这两个整数的总和。

在阻塞方式中我会这样做: Mono<Integer> result = Mono.just(int1.block() + int2.block())

提前致谢!

尝试 zip:

Mono<Integer> int1 = Mono.just(1); //or any other mono, including lazy one
Mono<Integer> int2 = Mono.just(10); //same


Mono<Integer> sum = Mono.zip(
    int1, int2,
    //zip defaults to producing a Tuple2
    //but for the 2 args version you can provide a BiFunction
    (a, b) -> a + b
);

//if you want to verify, eg. in a test:
StepVerifier.create(sum)
    .expectNext(11)
    .verifyComplete();