【容器】CentOS7.2下安裝和使用jetty-9.4.0
阿新 • • 發佈:2019-01-08
緣起
一直使用tomcat做容器。但最近玩了玩spring boot,看到內部整合的jetty蠻好玩,然後來自己搭建個jetty。通過配置和使用,目的也是為了讓自己掌握另一種容器,以後方便為業務遷移微服務來服務吧。
準備
A: 下載資源 選擇最新的tgz包。
B: 準備好centOS伺服器。
C : yum自己喜歡的編輯器。我是用vim。
步驟
一,在自定義的路徑下面下載,並改名。
wget http://central.maven.org/maven2/org/eclipse/jetty/jetty-distribution/9.4.0.v20161208/jetty-distribution-9.4 .0.v20161208.tar.gz
mv jetty-distribution-9.4.0.v20161208 jetty-9.4.0
二,最簡操作。
進入到bin中,然後發現有jetty.sh,然後啟動:
cd /usr/local/jetty/jetty-9.4.0/bin
./jetty.sh start
就可以開啟介面了。
啟動的是一個空專案。
Error 404 - Not Found.
No context on this server matched or handled this request.
Contexts known to this server are:
Powered by Jetty:// 9.4.0.v20161208
三,初探jetty.xml。
第一部分 連線池配置
<Arg name="threadpool"><New id="threadpool" class="org.eclipse.jetty.util.thread.QueuedThreadPool"/></Arg>
-->
<Get name="ThreadPool">
<Set name="minThreads" type="int"><Property name="jetty.threadPool.minThreads" deprecated="threads.min" default="10"/></Set>
<Set name="maxThreads" type="int"><Property name="jetty.threadPool.maxThreads" deprecated="threads.max" default="200"/></Set>
<Set name="idleTimeout" type="int"><Property name="jetty.threadPool.idleTimeout" deprecated="threads.timeout" default="60000"/></Set>
<Set name="detailedDump">false</Set>
</Get>
相關的調優可以這裡著手。
第二部分 針對https的配置
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
<Set name="secureScheme"><Property name="jetty.httpConfig.secureScheme" default="https" /></Set>
<Set name="securePort"><Property name="jetty.httpConfig.securePort" deprecated="jetty.secure.port" default="8443" /></Set>
當然,我們一般不會再容器裡面配置https。
第三部分 針對輸入輸出流和http訪問頭相關設定
<Set name="outputBufferSize"><Property name="jetty.httpConfig.outputBufferSize" deprecated="jetty.output.buffer.size" default="32768" /></Set>
<Set name="outputAggregationSize"><Property name="jetty.httpConfig.outputAggregationSize" deprecated="jetty.output.aggregation.size" default="8192" /></Set>
<Set name="requestHeaderSize"><Property name="jetty.httpConfig.requestHeaderSize" deprecated="jetty.request.header.size" default="8192" /></Set>
<Set name="responseHeaderSize"><Property name="jetty.httpConfig.responseHeaderSize" deprecated="jetty.response.header.size" default="8192" /></Set>
<Set name="sendServerVersion"><Property name="jetty.httpConfig.sendServerVersion" deprecated="jetty.send.server.version" default="true" /></Set>
<Set name="sendDateHeader"><Property name="jetty.httpConfig.sendDateHeader" deprecated="jetty.send.date.header" default="false" /></Set>
<Set name="headerCacheSize"><Property name="jetty.httpConfig.headerCacheSize" default="512" /></Set>
對流的控制,可以在這裡調優。
第四部分 額外服務設定
<Set name="stopAtShutdown"><Property name="jetty.server.stopAtShutdown" default="true"/></Set>
<Set name="stopTimeout"><Property name="jetty.server.stopTimeout" default="5000"/></Set>
<Set name="dumpAfterStart"><Property name="jetty.server.dumpAfterStart" deprecated="jetty.dump.start" default="false"/></Set>
<Set name="dumpBeforeStop"><Property name="jetty.server.dumpBeforeStop" deprecated="jetty.dump.stop" default="false"/></Set>
主要是對於服務停止後關閉設定,和過期相關的設定。
四,當然,在整個配置路徑下,也可以看到一些熟悉的面孔,如gzip的壓縮配置等,感覺如果玩過tomcat優化的同學,應該不會陌生。
五,對於基本需求,比如改埠這種事情,不可避免。但是似乎9.4.0對配置檔案有過更新。原本在jetty.xml進行配置就可以搞定,但是最新版本看說明文字。
<!-- =========================================================== -->
<!-- Http Configuration. -->
<!-- This is a common configuration instance used by all -->
<!-- connectors that can carry HTTP semantics (HTTP, HTTPS, etc.)-->
<!-- It configures the non wire protocol aspects of the HTTP -->
<!-- semantic. -->
<!-- -->
<!-- This configuration is only defined here and is used by -->
<!-- reference from other XML files such as jetty-http.xml, -->
<!-- jetty-https.xml and other configuration files which -->
<!-- instantiate the connectors. -->
<!-- -->
<!-- Consult the javadoc of o.e.j.server.HttpConfiguration -->
<!-- for all configuration that may be set here. -->
<!-- =========================================================== -->
說的比較清楚,如果該檔案中沒有的內容,到jetty-http或者https中進行設定。
因此,我們找到jetty-http.xml。
可以看到該處已經有對host和預設埠8080的設定。
<Set name="host"><Property name="jetty.http.host" deprecated="jetty.host" /></Set>
<Set name="port"><Property name="jetty.http.port" deprecated="jetty.port" default="8080" /></Set>
這裡進行變更,然後重新啟動即可。