专业的JAVA编程教程与资源

网站首页 > java教程 正文

Java,SpringBoot,WebFlux,基于Reactive Streams响应式编程

temp10 2024-12-03 18:32:16 java教程 16 ℃ 0 评论

WebFlux

Spring5,响应式编程,新增加的基于Reactive Streams的Spring WebFlux框架,使用WebFlux模块来构建:异步的、非堵塞的、事件驱动的服务,其在伸缩性方面表现非常好。

Spring Boot Webflux是基于Reactor实现的,Spring Boot 2.0包括一个新的spring-webflux模块,该模块包含对响应式HTTP和WebSocket客户端的支持,以及对REST、HTML和WebSocket交互等程序的支持,一般来说,Spring MVC用于同步处理,Spring Webflux用于异步处理。

Java,SpringBoot,WebFlux,基于Reactive Streams响应式编程

地址:https://start.spring.io/,https://docs.spring.io/spring-framework/docs/5.2.8.RELEASE/spring-framework-reference/web-reactive.html

Spring WebFlux功能模块

1、Router Functions

对标准的@Controller,@RequestMapping等的Spring MVC注解,提供一套函数式风格的API,用于创建Router、Handler和Filter。

2、WebFlux

核心组件,协调上下游各个组件提供 响应式编程 支持。

3、Reactive Streams

一种支持背压 (Backpressure) 的异步数据流处理标准,主流实现有RxJava和Reactor,Spring WebFlux集成的是Reactor。

Flux & Mono

Mono,A Reactive Streams Publisher with basic rx operators that completes successfully by emitting an element, or with an error.

Mono是响应流 Publisher ,即要么成功发布元素,要么错误。

Flux,A Reactive Streams Publisher with rx operators that emits 0 to N elements, and then completes (successfully or with an error).

Flux是响应流 Publisher ,即要么成功发布 0 到 N 个元素,要么错误。Flux 其实是 Mono 的一个补充。

Flux和Mono属于事件发布者,类似于生产者,对消费者提供订阅接口,当有事件发生的时候,Flux或Mono会回调消费者相应的方法来通知消费者相应的事件。

Mono

1、0-1的非阻塞结果;

2、Reactive Streams JVM API Publisher;

3、非阻塞Optional;

Flux

1、0-N的非阻塞序列;

2、Reactive Streams JVM API Publisher;

3、非阻塞Stream;

Mono常用的方法有:

1、Mono.create():使用 MonoSink 来创建 Mono

2、Mono.justOrEmpty():从一个 Optional 对象或 null 对象中创建 Mono。

3、Mono.error():创建一个只包含错误消息的 Mono

4、Mono.never():创建一个不包含任何消息通知的 Mono

5、Mono.delay():在指定的延迟时间之后,创建一个 Mono,产生数字 0 作为唯一值.

快速上手

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

处理器类Handler:

package com.what21.demo.handler;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@Component
public class CityHandler {

    public Mono<ServerResponse> helloCity(ServerRequest request) {
        return ServerResponse.ok()
                .contentType(MediaType.TEXT_PLAIN)
                .body(BodyInserters.fromObject("Hello, City!"));
    }

}

路由器类Router:

package com.what21.demo.router;

import com.what21.demo.handler.CityHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

@Configuration
public class CityRouter {

    @Bean
    public RouterFunction<ServerResponse> routeCity(CityHandler cityHandler) {
        return RouterFunctions
                .route(RequestPredicates.GET("/hello")
                        .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), cityHandler::helloCity);
    }

}

启动类:

package com.what21.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

访问地址:

http://127.0.0.1:8080/hello

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表