方永、南天紫云

道亦有道

jetty多端口部署
2014年05月12日

有时需要将多个项目归并在同一个jetty下面部署,節省資源,便於管理。下面是部署過程:

僅在jetty 8.1.15測試通過,jetty9以上版本由於xml格式變動,需要做一些修改

一、 在 webapps 及 contexts 下面各新建目錄 1,2,3,4 ,在jetty的 etc 中添加 jetty2.xml 、 jetty3.xml 、 jetty4.xml 文件,內容如下:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id="Server2" 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="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>
 
 
<Set name="handler">
      <New id="Handlers2" class="org.eclipse.jetty.server.handler.HandlerCollection">
        <Set name="handlers">
         <Array type="org.eclipse.jetty.server.Handler">
           <Item>
             <New id="Contexts2" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
           </Item>
         </Array>
        </Set>
      </New>
 </Set>
 
 
 
 <Call name="addConnector">
      <Arg>
          <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="host"><Property name="jetty.host" /></Set>
            <Set name="port"><Property name="jetty.port" default="8010"/></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>
 
 
  <Call name="addBean">
    <Arg>
      <New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
        <Set name="contexts">
          <Ref id="Contexts2" />
        </Set>
          <Call id="webappprovider" name="addAppProvider">
            <Arg>
              <New class="org.eclipse.jetty.deploy.providers.WebAppProvider">
                <Set name="monitoredDirName"><Property name="jetty.home" default="." />/webapps/2</Set>
                <Set name="defaultsDescriptor"><Property name="jetty.home" default="."/>/etc/webdefault.xml</Set>
                <Set name="scanInterval">1</Set>
                <Set name="contextXmlDir"><Property name="jetty.home" default="." />/contexts/2</Set>
              </New>
            </Arg>
          </Call>
          <Call name="addAppProvider">
             <Arg>
               <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
                 <Set name="monitoredDirName"><Property name="jetty.home" default="." />/contexts/2</Set>
                 <Set name="scanInterval">1</Set>
               </New>
             </Arg>
          </Call>
 
      </New>
    </Arg>
  </Call>
 
</Configure>

修改相對目錄及端口號。

二、 更改 jetty-webapps.xml 、 jetty-contexts.xml 的相對目錄,如 monitoredDirName 的值改爲 /webapps/1 。

三、添加或修改 contexts 中 1,2,3,4 中項目的配置文件,內容可如下:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
 
  <Set name="contextPath">/</Set>
  <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/4/root</Set>
 
  <Set name="extractWAR">true</Set>
  <Set name="copyWebDir">false</Set>
  <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set>
 
</Configure>

四、 在 etc/jetty.conf 中添加如下内容:

etc/jetty2.xml
etc/jetty3.xml
etc/jetty4.xml

五、 启动jetty。