【Spring Boot】内嵌容器

Spring Boot内嵌容器支持Tomcat、Jetty、Undertow。

tomcat容器

spring boot 的web应用开发必须使用spring-boot-starter-web,其默认嵌入的servlet容器是Tomcat。

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.4.3.RELEASE</version>
</parent>
 
<dependencies>
   <!-- TOMCAT -->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>

嵌入的servlet容器版本在pom的以下父依赖项中定义,比如上面的version1.4.3引入了Tomcat版本8.5.6。
如果想改变tomcat版本,也可以更改pom.xml或application.properties文件中的属性进行修改:
application.properties 文件修改:

<properties>
   <tomcat.version>8.5.6</tomcat.version></properties>

pom.xml文件修改:

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-core</artifactId>
   <version>${tomcat.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-el</artifactId>
   <version>${tomcat.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-websocket</artifactId>
   <version>${tomcat.version}</version>
</dependency>

如果想使用其他容器,可以移除tomcat容器,具体看下面容器的介绍。

Undertow容器

要将嵌入的servlet容器更改为undrow,您需要编辑pom文件以删除tomcat依赖项并添加undrow。

<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>

配置Undertow,application.xml配置如下:

server.undertow.accesslog.dir= # Undertow access log directory.
server.undertow.accesslog.enabled=false # Enable access log.
server.undertow.accesslog.pattern=common # Format pattern for access logs.
server.undertow.accesslog.prefix=access_log. # Log file name prefix.
server.undertow.accesslog.rotate=true # Enable access log rotation.
server.undertow.accesslog.suffix=log # Log file name suffix.
server.undertow.buffer-size= # Size of each buffer in bytes.
server.undertow.buffers-per-region= # Number of buffer per region.
server.undertow.direct-buffers= # Allocate buffers outside the Java heap.
server.undertow.io-threads= # Number of I/O threads to create for the worker.
server.undertow.max-http-post-size=0 # Maximum size in bytes of the HTTP post content.
server.undertow.worker-threads= # Number of worker threads.

Jetty容器

配置情况,移处默认tomcat内嵌容器

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
 <!-- 移处TOMCAT -->
   <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>

嵌入式Web容器层面的约定和定制

嵌入式Web容器对外提供HTTP服务,默认端口8080对外监听和提供服务。想改变默认的配置端口,可以在application.properties中指定。

server.port = 9000(the port number you want)
server.address
server.ssl.*
server.tomcat.*

如果上诉仍然没有办法满足要求,springBoot支持对嵌入式的Web容器实例进行定制,可以通过向IoC容器中注册一个EmbeddedServletContainerCustomizer类型的组件来对嵌入式的Web容器进行定制。

public class UnveilSpringEmbeddedTomcatCustomizer implements EmbeddedServletContainer{
        public void customize(ConfigurableEmbeddedServletContainer container){
            container.setPort(9999);
            container.setContextPath("C\hello");
                           ...
        }
    }

区别

  • 内嵌Tomcat、Jetty无法执行jar形式的jsp;Undertow不支持JSP
  • 压测结果:3个容器在相同的用例及并发请求下,Undertow稍微比Tomcat和Jetty好一点。
  • 资源消耗:JETY启动时内存占用最大,使用311 MB。Tomcat和Undertow的初始脚印相似,在120 MB左右,Undertow出现在114 MB的最低水平。响应头中的关键差异在于默认情况下包括HTTP持久连接。该头将在支持持久连接的客户端中使用,以通过重用连接细节来优化性能。Undertow在性能和内存使用方面是最好的
原文地址:https://www.cnblogs.com/zhuyeshen/p/11730714.html