H__D  

  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

  

posted on 2021-06-01 17:40  H__D  阅读(2722)  评论(0编辑  收藏  举报