spring中的反应式编程

在看了spring-cloud-gateway部分代码后,发现很多地方用到了反应式编程,Mono和Flux到处可见。反应式本质上类似于发布订阅,事件驱动,类似的有epoll

//可以多次发射
Flux.create(t->{
    t.next("hello");
    t.next("world");
    t.complete();
}).subscribe(System.out::println);

//只能调用一次next
Flux.generate(t->{
    t.next("peter");
    t.complete();
}).subscribe(System.out::println);

Flux.just("1","2","3","4","5").subscribe(System.out::println);

Flux.from(Flux.just("1","2","3","4","5")).subscribe(System.out::println);

Flux.fromIterable(Lists.newArrayList("1", "2", "3", "4", "5")).subscribe(System.out::println);

Flux.fromStream(Stream.of("1", "2", "3", "4", "5")).subscribe(System.out::println);

Flux.fromArray(new String[]{"1", "2", "3", "4", "5"}).subscribe(System.out::println);

Flux.defer(()-> Flux.just("peter","jack","bob")).subscribe(System.out::println);

//不发射任何东西
Flux.empty().subscribe(System.out::println);

Flux.range(0,10).subscribe(System.out::println);

Flux.never().subscribe(System.out::println);

//创建一个Flux,它在订阅之后立即以指定的错误终止
Flux.error(new RuntimeException("运行错误")).subscribe(System.out::println);

//合并[a,c][b,d]
Flux.just("a", "b")
        .zipWith(Flux.just("c", "d"))
        .subscribe(System.out::println);

//合并a,b,c,d
Flux.just("a", "b")
        .mergeWith(Flux.just("c", "d"))
        .subscribe(System.out::println);

你可能感兴趣的:(spring中的反应式编程)