Inside in Jetty 8.x 模塊化
Inside in Jetty 8.x帶有一個默認的test環境。我們從這個默認的環境入手。
首先,來分析下start.ini里面的配置,這個配置決定啟動了哪些模塊。
$ grep -v "#" start.ini|grep -v "^$" OPTIONS=Server,jsp,jmx,resources,websocket,ext,plus,annotations etc/jetty.xml etc/jetty-annotations.xml etc/jetty-deploy.xml etc/jetty-webapps.xml etc/jetty-contexts.xml etc/jetty-testrealm.xml
利用上節學到的只是,我們先來分析下用到了那些模塊。
java -jar start.jar --list-options
查找Server,jsp,jmx,resources,websocket,ext,plus,annotations這些對應的模塊有:
GLOBAL option (Appended Entries) (*) ------------------------------------------------------------- 0: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-util-8.1.2.v20120308.jar 1: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-io-8.1.2.v20120308.jar
Option [Server] (Aggregate) ------------------------------------------------------------- 0: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-xml-8.1.2.v20120308.jar 1: 3.0.0.v201112011016 | ${jetty.home}/lib/servlet-api-3.0.jar 2: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-http-8.1.2.v20120308.jar 3: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-continuation-8.1.2.v20120308.jar 4: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-server-8.1.2.v20120308.jar 5: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-security-8.1.2.v20120308.jar 6: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-servlet-8.1.2.v20120308.jar 7: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-webapp-8.1.2.v20120308.jar 8: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-deploy-8.1.2.v20120308.jar 9: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-servlets-8.1.2.v20120308.jar
Option [jsp] ------------------------------------------------------------- 0: 2.2.0.v201108011116 | ${jetty.home}/lib/jsp/com.sun.el-2.2.0.v201108011116.jar 1: 2.2.0.v201108011116 | ${jetty.home}/lib/jsp/javax.el-2.2.0.v201108011116.jar 2: 1.2.0.v201105211821 | ${jetty.home}/lib/jsp/javax.servlet.jsp.jstl-1.2.0.v201105211821.jar 3: 2.2.0.v201112011158 | ${jetty.home}/lib/jsp/javax.servlet.jsp-2.2.0.v201112011158.jar 4: 2.2.2.v201112011158 | ${jetty.home}/lib/jsp/org.apache.jasper.glassfish-2.2.2.v201112011158.jar 5: 1.2.0.v201112081803 | ${jetty.home}/lib/jsp/org.apache.taglibs.standard.glassfish-1.2.0.v201112081803.jar 6: 3.7.0.M20110909-1335 | ${jetty.home}/lib/jsp/org.eclipse.jdt.core-3.7.1.jar
Option [jmx] ------------------------------------------------------------- 0: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-jmx-8.1.2.v20120308.jar
Option [resources] ------------------------------------------------------------- 0: (dir) | ${jetty.home}/resources
Option [websocket] ------------------------------------------------------------- 0: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-websocket-8.1.2.v20120308.jar
Option [ext] ------------------------------------------------------------- Empty option, no classpath entries active.
Option [plus] ------------------------------------------------------------- 0: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-jndi-8.1.2.v20120308.jar 1: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-plus-8.1.2.v20120308.jar 2: 1.1.0.v201105071233 | ${jetty.home}/lib/jndi/javax.activation-1.1.0.v201105071233.jar 3: 1.4.1.v201005082020 | ${jetty.home}/lib/jndi/javax.mail.glassfish-1.4.1.v201005082020.jar
Option [annotations] ------------------------------------------------------------- 0: 8.1.2.v20120308 | ${jetty.home}/lib/jetty-annotations-8.1.2.v20120308.jar 1: 1.1.0.v201108011116 | ${jetty.home}/lib/annotations/javax.annotation-1.1.0.v201108011116.jar 2: 3.1.0.v200803061910 | ${jetty.home}/lib/annotations/org.objectweb.asm-3.1.0.v200803061910.jar
從上一節中我們知道,這些模塊相當于將那些組件加入classpath中,jetty在啟動時也會裝載這些模塊。
$java -jar start.jar --dry-run|awk '{print $4}'|sed 's/:/\n/g' /opt/apps/jetty8/lib/jetty-xml-8.1.2.v20120308.jar /opt/apps/jetty8/lib/servlet-api-3.0.jar /opt/apps/jetty8/lib/jetty-http-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jetty-continuation-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jetty-server-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jetty-security-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jetty-servlet-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jetty-webapp-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jetty-deploy-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jetty-servlets-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jetty-annotations-8.1.2.v20120308.jar /opt/apps/jetty8/lib/annotations/javax.annotation-1.1.0.v201108011116.jar /opt/apps/jetty8/lib/annotations/org.objectweb.asm-3.1.0.v200803061910.jar /opt/apps/jetty8/lib/jetty-jmx-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jsp/com.sun.el-2.2.0.v201108011116.jar /opt/apps/jetty8/lib/jsp/javax.el-2.2.0.v201108011116.jar /opt/apps/jetty8/lib/jsp/javax.servlet.jsp.jstl-1.2.0.v201105211821.jar /opt/apps/jetty8/lib/jsp/javax.servlet.jsp-2.2.0.v201112011158.jar /opt/apps/jetty8/lib/jsp/org.apache.jasper.glassfish-2.2.2.v201112011158.jar /opt/apps/jetty8/lib/jsp/org.apache.taglibs.standard.glassfish-1.2.0.v201112081803.jar /opt/apps/jetty8/lib/jsp/org.eclipse.jdt.core-3.7.1.jar /opt/apps/jetty8/lib/jetty-jndi-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jetty-plus-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jndi/javax.activation-1.1.0.v201105071233.jar /opt/apps/jetty8/lib/jndi/javax.mail.glassfish-1.4.1.v201005082020.jar /opt/apps/jetty8/resources /opt/apps/jetty8/lib/jetty-websocket-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jetty-util-8.1.2.v20120308.jar /opt/apps/jetty8/lib/jetty-io-8.1.2.v20120308.jar
在總結下,不同的OPTION決定了啟動不同的模塊(也就是不同的組件和classpath)。另外,對于start.config里面的不同的OPTION可能有相同的模塊依賴。
默認的test.war啟動了如下模塊:
- Server: 一個標準的servlet容器
- jsp: jsp模塊
- jmx: jmx支持
- resources: 允許從${jetty.home}/resources中讀取類資源(實際上是配置log4j.properties)
- websocket: 支持websocket的例子
- ext: 由于${jetty.home}/lib/ext目錄為空,實際上什么都做。其實此特性是為了裝載自定義的組件依賴。
- plus: 一些擴展支持,從上面classpath中猜測,應該是jndi、java認證以及java mail的組件。
- annotations: java注解以及字節碼的支持。
test.war配置
再來看看加載jetty配置。 test.war模塊默認加載了6個配置組件。
etc/jetty.xml etc/jetty-annotations.xml etc/jetty-deploy.xml etc/jetty-webapps.xml etc/jetty-contexts.xml etc/jetty-testrealm.xml
Inside in Jetty.xml
默認的jetty.xml負責配置設置服務器的參數,包括綁定的地址、線程池大小以及一些默認的處理器(Handler)等。
<Configure id="Server" class="org.eclipse.jetty.server.Server"> <Set name="ThreadPool"> <New class="org.eclipse.jetty.util.thread.QueuedThreadPool"> <Set name="minThreads">10</Set> <Set name="maxThreads">200</Set> <Set name="detailedDump">false</Set> </New> </Set> <Call name="addConnector"> <Arg> <New class="org.eclipse.jetty.server.nio.SelectChannelConnector"> <Set name="host"><Property name="Inside in Jetty.host" /></Set> <Set name="port"><Property name="Inside in Jetty.port" default="8080"/></Set> <Set name="maxIdleTime">300000</Set> <Set name="Acceptors">2</Set> <Set name="statsOn">false</Set> <Set name="confidentialPort">8443</Set> <Set name="lowResourcesConnections">20000</Set> <Set name="lowResourcesMaxIdleTime">5000</Set> </New> </Arg> </Call> <Set name="handler"> <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection"> <Set name="handlers"> <Array type="org.eclipse.jetty.server.Handler"> <Item> <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/> </Item> <Item> <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/> </Item> </Array> </Set> </New> </Set> <Set name="stopAtShutdown">true</Set> <Set name="sendServerVersion">true</Set> <Set name="sendDateHeader">true</Set> <Set name="gracefulShutdown">1000</Set> <Set name="dumpAfterStart">false</Set> <Set name="dumpBeforeStop">false</Set> </Configure>
可以看出默認的線程池大小是最小線程10個,最大線程200個。綁定在所有網卡的8080端口。其它配置以后再分析。
Inside in Jetty-annotation.xml
Inside in Jetty-annotation.xml配置應該是描述支持哪些注解配置方式。
<Call name="setAttribute"> <Arg>org.eclipse.jetty.webapp.configuration</Arg> <Arg> <Array type="java.lang.String"> <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item> <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item> <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item> <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item> <Item>org.eclipse.jetty.annotations.AnnotationConfiguration</Item> <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item> </Array> </Arg> </Call>
Inside in Jetty-deploy.xml
Inside in Jetty-deploy.xml配置web發布方式。
<Call name="addBean"> <Arg> <New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager"> <Set name="contexts"> <Ref id="Contexts" /> </Set> <Call name="setContextAttribute"> <Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg> <Arg>.*/servlet-api-[^/]*\.jar$</Arg> </Call> </New> </Arg> </Call>
事實上這里沒有定義要發布的目錄或者應用位置,因此jetty-deploy依賴于jetty-contexts.xml或者jetty-webapps.xml。
Inside in Jetty-webapps.xml
Inside in Jetty-webapps.xml定義要發布的內容,通常是要發布應用或者應用的定義。默認是存放于${jetty.home}/webapps下的應用以及${jetty.home}/contexts下的xml定義。
<Ref id="DeploymentManager"> <Call id="webappprovider" name="addAppProvider"> <Arg> <New class="org.eclipse.jetty.deploy.providers.WebAppProvider"> <Set name="monitoredDirName"><Property name="Inside in Jetty.home" default="." />/webapps</Set> <Set name="defaultsDescriptor"><Property name="Inside in Jetty.home" default="."/>/etc/webdefault.xml</Set> <Set name="scanInterval">1</Set> <Set name="contextXmlDir"><Property name="Inside in Jetty.home" default="." />/contexts</Set> <Set name="extractWars">true</Set> </New> </Arg> </Call> </Ref>
Inside in Jetty-contexts.xml
Inside in Jetty-contexts.xml定義一些預置規則。類似于一些攔截器。例如可以講某些URI rewrite或者靜態資源cache配置等。
這會自動掃描${jetty.home}/contexts下面的xml配置。
<Ref id="DeploymentManager"> <Call name="addAppProvider"> <Arg> <New class="org.eclipse.jetty.deploy.providers.ContextProvider"> <Set name="monitoredDirName"><Property name="Inside in Jetty.home" default="." />/contexts</Set> <Set name="scanInterval">1</Set> </New> </Arg> </Call> </Ref>
Inside in Jetty-testrealm.xml
Inside in Jetty-testrealm.xml用于test.war的特定配置,用于配置一些認證信息。
<Call name="addBean"> <Arg> <New class="org.eclipse.jetty.security.HashLoginService"> <Set name="name">Test Realm</Set> <Set name="config"><Property name="Inside in Jetty.home" default="."/>/etc/realm.properties</Set> <Set name="refreshInterval">0</Set> </New> </Arg> </Call>
test.xml
事實上這么模塊的配置都是在<Configure id="Server" class="org.eclipse.jetty.server.Server">節點配置下。 因此可以合并成一個大的xml。這樣做的好處是在一個xml包含所有配置,方便靈活定義。當然,壞處就是復用率低。
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <Set name="ThreadPool"> <!-- Default queued blocking threadpool --> <New class="org.eclipse.jetty.util.thread.QueuedThreadPool"> <Set name="minThreads">10</Set> <Set name="maxThreads">200</Set> <Set name="detailedDump">false</Set> </New> </Set> <Call name="addConnector"> <Arg> <New class="org.eclipse.jetty.server.nio.SelectChannelConnector"> <Set name="host"><Property name="Inside in Jetty.host" /></Set> <Set name="port"><Property name="Inside in Jetty.port" default="8080"/></Set> <Set name="maxIdleTime">300000</Set> <Set name="Acceptors">2</Set> <Set name="statsOn">false</Set> <Set name="confidentialPort">8443</Set> <Set name="lowResourcesConnections">20000</Set> <Set name="lowResourcesMaxIdleTime">5000</Set> </New> </Arg> </Call> <Set name="handler"> <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection"> <Set name="handlers"> <Array type="org.eclipse.jetty.server.Handler"> <Item> <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/> </Item> <Item> <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/> </Item> </Array> </Set> </New> </Set> <Set name="stopAtShutdown">true</Set> <Set name="sendServerVersion">true</Set> <Set name="sendDateHeader">true</Set> <Set name="gracefulShutdown">1000</Set> <Set name="dumpAfterStart">false</Set> <Set name="dumpBeforeStop">false</Set> <Call name="setAttribute"> <Arg>org.eclipse.jetty.webapp.configuration</Arg> <Arg> <Array type="java.lang.String"> <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item> <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item> <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item> <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item> <Item>org.eclipse.jetty.annotations.AnnotationConfiguration</Item> <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item> </Array> </Arg> </Call> <Call name="addBean"> <Arg> <New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager"> <Set name="contexts"> <Ref id="Contexts" /> </Set> <Call name="setContextAttribute"> <Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg> <Arg>.*/servlet-api-[^/]*\.jar$</Arg> </Call> </New> </Arg> </Call> <Ref id="DeploymentManager"> <Call id="webappprovider" name="addAppProvider"> <Arg> <New class="org.eclipse.jetty.deploy.providers.WebAppProvider"> <Set name="monitoredDirName"><Property name="Inside in Jetty.home" default="." />/webapps</Set> <Set name="defaultsDescriptor"><Property name="Inside in Jetty.home" default="."/>/etc/webdefault.xml</Set> <Set name="scanInterval">1</Set> <Set name="contextXmlDir"><Property name="Inside in Jetty.home" default="." />/contexts</Set> <Set name="extractWars">true</Set> </New> </Arg> </Call> </Ref> <Ref id="DeploymentManager"> <Call name="addAppProvider"> <Arg> <New class="org.eclipse.jetty.deploy.providers.ContextProvider"> <Set name="monitoredDirName"><Property name="Inside in Jetty.home" default="." />/contexts</Set> <Set name="scanInterval">1</Set> </New> </Arg> </Call> </Ref> <Call name="addBean"> <Arg> <New class="org.eclipse.jetty.security.HashLoginService"> <Set name="name">Test Realm</Set> <Set name="config"><Property name="Inside in Jetty.home" default="."/>/etc/realm.properties</Set> <Set name="refreshInterval">0</Set> </New> </Arg> </Call> </Configure>
我們將test.xml放入etc目錄下面。這是保持start.ini文件不存在,也就是不使用start.ini里面的配置。
手動運行它。
java -jar start.jar OPTIONS=Server,jsp,jmx,resources,websocket,ext,plus,annotations etc/test.xml
這時候的啟動就和默認啟動是一樣的了。
這時候就可以訪問了。
Inside in Jetty Statistics
這一部分,我們利用學習到的只是來部署一個Jetty統計模塊。
修改設置
將連接計數參數打開:
<Call name="addConnector"> <Arg> <New class="org.eclipse.jetty.server.nio.SelectChannelConnector"> <Set name="host"><Property name="Inside in Jetty.host" /></Set> <Set name="port"><Property name="Inside in Jetty.port" default="8080"/></Set> <Set name="maxIdleTime">300000</Set> <Set name="Acceptors">2</Set> <Set name="statsOn">true</Set><!-- modify this --> </New> </Arg> </Call>
設置servlet
為了不影響默認的test.war環境,我們增加一個最簡單的WAR環境。
根據前面學到的知識,只需要將war環境放到webapps目錄下即可。可以是一個war包,也可以是一個以.war結尾的目錄。
$tree webapps/demo.war/ webapps/demo.war/ `-- WEB-INF |-- Inside in Jetty-web.xml `-- web.xml 1 directory, 2 files
$cat webapps/demo.war/WEB-INF/jetty-web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="contextPath">/demo</Set> </Configure>
$cat webapps/demo.war/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="false" version="3.0"> <display-name>static demo</display-name> <servlet> <servlet-name>statistic</servlet-name> <servlet-class>org.eclipse.jetty.servlet.StatisticsServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>restrictToLocalhost</param-name><param-value>false</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>statistic</servlet-name> <url-pattern>/statistic/*</url-pattern> </servlet-mapping> </web-app>
說明幾點:
- 為了不和test.war的contextPath混淆,這里強制修改為/demo。
- 增加一個jetty內置的統計servlet(org.eclipse.jetty.servlet.StatisticsServlet)
- 將servlet的參數restrictToLocalhost修改為false,否則默認情況下只能通過本機訪問,不能遠程訪問
運行demo
保持test.war不變增加一個統計配置。
$java -jar start.jar etc/jetty-stats.xml
執行效果
使用瀏覽器訪問
http://127.0.0.1:8080/demo/statistic/
效果如下:


統計結果包含6個部分:
- 統計結果收集時間
- 請求數詳情(當前請求數、最大請求數、總共請求數、請求時間等)
- 請求分發詳情(和請求數不同的是,這是jetty內部分發請求的數量,包括forward/include等)
- 響應狀態詳情(1xx/2xx/3xx/4xx/5xx以及總共發送的字節數)
- 連接數詳情 (當前連接數、最大連接數、連接持續時間等)
- 內存狀況(堆內存和非堆內存使用狀況,非堆內存通常也稱永久代內存)
小結
Inside in Jetty 8.x已經將各個模塊拆分非常詳細了。每一個模塊的命名都非常有規律。通常從名稱上就能夠猜測出模塊的作用。
部分模塊可能還需要對應的配置。${jetty.home}/etc下面有大量的配置,這些零散的配置拆分是為了可復用。 如果一個jetty要想啟動多個java進程,那么只需要指定不同的配置即可。甚至為了方便定制化,可能為每一個java進程創建一個完整的jetty.xml配置,而不需要${jetty.home}/etc下面的配置。