題目詳細答案
Spring Boot 默認使用 Tomcat 作為嵌入式的 Servlet 容器,但你也可以切換到 Undertow。Undertow 是一個輕量級、高性能的 Web 服務器和 Servlet 容器。
步驟 1:排除 Tomcat 依賴
需要在pom.xml
文件(如果使用的是 Maven)或build.gradle
文件(如果使用的是 Gradle)中排除 Tomcat 依賴。
Maven:
<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>
Gradle:
implementation('org.springframework.boot:spring-boot-starter-web') {exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
步驟 2:添加 Undertow 依賴
添加 Undertow 依賴。
Maven:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
步驟 3:確認配置
確保你的 Spring Boot 應用程序的主類沒有特定于 Tomcat 的配置。通常情況不需要做任何修改,因為 Spring Boot 會自動配置 Undertow。
示例pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.4</version><relativePath/> <!-- lookup parent from repository --></parent><properties><java.version>11</java.version></properties><dependencies><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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
驗證
啟動Spring Boot 應用程序。應該能夠在控制臺日志中看到 Undertow 服務器啟動的消息,而不是 Tomcat。
2021-08-30 10:00:00.000 INFO 12345 --- [ main] io.undertow : starting undertow server
Spring Boot 切換默認 Servlet 容器到 Undertow 的詳細指南
為什么選擇 Undertow?
Undertow 是一個由 Red Hat 開發的輕量級、高性能的 Web 服務器和 Servlet 容器,相比 Tomcat 有以下優勢:
- 更輕量級,內存占用更少
- 更高的性能,特別是在高并發場景下
- 支持 HTTP/2
- 靈活的架構設計
詳細切換步驟
1. 排除 Tomcat 依賴
Maven 項目 (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>
Gradle 項目 (build.gradle
):
implementation('org.springframework.boot:spring-boot-starter-web') {exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
2. 添加 Undertow 依賴
Maven 項目 (pom.xml
):
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
Gradle 項目 (build.gradle
):
implementation 'org.springframework.boot:spring-boot-starter-undertow'
3. 完整 Maven 配置示例
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.4</version> <!-- 或使用最新版本 --></parent><properties><java.version>11</java.version></properties><dependencies><!-- Web starter with Tomcat excluded --><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><!-- Undertow starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-undertow</artifactId></dependency><!-- Test dependencies --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
4. 自定義 Undertow 配置 (可選)
在 application.properties
或 application.yml
中可以配置 Undertow 參數:
application.properties:
# 服務器端口
server.port=8080# Undertow 特定配置
server.undertow.threads.io=16
server.undertow.threads.worker=256
server.undertow.buffer-size=1024
server.undertow.direct-buffers=true
application.yml:
server:port: 8080undertow:threads:io: 16worker: 256buffer-size: 1024direct-buffers: true
5. 驗證切換成功
啟動應用程序后,在控制臺日志中應該能看到類似以下信息,表明 Undertow 已成功啟動:
2023-08-11 14:30:00.000 INFO 12345 --- [ main] o.s.b.web.embedded.undertow.Undertow : Undertow started on port(s) 8080 (http)
而不是 Tomcat 的啟動日志。
常見問題解決
- 端口沖突:
- 確保沒有其他應用程序占用相同端口
- 在
application.properties
中修改server.port
- 依賴沖突:
- 確保完全排除了 Tomcat 依賴
- 使用
mvn dependency:tree
檢查依賴關系
- 性能調優:
- 根據應用負載調整線程池大小
- 監控應用性能指標進行優化
總結
通過以上步驟,你可以輕松將 Spring Boot 的默認 Servlet 容器從 Tomcat 切換到 Undertow。Undertow 特別適合需要高性能和低內存占用的應用場景。切換后,大部分 Spring Boot 的功能和配置方式保持不變,確保了良好的開發體驗。