一、切换嵌入式 Servlet 容器
默认的 WebServer 有:Tomcat、Jetty、Undertow;如果需要切换 Web 服务器,只需要排除默认的 Tomcat 依赖,添加对应要切换的 WebServer 的依赖即可;例如:我们切换 Tomcat 为 Undertow 的话,只需要在 pom.xml 配置文件中设置并添加如下依赖即可
<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-undertow</artifactId>
</dependency>
二、自定义 WebServer 配置
默认情况下,我们通过在 application.properties 配置文件中添加 server.xxx 配置即可修改 WebServer 的默认配置;如果要自定义修改配置,可参考如下实现方法
新建 CustomServer.class 并实现 WebServerFactoryCustomizer<ConfigurableWebServerFactory> 接口,重写 customize() 方法即可
@Component
public class CustomServer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
@Override
public void customize(ConfigurableWebServerFactory factory) {
// 修改服务启动端口
factory.setPort(9090);
}
}
如果 application.properties 中也配置了 WebServer 配置,则自定义的 CustomServer 优先级更高