java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring WebFlux

Spring 零基础入门WebFlux框架体系

作者:xharvard

Spring5发布有两年了,随Spring5一起发布了一个和Spring WebMvc同级的Spring WebFlux。这是一个支持反应式编程模型的新框架体系。反应式模型区别于传统的MVC最大的不同是异步的、事件驱动的、非阻塞的,这使得应用程序的并发性能会大大提高,单位时间能够处理更多的请求

传统的基于Servlet的Web框架,如Spring MVC,在本质上都是阻塞和多线程的,每个连接都会使用一个线程。在请求处理的时候,会在线程池中拉取一个工作者( worker )线程来对请求进行处理。同时,请求线程是阻塞的,直到工作者线程提示它已经完成为止。

首先webflux不是spring mvc的替代方案!webflux强调的是异步非阻塞,spring mvc强调的是同步阻塞,如果方案大部分偏向于非同步,则spring webflux才是首选。另外,如果依赖了大量阻塞式持久化 API 和网络 API,建议使⽤ Spring MVC。

WebMVC:springMvc构建在servlet api之上,因此需要Servlet容器(比如Tomcat)才能执行。使用的是同步阻塞式IO模型,每一个请求对应一个线程去处理。

WebFlux:spring webflux是一个异步非阻塞式的web框架,它能够充分利用多核cpu的硬件资源去处理大量的并发请求。

相同之处

都可以使用springmvc注解,如@Controller,方便我们在两个web框架中自由转换;

都可以使用tomcat,jetty,undertow, servlet容器(servlet 3.1+)

注意点:

spring mvc因为是使用的同步阻塞式,更方便开发人员编写功能,debug测试等,一般来说,如果spring mvc能够满足的场景,就尽量不要用webflux;

使用WebFlux有一个很有 意思的副作用, 即WebFlux 的默认嵌人式服务器是Netty而不是Tomcat。Netty 是一个异步 、事件驱动的服务器,非常适合Spring WebFlux这样的反应式Web框架。

Spring WebFlux的控制器方法要接受和返回反应式类型,如Mono和Flux,而不是领域类型和集合。

SpringWebflux 实现方式有两种:注解编程模型函数式编程模型

基于注解编程模型

该模型与Spring MVC很相似。甚至注解都一样。

搭建一个简易的Spring WebFlux工程:

1,到https://start.spring.io/创建spring boot工程,选择Spring Reactive Web 依赖。

2,导入到IDEA,创建controller, service, entity 等包

3,创建entity

public class User {
    private int id;
    private String name;
    public User() {
    }
    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

4,创建service 这里使用map模拟数据库操作

public interface UserService {
    Mono<User> selectById(int id);
    Flux<User> selectAllUsers();
    Mono<Void> insertUser(Mono<User> user);
}
@Repository
public class UserServiceImpl implements UserService {
    private static Map<Integer, User> users = new HashMap<>();
    public UserServiceImpl() {
        users.put(1, new User(1, "刘备"));
        users.put(2, new User(2, "曹操"));
        users.put(3, new User(3, "孙权"));
    }
    @Override
    public Mono<User> selectById(int id) {
        return Mono.just(users.get(id));
    }
    @Override
    public Flux<User> selectAllUsers() {
        return Flux.fromIterable(users.values());
    }
    @Override
    public Mono<Void> insertUser(Mono<User> userMono) {
        return userMono.doOnNext(user -> {
            int id = users.size() + 1;
            users.put(id, user);
        }).thenEmpty(Mono.empty());
    }
}

5,创建controller

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/users/{id}")
    public Mono<User> getUser(@PathVariable int id) {
        return userService.selectById(id);
    }
    @GetMapping("/users")
    public Flux<User> getAllUsers() {
        return userService.selectAllUsers();
    }
}

6,启动 SringBoot,这里修改了端口号为9999

"D:\Program Files\java\tsjdk64\bin\java.exe" -Dvisualvm.id=31217450973500 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.2\lib\idea_rt.jar=64346:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\java\tsjdk64\jre\lib\charsets.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\access-bridge-64.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\cldrdata.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\dnsns.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\jaccess.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\localedata.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\nashorn.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\sunec.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\sunjce_provider.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\sunmscapi.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\sunpkcs11.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\zipfs.jar;D:\Program Files\java\tsjdk64\jre\lib\jce.jar;D:\Program Files\java\tsjdk64\jre\lib\jfr.jar;D:\Program Files\java\tsjdk64\jre\lib\jsse.jar;D:\Program Files\java\tsjdk64\jre\lib\management-agent.jar;D:\Program Files\java\tsjdk64\jre\lib\resources.jar;D:\Program Files\java\tsjdk64\jre\lib\rt.jar;D:\05_spring_dev\java-study\spring-webflux\target\classes;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-webflux\2.7.1\spring-boot-starter-webflux-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter\2.7.1\spring-boot-starter-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot\2.7.1\spring-boot-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-context\5.3.21\spring-context-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-aop\5.3.21\spring-aop-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-expression\5.3.21\spring-expression-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-autoconfigure\2.7.1\spring-boot-autoconfigure-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-logging\2.7.1\spring-boot-starter-logging-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\Program Files\apache-maven-3.6.3\repo\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\Program Files\apache-maven-3.6.3\repo\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\Program Files\apache-maven-3.6.3\repo\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\Program Files\apache-maven-3.6.3\repo\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\Program Files\apache-maven-3.6.3\repo\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\Program Files\apache-maven-3.6.3\repo\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-json\2.7.1\spring-boot-starter-json-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\com\fasterxml\jackson\core\jackson-databind\2.13.3\jackson-databind-2.13.3.jar;D:\Program Files\apache-maven-3.6.3\repo\com\fasterxml\jackson\core\jackson-annotations\2.13.3\jackson-annotations-2.13.3.jar;D:\Program Files\apache-maven-3.6.3\repo\com\fasterxml\jackson\core\jackson-core\2.13.3\jackson-core-2.13.3.jar;D:\Program Files\apache-maven-3.6.3\repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.3\jackson-datatype-jdk8-2.13.3.jar;D:\Program Files\apache-maven-3.6.3\repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.3\jackson-datatype-jsr310-2.13.3.jar;D:\Program Files\apache-maven-3.6.3\repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.3\jackson-module-parameter-names-2.13.3.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-reactor-netty\2.7.1\spring-boot-starter-reactor-netty-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\io\projectreactor\netty\reactor-netty-http\1.0.20\reactor-netty-http-1.0.20.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-codec-http\4.1.78.Final\netty-codec-http-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-common\4.1.78.Final\netty-common-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-buffer\4.1.78.Final\netty-buffer-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-transport\4.1.78.Final\netty-transport-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-codec\4.1.78.Final\netty-codec-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-handler\4.1.78.Final\netty-handler-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-codec-http2\4.1.78.Final\netty-codec-http2-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-resolver-dns\4.1.78.Final\netty-resolver-dns-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-resolver\4.1.78.Final\netty-resolver-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-codec-dns\4.1.78.Final\netty-codec-dns-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-resolver-dns-native-macos\4.1.78.Final\netty-resolver-dns-native-macos-4.1.78.Final-osx-x86_64.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-resolver-dns-classes-macos\4.1.78.Final\netty-resolver-dns-classes-macos-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-transport-native-epoll\4.1.78.Final\netty-transport-native-epoll-4.1.78.Final-linux-x86_64.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-transport-native-unix-common\4.1.78.Final\netty-transport-native-unix-common-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-transport-classes-epoll\4.1.78.Final\netty-transport-classes-epoll-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\projectreactor\netty\reactor-netty-core\1.0.20\reactor-netty-core-1.0.20.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-handler-proxy\4.1.78.Final\netty-handler-proxy-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-codec-socks\4.1.78.Final\netty-codec-socks-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-web\5.3.21\spring-web-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-beans\5.3.21\spring-beans-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-webflux\5.3.21\spring-webflux-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-core\5.3.21\spring-core-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-jcl\5.3.21\spring-jcl-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\io\projectreactor\reactor-core\3.4.19\reactor-core-3.4.19.jar;D:\Program Files\apache-maven-3.6.3\repo\org\reactivestreams\reactive-streams\1.0.4\reactive-streams-1.0.4.jar" org.xharavard.webflux.WebfluxApplication
 
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.1)
 
2022-07-14 17:05:21.976  INFO 10060 --- [           main] o.xharavard.webflux.WebfluxApplication   : Starting WebfluxApplication using Java 1.8.0-taishan on LAPTOP-N89I5VOK with PID 10060 (D:\05_spring_dev\java-study\spring-webflux\target\classes started by xhh in D:\05_spring_dev\java-study)
2022-07-14 17:05:21.978  INFO 10060 --- [           main] o.xharavard.webflux.WebfluxApplication   : No active profile set, falling back to 1 default profile: "default"
2022-07-14 17:05:23.635  INFO 10060 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 9999
2022-07-14 17:05:23.640  INFO 10060 --- [           main] o.xharavard.webflux.WebfluxApplication   : Started WebfluxApplication in 2.045 seconds (JVM running for 2.394)

7,访问

浏览器输入:http://localhost:9999/users/1

返回 {"id":1,"name":"刘备"}

函数式编程模型

(1)在使用函数式编程模型操作时候,需要自己初始化服务器

(2)基于函数式编程模型时候,有两个核心接口:RouterFunction(实现路由功能,请求转发给对应的 handler)和 HandlerFunction(处理请求生成响应的函数)。核心任务定义两个函数式接口的实现并且启动需要的服务器。

(3)Spring WebFlux 请 求 和 响 应 不 再 是 ServletRequest 和 ServletResponse ,而是ServerRequest 和 ServerResponse

1,entity和service同之前一样

2,创建Handler

public class UserHandler {
    @Autowired
    private final UserService userService;
    public UserHandler(UserService userService) {
        this.userService = userService;
    }
    public Mono<ServerResponse> getUserById(ServerRequest request) {
        int userId = Integer.parseInt(request.pathVariable("id"));
        //空值处理
        Mono<ServerResponse> notFount = ServerResponse.notFound().build();
        Mono<User> userMono = this.userService.selectById(userId);
        return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(userMono, User.class)
            .switchIfEmpty(notFount);
    }
    public Mono<ServerResponse> getAllUsers(ServerRequest request) {
        Flux<User> users = this.userService.selectAllUsers();
        return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(users, User.class);
    }
}

3,初始化服务器,编写 Router

public class Server {
    public static void main(String[] args) throws IOException {
        Server server = new Server();
        server.createReactorServer();
        System.out.println("enter to exit");
        System.in.read();
    }
    // 创建路由
    public RouterFunction<ServerResponse> routingFunction() {
        UserService userService = new UserServiceImpl();
        UserHandler userHandler = new UserHandler(userService);
 
        return RouterFunctions
            .route(GET("/users/{id}").and(accept(MediaType.APPLICATION_JSON)), userHandler::getUserById)
            .andRoute(GET("/users").and(accept(MediaType.APPLICATION_JSON)), userHandler::getAllUsers);
    }
    // 创建服务器完成适配
    public void createReactorServer() {
        //路由和Handler适配
        RouterFunction<ServerResponse> route = routingFunction();
        HttpHandler httpHandler = toHttpHandler(route);
        ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
        //创建服务器
        HttpServer httpServer = HttpServer.create();
        httpServer.handle(adapter).bindNow();
    }
}

启动上述程序

"D:\Program Files\java\tsjdk64\bin\java.exe" -Dvisualvm.id=32992872873300 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.2\lib\idea_rt.jar=65106:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\java\tsjdk64\jre\lib\charsets.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\access-bridge-64.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\cldrdata.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\dnsns.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\jaccess.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\localedata.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\nashorn.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\sunec.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\sunjce_provider.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\sunmscapi.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\sunpkcs11.jar;D:\Program Files\java\tsjdk64\jre\lib\ext\zipfs.jar;D:\Program Files\java\tsjdk64\jre\lib\jce.jar;D:\Program Files\java\tsjdk64\jre\lib\jfr.jar;D:\Program Files\java\tsjdk64\jre\lib\jsse.jar;D:\Program Files\java\tsjdk64\jre\lib\management-agent.jar;D:\Program Files\java\tsjdk64\jre\lib\resources.jar;D:\Program Files\java\tsjdk64\jre\lib\rt.jar;D:\05_spring_dev\java-study\spring-webflux\target\classes;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-webflux\2.7.1\spring-boot-starter-webflux-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter\2.7.1\spring-boot-starter-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot\2.7.1\spring-boot-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-context\5.3.21\spring-context-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-aop\5.3.21\spring-aop-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-expression\5.3.21\spring-expression-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-autoconfigure\2.7.1\spring-boot-autoconfigure-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-logging\2.7.1\spring-boot-starter-logging-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\Program Files\apache-maven-3.6.3\repo\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\Program Files\apache-maven-3.6.3\repo\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\Program Files\apache-maven-3.6.3\repo\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\Program Files\apache-maven-3.6.3\repo\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\Program Files\apache-maven-3.6.3\repo\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\Program Files\apache-maven-3.6.3\repo\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-json\2.7.1\spring-boot-starter-json-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\com\fasterxml\jackson\core\jackson-databind\2.13.3\jackson-databind-2.13.3.jar;D:\Program Files\apache-maven-3.6.3\repo\com\fasterxml\jackson\core\jackson-annotations\2.13.3\jackson-annotations-2.13.3.jar;D:\Program Files\apache-maven-3.6.3\repo\com\fasterxml\jackson\core\jackson-core\2.13.3\jackson-core-2.13.3.jar;D:\Program Files\apache-maven-3.6.3\repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.3\jackson-datatype-jdk8-2.13.3.jar;D:\Program Files\apache-maven-3.6.3\repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.3\jackson-datatype-jsr310-2.13.3.jar;D:\Program Files\apache-maven-3.6.3\repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.3\jackson-module-parameter-names-2.13.3.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-reactor-netty\2.7.1\spring-boot-starter-reactor-netty-2.7.1.jar;D:\Program Files\apache-maven-3.6.3\repo\io\projectreactor\netty\reactor-netty-http\1.0.20\reactor-netty-http-1.0.20.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-codec-http\4.1.78.Final\netty-codec-http-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-common\4.1.78.Final\netty-common-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-buffer\4.1.78.Final\netty-buffer-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-transport\4.1.78.Final\netty-transport-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-codec\4.1.78.Final\netty-codec-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-handler\4.1.78.Final\netty-handler-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-codec-http2\4.1.78.Final\netty-codec-http2-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-resolver-dns\4.1.78.Final\netty-resolver-dns-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-resolver\4.1.78.Final\netty-resolver-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-codec-dns\4.1.78.Final\netty-codec-dns-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-resolver-dns-native-macos\4.1.78.Final\netty-resolver-dns-native-macos-4.1.78.Final-osx-x86_64.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-resolver-dns-classes-macos\4.1.78.Final\netty-resolver-dns-classes-macos-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-transport-native-epoll\4.1.78.Final\netty-transport-native-epoll-4.1.78.Final-linux-x86_64.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-transport-native-unix-common\4.1.78.Final\netty-transport-native-unix-common-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-transport-classes-epoll\4.1.78.Final\netty-transport-classes-epoll-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\projectreactor\netty\reactor-netty-core\1.0.20\reactor-netty-core-1.0.20.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-handler-proxy\4.1.78.Final\netty-handler-proxy-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\io\netty\netty-codec-socks\4.1.78.Final\netty-codec-socks-4.1.78.Final.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-web\5.3.21\spring-web-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-beans\5.3.21\spring-beans-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-webflux\5.3.21\spring-webflux-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-core\5.3.21\spring-core-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\org\springframework\spring-jcl\5.3.21\spring-jcl-5.3.21.jar;D:\Program Files\apache-maven-3.6.3\repo\io\projectreactor\reactor-core\3.4.19\reactor-core-3.4.19.jar;D:\Program Files\apache-maven-3.6.3\repo\org\reactivestreams\reactive-streams\1.0.4\reactive-streams-1.0.4.jar" org.xharavard.webflux.Server
17:34:57.051 [main] DEBUG reactor.util.Loggers - Using Slf4j logging framework
17:34:57.299 [main] DEBUG org.springframework.web.server.adapter.HttpWebHandlerAdapter - enableLoggingRequestDetails='false': form data and headers will be masked to prevent unsafe logging of potentially sensitive data
17:34:57.331 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
17:34:57.333 [main] DEBUG io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false
17:34:57.333 [main] DEBUG io.netty.util.internal.PlatformDependent0 - Java version: 8
17:34:57.334 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
17:34:57.334 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
17:34:57.334 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.storeFence: available
17:34:57.335 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
17:34:57.335 [main] DEBUG io.netty.util.internal.PlatformDependent0 - direct buffer constructor: available
17:34:57.341 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
17:34:57.341 [main] DEBUG io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable prior to Java9
17:34:57.341 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available
17:34:57.341 [main] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available
17:34:57.342 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\xhh\AppData\Local\Temp (java.io.tmpdir)
17:34:57.342 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
17:34:57.342 [main] DEBUG io.netty.util.internal.PlatformDependent - Platform: Windows
17:34:57.343 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: 3787980800 bytes
17:34:57.343 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1
17:34:57.344 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available
17:34:57.344 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
17:34:57.352 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
17:34:57.352 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
17:34:57.375 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
17:34:57.375 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4
17:34:57.400 [main] DEBUG reactor.netty.tcp.TcpResources - [http] resources will use the default LoopResources: DefaultLoopResources {prefix=reactor-http, daemon=true, selectCount=8, workerCount=8}
17:34:57.400 [main] DEBUG reactor.netty.tcp.TcpResources - [http] resources will use the default ConnectionProvider: reactor.netty.resources.DefaultPooledConnectionProvider@65f095f8
17:34:57.401 [main] DEBUG reactor.netty.resources.DefaultLoopIOUring - Default io_uring support : false
17:34:57.654 [main] DEBUG reactor.netty.resources.DefaultLoopEpoll - Default Epoll support : false
17:34:57.655 [main] DEBUG reactor.netty.resources.DefaultLoopKQueue - Default KQueue support : false
17:34:57.660 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 16
17:34:57.678 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
17:34:57.678 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
17:34:57.686 [main] DEBUG io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
17:34:57.707 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 8008 (auto-detected)
17:34:57.709 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
17:34:57.709 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false
17:34:57.851 [main] DEBUG io.netty.util.NetUtilInitializations - Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1)
17:34:57.852 [main] DEBUG io.netty.util.NetUtil - Failed to get SOMAXCONN from sysctl and file \proc\sys\net\core\somaxconn. Default: 200
17:34:57.990 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: 8c:c6:81:ff:fe:4c:01:12 (auto-detected)
17:34:58.005 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 16
17:34:58.005 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 16
17:34:58.005 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192
17:34:58.005 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 9
17:34:58.005 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 4194304
17:34:58.005 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256
17:34:58.005 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64
17:34:58.005 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
17:34:58.005 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192
17:34:58.005 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimIntervalMillis: 0
17:34:58.005 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: false
17:34:58.005 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedByteBuffersPerChunk: 1023
17:34:58.010 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
17:34:58.010 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
17:34:58.010 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
17:34:58.077 [reactor-http-nio-1] DEBUG reactor.netty.transport.ServerTransport - [c9915a48, L:/0:0:0:0:0:0:0:0:65129] Bound new server
enter to exit
17:35:05.073 [reactor-http-nio-2] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkAccessible: true
17:35:05.074 [reactor-http-nio-2] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkBounds: true
17:35:05.074 [reactor-http-nio-2] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@301be937
17:35:05.102 [reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServerOperations - [09cda6a3, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] New http connection, requesting read
17:35:05.102 [reactor-http-nio-3] DEBUG reactor.netty.http.server.HttpServerOperations - [1a0030eb, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65131] New http connection, requesting read
17:35:05.102 [reactor-http-nio-3] DEBUG reactor.netty.transport.TransportConfig - [1a0030eb, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65131] Initialized pipeline DefaultChannelPipeline{(reactor.left.httpCodec = io.netty.handler.codec.http.HttpServerCodec), (reactor.left.httpTrafficHandler = reactor.netty.http.server.HttpTrafficHandler), (reactor.right.reactiveBridge = reactor.netty.channel.ChannelOperationsHandler)}
17:35:05.102 [reactor-http-nio-2] DEBUG reactor.netty.transport.TransportConfig - [09cda6a3, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] Initialized pipeline DefaultChannelPipeline{(reactor.left.httpCodec = io.netty.handler.codec.http.HttpServerCodec), (reactor.left.httpTrafficHandler = reactor.netty.http.server.HttpTrafficHandler), (reactor.right.reactiveBridge = reactor.netty.channel.ChannelOperationsHandler)}
17:35:05.106 [reactor-http-nio-2] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
17:35:05.106 [reactor-http-nio-2] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
17:35:05.106 [reactor-http-nio-2] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.chunkSize: 32
17:35:05.106 [reactor-http-nio-2] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.blocking: false
17:35:05.119 [reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServerOperations - [09cda6a3, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] Increasing pending responses, now 1
17:35:05.124 [reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [09cda6a3-1, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] Handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@3bc10f7a
17:35:05.138 [reactor-http-nio-2] DEBUG org.springframework.web.server.adapter.HttpWebHandlerAdapter - [09cda6a3-1] HTTP GET "/users/1"
17:35:05.238 [reactor-http-nio-2] DEBUG org.springframework.http.codec.json.Jackson2JsonEncoder - [09cda6a3-1] Encoding [org.xharavard.webflux.entity.User@39412266]
17:35:05.252 [reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServerOperations - [09cda6a3-1, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] Decreasing pending responses, now 0
17:35:05.252 [reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServerOperations - [09cda6a3-1, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] Last HTTP packet was sent, terminating the channel
17:35:05.253 [reactor-http-nio-2] DEBUG org.springframework.web.server.adapter.HttpWebHandlerAdapter - [09cda6a3-1] Completed 200 OK
17:35:05.256 [reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServerOperations - [09cda6a3-1, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] Last HTTP response frame
17:35:05.488 [reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServerOperations - [09cda6a3, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] Increasing pending responses, now 1
17:35:05.488 [reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [09cda6a3-2, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] Handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@3bc10f7a
17:35:05.488 [reactor-http-nio-2] DEBUG org.springframework.web.server.adapter.HttpWebHandlerAdapter - [09cda6a3-2] HTTP GET "/favicon.ico"
17:35:05.494 [reactor-http-nio-2] DEBUG org.springframework.web.server.handler.ResponseStatusExceptionHandler - [09cda6a3-2] Resolved [ResponseStatusException: "404 NOT_FOUND "No matching router function""] for HTTP GET /favicon.ico
17:35:05.494 [reactor-http-nio-2] DEBUG org.springframework.web.server.adapter.HttpWebHandlerAdapter - [09cda6a3-2] Completed 404 NOT_FOUND
17:35:05.494 [reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServerOperations - [09cda6a3-2, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] Last HTTP response frame
17:35:05.494 [reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServerOperations - [09cda6a3-2, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] No sendHeaders() called before complete, sending zero-length header
17:35:05.494 [reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServerOperations - [09cda6a3-2, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] Decreasing pending responses, now 0
17:35:05.494 [reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServerOperations - [09cda6a3-2, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] Last HTTP packet was sent, terminating the channel
17:35:05.495 [reactor-http-nio-2] DEBUG reactor.netty.channel.ChannelOperationsHandler - [09cda6a3, L:/0:0:0:0:0:0:0:1:65129 - R:/0:0:0:0:0:0:0:1:65130] No ChannelOperation attached. Dropping: EmptyLastHttpContent

浏览器输入:http://localhost:65129/users

返回[{"id":1,"name":"刘备"},{"id":2,"name":"曹操"},{"id":3,"name":"孙权"}]

到此这篇关于Spring 零基础入门WebFlux框架体系的文章就介绍到这了,更多相关Spring WebFlux内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文