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在性能和內存使用方面是最好的。