SpringBoot內置了三種servlet容器供大家選擇,默認的是tomcat,三種servlet容器 tomcat,jetty 和 undertow 可以說是javaweb項目當下最火的三款服務器,tomcat是apache下的一款重量級的服務器,不用多說歷史悠久,經得起實踐的考驗。然而:當下微服務興起,spring boot ,spring cloud 越來越熱的情況下,選擇一款輕量級而性能優越的服務器是必要的選擇。spring boot 完美集成了tomcat,jetty和undertow。
一、SpringBoot 內嵌容器切換
1、切換Tomcat
默認就是,不必切換
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency>
2、切換Jeety
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 <exclusions> 6 <exclusion> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-tomcat</artifactId> 9 </exclusion> 10 </exclusions> 11 </dependency> 12 13 <!-- Jetty適合長連接應用,就是聊天類的長連接 --> 14 <!-- 使用Jetty,需要在spring-boot-starter-web排除spring-boot-starter-tomcat,因為SpringBoot默認使用tomcat --> 15 <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-jetty</artifactId> 18 </dependency> 19 </dependencies>
3、切換Undertow
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 <exclusions> 6 <exclusion> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-tomcat</artifactId> 9 </exclusion> 10 </exclusions> 11 </dependency> 12 13 <!-- undertow不支持jsp --> 14 <!-- 使用undertow,需要在spring-boot-starter-web排除spring-boot-starter-tomcat,因為SpringBoot默認使用tomcat --> 15 <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-undertow</artifactId> 18 </dependency> 19 </dependencies>
二、SpringBoot 內嵌容器性能對比
准備:三個springboot的web項目jar報,分別使用 tomcat,jetty 和 undertow
壓測工具:JMeter
1、單例接口壓測
1)JMeter配置,300個線程,10秒內啟動,一個線程循環請求60次,如下,只對一個接口 (HTTP請求-list) 進行壓測
2)壓測結果
Tomcat:
JMete:
Undertow:
2、混合接口壓測
1)JMeter配置,300個線程,10秒內啟動,一個線程循環請求60次,如下,只對三個接口 (HTTP請求-list、) 進行壓測
2)壓測結果
Tomcat:
JMete:
Undertow:
注:單接口比混合接口慢與接口復雜度有關
參考:
1、https://www.cnblogs.com/fanshuyao/p/8668059.html
2、https://www.cnblogs.com/maybo/p/7784687.html