嵌入式web容器
支持三种容器:Tomcat Jetty Undertow
springboot2.x的重大更新之一,就是支持响应式web server(1.x只支持servlet web容器)
一、 更改容器配置
方式1:
在application.properties中设置,形如:
server.port=8090
#localhost:8090/duing
server.servlet.context-path=/duing
server.tomcat.max-connections=5000
方式2:
创建自定义器,实现WebServerFactoryCustomizer的customize方法,可以设置端口号等 (优先级高于配置文件)
package com.duing.config;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebConfig {
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> customizer(){
return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8899);
}
};
}
}
二、更改使用容器
从starter-web中移除starter-tomcat,引入新的容器启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
三、使用响应式web容器
去掉对starter-web的引用,增加starter-webflux(两者同时存在时,还会使用servlet容器)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
编写一个响应式编程的demo
package com.duing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
@SpringBootApplication
public class WebContainerApplication {
public static void main(String[] args) {
SpringApplication.run(WebContainerApplication.class, args);
}
@Bean
public RouterFunction<ServerResponse> hello(){
return route(GET("/hello"),
serverRequest -> ok().body(
Mono.just("Hello world"),String.class));
}
}
四、查看web应用启动类
方式一
通过WebServerApplicationContext容器获取类名,在容器启动后回调使用 (当前应用为web应用时可用)
@Bean
public ApplicationRunner runner(WebServerApplicationContext context){
return args -> {
System.out.println("当前容器的实现类为:"+context.getWebServer().getClass().getName());
};
}
方式二
通过监听WebServerInitializedEvent初始化事件,在容器启动前触发
package com.duing.config;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class ListenerConfig {
@EventListener(WebServerInitializedEvent.class)
public void onWebServerReady(WebServerInitializedEvent event){
System.out.println("当前容器的实现类是:"+
event.getWebServer().getClass().getName());
}
}