1. Spring Boot 与 Web 容器
- Spring Boot 是 Java 开发中常用的框架,其处理请求的能力取决于内置的 Web 容器。
- Web 容器 是运行 Web 应用的服务器,常见的有 Tomcat、Undertow 和 Jetty。
2. 常见的三种 Web 容器
- Tomcat:
- Apache 软件基金会项目。
- 广泛使用,支持 Java Servlet 和 JSP 规范。
- 企业级特性丰富,如 SSL、连接池。
- 适合大型复杂应用。
- Undertow:
- Red Hat 开发。
- 灵活、高性能,注重低内存占用和高并发。
- 适合 RESTful API 服务。
- 支持 Servlet 3.1、WebSocket、NIO 和 HTTP/2。
- Jetty:
- Eclipse 基金会维护。
- 轻量级、可嵌入、高度可配置。
- 适合快速启动和轻量级部署。
- 支持 Servlet 规范和 WebSocket。
3. Tomcat 的最大连接数和最大等待数
- 最大连接数:默认为 8192 个。
- 最大等待数:默认为 100 个。
- 处理请求总数:最大连接数 + 最大等待数 = 8292 个。
4. 修改 Tomcat 的配置
- 可以在 Spring Boot 配置文件中修改最大连接数和最大等待数:
server: tomcat: max-connections: 2000 # 最大连接数 accept-count: 200 # 最大等待数
5. 设置 Web 容器为 Jetty 或 Undertow
- 设置为 Jetty:
<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-jetty</artifactId> </dependency> </dependencies>
- 设置为 Undertow:
<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> </dependencies>