在SpringApplication初始化时有提到一个枚举类WebApplicationType,该枚举类有三个枚举值:
英语好的可以参考spring的官方文档:https://spring.io/reactive
Reactive Project
是Spring的reactive栈的非阻塞的基础,比如Spring WebFlux, Spring Data, 和 Spring Cloud Gateway。
Reactive processing 是一种能让开发人员构建非阻塞异步的程序来处理流控制的规范。
Reactive 系统可以更好地利用现代处理器。此外,在反应式编程中加入反压力确保了解耦组件之间更好的弹性。
开发人员从阻塞式开发转到非阻塞式开发的主要原因就是效率,Reactive编程则可以使用更少的资源做更多的事。
在spring中提供了两种并行的栈,一个是基于Servlet API,带有Spring MVC和Spring Data结构(也就是目前主流web项目用到的架构)。另一种是完全的Reactive栈,它利用了Spring WebFlux和Spring Data的响应式存储库。
git clone https://github.com/spring-guides/gs-reactive-rest-service.git
拉取demo,gs-reactive-rest-service/complete下面有写好的代码。
代码看起来确实和以往的SERVLET的不一样,看起来还不是很习惯。
Servlet服务:
hello.Application
Connected to the target VM, address: '127.0.0.1:9519', transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.1)
2022-11-11 11:06:20.504 INFO 16684 --- [ main] hello.Application : Starting Application using Java 1.8.0_171 on DESKTOP-UCCV5BE with PID 16684 (D:\workspace\java\gs-reactive-rest-service\ServletProject\target\classes started by 刘先生 in D:\workspace\java\gs-reactive-rest-service\ServletProject)
2022-11-11 11:06:20.512 INFO 16684 --- [ main] hello.Application : No active profile set, falling back to 1 default profile: "default"
2022-11-11 11:06:24.022 INFO 16684 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-11-11 11:06:24.046 INFO 16684 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-11-11 11:06:24.047 INFO 16684 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.64]
2022-11-11 11:06:24.502 INFO 16684 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-11-11 11:06:24.503 INFO 16684 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3813 ms
2022-11-11 11:06:26.391 INFO 16684 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-11-11 11:06:26.464 INFO 16684 --- [ main] hello.Application : Started Application in 7.029 seconds (JVM running for 9.601)
内嵌的tomcatWebServer
Reactive服务:
Connected to the target VM, address: '127.0.0.1:9567', transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.1)
2022-11-11 11:09:12.079 INFO 34068 --- [ main] hello.Application : Starting Application using Java 1.8.0_171 on DESKTOP-UCCV5BE with PID 34068 (D:\workspace\java\gs-reactive-rest-service\initial\target\classes started by 刘先生 in D:\workspace\java\gs-reactive-rest-service\initial)
2022-11-11 11:09:12.082 INFO 34068 --- [ main] hello.Application : No active profile set, falling back to 1 default profile: "default"
2022-11-11 11:09:13.963 INFO 34068 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port 8080
2022-11-11 11:09:13.971 INFO 34068 --- [ main] hello.Application : Started Application in 2.165 seconds (JVM running for 3.258)
内嵌的NettyWebServer
关于tomcatWebServer和NettyWebServer的探究,准备另开篇幅去研究