Spring Boot Jetty 提供了另一種選擇,可以取代 Tomcat 內嵌 Server ,Spring Boot 168 EP 6 增加了 Jetty 的相依套件,並排除 Tomcat 套件。
Table of Contents
Toggle前言
Jetty 是一個基於 Java 的網頁伺服器與 Servlet 容器,它的架構是基於 Handler 來實現的,擴展簡單,可以同時處理大量連接,而且長時間保持連接,適合於 Web 聊天應用等等。
Spring Boot Jetty
檔案目錄
./
+- build.gradle
+- src
+- main
+- resources
| +- application.properties
Gradle
build.gradle
增加 Jetty ,排除 Tomcat 。
增加 JSTL。
增加 Servlet。
修改完後,點右鍵,Gradle -> Refresh Gradle Project 。
buildscript {
group 'org.ruoxue.spring-boot-168'
version = '0.0.1-SNAPSHOT'
ext {
springBootVersion = '2.1.7.RELEASE'
}
}
configurations.all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter-jetty:${springBootVersion}"
implementation 'org.eclipse.jetty:apache-jsp:9.4.14.v20181114'
implementation 'javax.servlet:jstl:1.2'
implementation 'javax.servlet:javax.servlet-api:3.1.0'
}
設定
application.properties
增加 Jetty 設定。
server.jetty.threads.acceptors=2
server.jetty.threads.max=500
server.jetty.threads.min=8
server.jetty.threads.selectors=4
測試
Application.main
在主程式點右鍵執行 Run As -> Java Application,查看 console,Server 已成功啟動,監聽 10000 port。

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
2022-04-18 17:29:43.852 INFO 4176 --- [ main] org.ruoxue.spring_boot_168.Application : Starting Application on cheng-pc with PID 4176 (D:\dev\gitlab\ruoxue\spring-boot-168\bin\main started by cheng in D:\dev\gitlab\ruoxue\spring-boot-168)
2022-04-18 17:29:43.854 INFO 4176 --- [ main] org.ruoxue.spring_boot_168.Application : No active profile set, falling back to default profiles: default
[GC (Metadata GC Threshold) 141030K->16355K(1005056K), 0.0089991 secs]
[Full GC (Metadata GC Threshold) 16355K->15549K(1005056K), 0.0161446 secs]
2022-04-18 17:29:44.633 INFO 4176 --- [ main] o.s.b.w.e.j.JettyServletWebServerFactory : Server initialized with port: 10000
2022-04-18 17:29:44.635 INFO 4176 --- [ main] org.eclipse.jetty.server.Server : jetty-9.4.19.v20190610; built: 2019-06-10T16:30:51.723Z; git: afcf563148970e98786327af5e07c261fda175d3; jvm 1.8.0_241-b07
2022-04-18 17:29:44.654 INFO 4176 --- [ main] o.e.j.s.h.ContextHandler.application : Initializing Spring embedded WebApplicationContext
2022-04-18 17:29:44.655 INFO 4176 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 771 ms
2022-04-18 17:29:44.731 INFO 4176 --- [ main] org.eclipse.jetty.server.session : DefaultSessionIdManager workerName=node0
2022-04-18 17:29:44.731 INFO 4176 --- [ main] org.eclipse.jetty.server.session : No SessionScavenger set, using defaults
2022-04-18 17:29:44.732 INFO 4176 --- [ main] org.eclipse.jetty.server.session : node0 Scavenging every 600000ms
2022-04-18 17:29:44.751 INFO 4176 --- [ main] o.e.j.s.h.ContextHandler.application : Warning: No org.apache.tomcat.JarScanner set in ServletContext. Falling back to default JarScanner implementation.
2022-04-18 17:29:44.820 INFO 4176 --- [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2022-04-18 17:29:44.824 INFO 4176 --- [ main] o.e.jetty.server.handler.ContextHandler : Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@7726e185{application,/,[file:///C:/Users/cheng/AppData/Local/Temp/jetty-docbase.5287730162695741817.10000/],AVAILABLE}
2022-04-18 17:29:44.824 INFO 4176 --- [ main] org.eclipse.jetty.server.Server : Started @1550ms
2022-04-18 17:29:44.941 INFO 4176 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2022-04-18 17:29:45.090 INFO 4176 --- [ main] o.e.j.s.h.ContextHandler.application : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-04-18 17:29:45.091 INFO 4176 --- [ main] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-04-18 17:29:45.094 INFO 4176 --- [ main] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
2022-04-18 17:29:45.175 INFO 4176 --- [ main] o.e.jetty.server.AbstractConnector : Started ServerConnector@305f031{HTTP/1.1,[http/1.1]}{0.0.0.0:10000}
2022-04-18 17:29:45.177 INFO 4176 --- [ main] o.s.b.web.embedded.jetty.JettyWebServer : Jetty started on port(s) 10000 (http/1.1) with context path '/'
2022-04-18 17:29:45.179 INFO 4176 --- [ main] org.ruoxue.spring_boot_168.Application : Started Application in 1.608 seconds (JVM running for 1.905)
心得分享
Google 應用引擎已經全面切換為 Jetty,它的應用更加快速,修改簡單,對新的 Servlet 規範的支援也較廣泛。