jetty-如何让一个应用响应一个特定端口

本文详细介绍了如何利用Jetty服务器实例实现一个应用一个端口访问的技术,包括配置两个Jetty服务器实例、创建上下文文件以及部署应用的具体步骤。通过这种方式,可以确保不同的应用只能从各自指定的端口进行访问,提高了系统的安全性与可用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jetty中文文档:www.jettycn.com

 

 

一个应用一个端口

Jetty >> howto >> 一个应用一个端口

A应用只能从A端口访问,B应用只能从B端口访问

要做到这一点的最有效的方法是创建两个org.eclipse.jetty.server.Server实例。还有另外一种效率较低的替代方案

服务器实例A有一个监听A端口的connector并部署A应用,服务器实例B有一个监听B端口的connector并部署B应用。例如:

我们希望A应用从8080端口访问,B应用通过SSL协议从8443端口访问。我们建立了2个服务器实例,每个实例的jetty.xml文件如下所示:

jettyA.xml

<Configure id="ServerA" class="org.eclipse.jetty.server.Server">

    <!-- set up the port for ServerA -->
    <Set name="connectors">
      <Array type="org.eclipse.jetty.server.Connector">
        <Item>
          <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="port">8080</Set>
            <Set name="maxIdleTime">30000</Set>
            <Set name="Acceptors">10</Set>
          </New>
        </Item>
      </Array>
    </Set>

    <!-- set up a context provider for Server A -->
    <Call name="addLifeCycle">
      <Arg>
        <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
          <Set name="contexts"><Ref id="Contexts"/></Set>
          <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contextsA</Set>
          <Set name="scanInterval">5</Set>
        </New>
      </Arg>
    </Call>

</Configure>

jettyB.xml:

<Configure id="ServerB" class="org.eclipse.jetty.server.Server">

    <!-- set up the port for ServerB -->
    <Set name="connectors">
      <Array type="org.eclipse.jetty.server.Connector">
        <Item>
          <New class="org.eclipse.jetty.server.ssl.SslSocketConnector">
            <Set name="Port">8443</Set>
            <Set name="maxIdleTime">30000</Set>
            <Set name="Keystore"><SystemProperty name="jetty.home" default="." />/etc/keystore</Set>
            <Set name="Password">OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4</Set>
            <Set name="KeyPassword">OBF:1u2u1wml1z7s1z7a1wnl1u2g</Set>
          </New>
        </Item>
      </Array>
    </Set>

    <!-- set up a context provider for ServerB -->
    <Call name="addLifeCycle">
      <Arg>
        <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
          <Set name="contexts"><Ref id="Contexts"/></Set>
          <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contextsB</Set>
          <Set name="scanInterval">5</Set>
        </New>
      </Arg>
    </Call>

</Configure>

现在我们需要配置两个context文件,一个描述需要部署到ServerA上的A应用,另一个描述需要部署到ServerB上的B应用。然后,我们把这些文件分别放到$JETTY_HOME/contextsA或$JETTY_HOME/contextsB中。

contextA.xml

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

contextB.xml:

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

你将在同一个JVM中运行2个服务器实例,只需提供2个xml配置文件:

java -jar start.jar jettyA.xml jettyB.xml

当然,你也可以启动2个独立Jetty实例,一个实例使用jettyA.xml,另一个实例使用jettyB.xml。但是,在同一JVM中运行2个服务器通常是更有效的。

替代方案

还有另一种方法,也可以实现上述相同的结果,但它的效率稍微差一些。这需要设置web应用的connector列表,只有列表中的connector发送过来的请求才会被处理。相对于上面描述的方法,这是一个效率较低的解决方案,因为该请求提交给每一个web应用,web应用必须决定是否接收并处理这个请求。在第一个解决方案,请求只会传给唯一的一个web应用。在此配置中,你只需要一个服务器实例。你需要为每个connector定义唯一的名称,然后给每个web应用指派connector名称列表,这样web应用就可以响应指定connector发来的请求了。

以下代码展示了如何配置connector列表:

jetty.xml:

<Configure class="org.eclipse.jetty.server.Server">

    <!-- set up both connectors -->
    <Set name="connectors">
      <Array type="org.eclipse.jetty.server.Connector">
        <Item>
          <New id="connA" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="port">8080</Set>
            <Set name="maxIdleTime">30000</Set>
            <Set name="Acceptors">1</Set>
            <Set name="name">connA</Set>
          </New>
        </Item>
        <Item>
          <New id="connB" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="port">9090</Set>
            <Set name="maxIdleTime">30000</Set>
            <Set name="Acceptors">1</Set>            
            <Set name="name">connB</Set>
          </New>
        </Item>
      </Array>
    </Set>

    <!-- set up a context provider -->
    <Call name="addLifeCycle">
      <Arg>
        <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
          <Set name="contexts"><Ref id="Contexts"/></Set>
          <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts</Set>
          <Set name="scanInterval">5</Set>
        </New>
      </Arg>
    </Call>
</Configure>

contextA.xml:

    <Configure class="org.eclipse.jetty.webapp.WebAppContext">      
      <Set name="war"><SystemProperty name="jetty.home"/>/webapps/A</Set>
      <Set name="contextPath">/webappA</Set>
      <Set name="connectorNames">
        <Array type="String">
          <Item>connA</Item>
        </Array>
      </Set>
      ...
    </Configure>

contextB.xml:

    <Configure  class="org.eclipse.jetty.webapp.WebAppContext">
      <Set name="war"><SystemProperty name="jetty.home"/>/webapps/B</Set>
      <Set name="contextPath"/webappB</Set>
      <Set name="connectorNames">
        <Array type="String">
          <Item>connB</Item>
        </Array>
      </Set>
    </Configure>

现在像往常一样启动jetty(如果你的服务器配置文件叫jetty.xml,可以从命令行省略它):

java -jar start.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值