【问题标题】:How can I map multiple contexts to the same war file in Jetty?如何将多个上下文映射到 Jetty 中的同一个战争文件?
【发布时间】:2011-06-21 03:41:18
【问题描述】:

是否可以将多个 contextPaths 映射到 Jetty 中的一个 WAR 文件?例如

${jetty.home}/webapp/bookstore.war

然后我想有两个不同的背景指向这场战争。原因是某些配置差异取决于访问的 URL。

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/magazines</Set>
    <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/bookstore.war</Set>
</Configure>

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/books</Set>
    <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/bookstore.war</Set>
</Configure>

【问题讨论】:

  • 假设您可以将单个war 文件部署到多个上下文,您将如何加载不同的配置?基于路径?如果用户输入像myhost/magazines/../books...这样的网址怎么办?
  • 您最好尝试通过 Apache 前端处理差异,使用 tomcat 连接器将流量路由到 Jetty。

标签: java deployment jetty


【解决方案1】:

我是这样做的,我还为每个站点设置了不同的 SSL 证书(未显示)。我并不声称了解我所知道的一切,但这在几个安装中对我有用。每个实例都需要一个“jetty.xml”和一个“contexts.xml”文件。

假设码头安装在 /opt/Jetty...

启动两个引用两个版本 jetty.xml 的服务器实例(这可以在一个脚本中完成,如图所示,或在两个单独的启动脚本中完成)

start.sh...

cd /opt/Jetty 
java -jar start.jar etc/jetty.xml etc/jetty2.xml

如果您的服务器有多个 ip,您可以使用 context.xml 文件在每个 jetty.xml 文件的连接器部分指定不同的 ip 或主机名。如果你只有一个 ip,那么你将使用上下文 xml 中的上下文路径设置来区分这两个实例。

在 jetty.xml 中,参考 ip 或主机,以及 目录以包含第一个实例的 context.xml

<Call name="addConnector">
   <Arg>
       <New class="org.mortbay.jetty.nio.SelectChannelConnector">
         <Set name="host">HOST OR IP FOR FIRST INSTANCE</Set>
         <Set name="port"><SystemProperty name="jetty.port" default="80"/></Set>
         <Set name="maxIdleTime">30000</Set>
         <Set name="Acceptors">2</Set>
         <Set name="statsOn">false</Set>
         <Set name="confidentialPort">443</Set>
         <Set name="lowResourcesConnections">5000</Set>
         <Set name="lowResourcesMaxIdleTime">5000</Set>
       </New>
   </Arg>
 </Call>
 <Call name="addLifeCycle">
   <Arg>
     <New class="org.mortbay.jetty.deployer.ContextDeployer">
       <Set name="contexts"><Ref id="Contexts"/></Set>
       <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts/directory_for_FIRST_instance</Set>
       <Set name="scanInterval">5</Set>
     </New>
   </Arg>
 </Call>

在 jetty.xml 中,引用 ip 或主机,以及 目录以包含第二个实例的 context.xml

<Call name="addConnector">
   <Arg>
       <New class="org.mortbay.jetty.nio.SelectChannelConnector">
         <Set name="host">HOST OR IP FOR SECOND INSTANCE</Set>
         <Set name="port"><SystemProperty name="jetty.port" default="80"/></Set>
         <Set name="maxIdleTime">30000</Set>
         <Set name="Acceptors">2</Set>
         <Set name="statsOn">false</Set>
         <Set name="confidentialPort">443</Set>
         <Set name="lowResourcesConnections">5000</Set>
         <Set name="lowResourcesMaxIdleTime">5000</Set>
       </New>
   </Arg>
 </Call>
 <Call name="addLifeCycle">
   <Arg>
     <New class="org.mortbay.jetty.deployer.ContextDeployer">
       <Set name="contexts"><Ref id="Contexts"/></Set>
       <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts/directory_for_SECOND_instance</Set>
       <Set name="scanInterval">5</Set>
     </New>
   </Arg>
 </Call>

如果定义如上所示,您可以通过触摸上下文 xml 文件重新加载 war 文件并重新启动应用程序。

将单独的上下文文件放在上下文目录的不同子目录中,每个子目录都指向同一个war文件,但具有不同的上下文路径和不同的虚拟主机。

/opt/Jetty/contexts/subdirectory_for_first_instance/context_first.xml
/opt/Jetty/contexts/subdirectory_for_second_instance/context_second.xml

在 context_first.xml - 你可以指定一个节点 (firstapp) 将始终链接到你的第一个应用程序

<Set name="contextPath">/firstapp</Set>

在 context_second.xml 中 - 您可以指定一个节点(firstapp),它将始终链接到您的第二个应用程序

<Set name="contextPath">/secondapp</Set>

(如果你想从同一个 ip 运行它们,以上是必要的(两个不同的上下文路径),我相信)

然后在单独的上下文文件中定义两个虚拟主机(必须映射浏览器正在使用的 url):
在 context_first.xml 中:

<Set name="virtualHosts">
  <Array type="String">
    <Item>www.my_first_app.com</Item>
  </Array>
</Set>

在 context_second.xml 中

<Set name="virtualHosts">
  <Array type="String">
    <Item>www.my_second_app.com</Item>
  </Array>
</Set>

注意: 如果您有两个 ip 或主机名,则可以将两个应用程序的上下文路径设置为“/”
如果你只有一个 ip,上下文路径将决定访问哪个应用程序

此外,重要的是,您可以将上下文参数发送到您的应用程序,以便它可以在必要时确定它是哪个实例。

向每个实例发送唯一值的上下文参数示例:

 <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
 <!-- Custom context configuration                                  -->
 <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
 <Set name="initParams">
   <New class="java.util.HashMap">
    <Put name="customer">Joes Fish Store</Put>
    <Put name="ShowPanelNames">N</Put>
    <Put name="FiscalYearStartMonth">10</Put>
    <Put name="LiveEmail">Y</Put>
   </New>
 </Set>

【讨论】:

    【解决方案2】:

    您可以简单地复制战争文件并重命名它。

    【讨论】:

      【解决方案3】:

      我意识到这是旧的,但由于提供的答案并没有真正回答问题,为了将来参考,您可以通过向Configure 添加一个id 属性来使用相同的.war 实现多个WebappContexts。

      <Configure id="magazinesContext" class="org.eclipse.jetty.webapp.WebAppContext">
          <Set name="contextPath">/magazines</Set>
          <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/bookstore.war</Set>
          <Set name="extractWAR">true</Set>
      </Configure>
      
      <Configure id="booksContext" class="org.eclipse.jetty.webapp.WebAppContext">
          <Set name="contextPath">/books</Set>
          <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/bookstore.war</Set>
          <Set name="extractWAR">true</Set>
      </Configure>
      

      但是请注意,上下文中定义的所有命名资源必须使用

      分配给上下文
      <Arg>
          <Ref id="magazinesContext" />
      </Arg>
      

      因此,如果您有 dbcp 池资源“pg_conn”,但没有引用 WebappContext 的 id 的 Arg(在本例中为“magazinesContext”或“booksContext”),则资源将被全局定义,即最后加载的 WebAppContext 获胜。

      以下面的WebappContext 定义为例,其中“pg_conn”是全局定义的:

      <Configure id="magazinesContext" class="org.eclipse.jetty.webapp.WebAppContext">
          <Set name="contextPath">/magazines</Set>
          <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/bookstore.war</Set>
          <Set name="extractWAR">true</Set>
          <New id="pg_conn" class="org.mortbay.jetty.plus.naming.Resource">
              <Arg>jdbc/db</Arg>
              <Arg>
                  <New class="org.apache.commons.dbcp.BasicDataSource">
                      <Set name="driverClassName">org.postgresql.Driver</Set>
                      <Set name="url">jdbc:postgresql://localhost:5432/test_db</Set>
                      <Set name="username">test</Set>
                      <Set name="password">*****</Set>
                  </New>
              </Arg>
          </New>
      </Configure>
      

      还有这个,它是为WebappContext的实例定义的:

      <Configure id="magazinesContext" class="org.eclipse.jetty.webapp.WebAppContext">
          <Set name="contextPath">/magazines</Set>
          <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/bookstore.war</Set>
          <Set name="extractWAR">true</Set>
          <New id="pg_conn" class="org.mortbay.jetty.plus.naming.Resource">
              <Arg>
                  <Ref id="magazinesContext" />
              </Arg>
              <Arg>jdbc/db</Arg>
              <Arg>
                  <New class="org.apache.commons.dbcp.BasicDataSource">
                      <Set name="driverClassName">org.postgresql.Driver</Set>
                      <Set name="url">jdbc:postgresql://localhost:5432/test_db</Set>
                      <Set name="username">test</Set>
                      <Set name="password">*****</Set>
                  </New>
              </Arg>
          </New>
      </Configure>
      

      【讨论】:

        猜你喜欢
        • 2015-07-02
        • 2013-07-20
        • 2012-10-29
        • 2015-06-23
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        • 1970-01-01
        • 2012-08-12
        相关资源
        最近更新 更多