`
weifly
  • 浏览: 235319 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

 
阅读更多

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
分享到:
评论
2 楼 weifly 2012-05-31  
lge9101230 写道
我在配置完了以后 java -jar start.jar jettyA.xml 用着运行。但是java.io.FileNotFoundException: Unable to find XML Config: etc/jetty.xml。为什么还是默认的找jetty.xml这个文件而不是找我输入的jettyA.xml。


jetty目录中的start.ini中默认引入了很多配置文件:etc/jetty.xml, etc/jetty-deploy.xml 等等
1 楼 lge9101230 2012-05-17  
我在配置完了以后 java -jar start.jar jettyA.xml 用着运行。但是java.io.FileNotFoundException: Unable to find XML Config: etc/jetty.xml。为什么还是默认的找jetty.xml这个文件而不是找我输入的jettyA.xml。

相关推荐

    eclipse jetty插件run-jetty-run-1.3.3

    eclipse jetty插件,从...下载run-jetty-run.zip文件,解压后再编写个links文件丢到eclipse的dropins目录下即可,省去了使用eclipse update方式安装的麻烦。 link文件样例如: path=d:\\eclipse_plugins\\run-jetty-run

    jetty-util-6.1.26-API文档-中英对照版.zip

    赠送jar包:jetty-util-6.1.26.jar; 赠送原API文档:jetty-util-6.1.26-javadoc.jar; 赠送源代码:jetty-util-6.1.26-sources.jar; 赠送Maven依赖信息文件:jetty-util-6.1.26.pom; 包含翻译后的API文档:jetty-...

    run-jetty-run 1.3.5eclipse插件包

    安装方法为新加一个run-jetty-run.link文件,文件内容为 path=G:\\eclipse_plugins\\run-jetty-run 执行解压后的路径文件夹即可。 在线安装地址为: http://xzer.github.io/run-jetty-run-updatesite/nightly/ 支持...

    jetty-http-9.4.11.v20180605-API文档-中英对照版.zip

    赠送jar包:jetty-http-9.4.11.v20180605.jar; 赠送原API文档:jetty-http-9.4.11.v20180605-javadoc.jar; 赠送源代码:jetty-http-9.4.11.v20180605-sources.jar; 赠送Maven依赖信息文件:jetty-...

    jetty-server-8.1.8-API文档-中英对照版.zip

    赠送jar包:jetty-server-8.1.8.jar; 赠送原API文档:jetty-server-8.1.8-javadoc.jar; 赠送源代码:jetty-server-8.1.8-sources.jar; 赠送Maven依赖信息文件:jetty-server-8.1.8.pom; 包含翻译后的API文档:...

    jetty-all-9.4.47.v20220610-uber.jar

    jetty-all-9.4.47.v20220610-uber.jar

    jetty相关的全部jar包

    jetty-security-9.4.8.v20171121.jar,jetty-io-9.4.8.v20171121.jar,jetty-continuation-9.4.8.v20171121.jar,jetty-client-9.4.8.v20171121.jar,jetty-jmx-9.4.8.v20171121.jar,jetty-plus-9.4.8.v20171121....

    jetty-server-8.1.8.v20121106-API文档-中文版.zip

    赠送jar包:jetty-server-8.1.8.v20121106.jar; 赠送原API文档:jetty-server-8.1.8.v20121106-javadoc.jar; 赠送源代码:jetty-server-8.1.8.v20121106-sources.jar; 赠送Maven依赖信息文件:jetty-server-8.1.8...

    jetty-security-9.3.19.v20170502-API文档-中英对照版.zip

    赠送jar包:jetty-security-9.3.19.v20170502.jar; 赠送原API文档:jetty-security-9.3.19.v20170502-javadoc.jar; 赠送源代码:jetty-security-9.3.19.v20170502-sources.jar; 赠送Maven依赖信息文件:jetty-...

    jetty-util-6.1.26-API文档-中文版.zip

    赠送jar包:jetty-util-6.1.26.jar; 赠送原API文档:jetty-util-6.1.26-javadoc.jar; 赠送源代码:jetty-util-6.1.26-sources.jar; 赠送Maven依赖信息文件:jetty-util-6.1.26.pom; 包含翻译后的API文档:jetty-...

    jetty-io-9.4.8.v20171121-API文档-中文版.zip

    赠送jar包:jetty-io-9.4.8.v20171121.jar; 赠送原API文档:jetty-io-9.4.8.v20171121-javadoc.jar; 赠送源代码:jetty-io-9.4.8.v20171121-sources.jar; 赠送Maven依赖信息文件:jetty-io-9.4.8.v20171121.pom;...

    jetty-webapp-9.3.19.v20170502-API文档-中文版.zip

    赠送jar包:jetty-webapp-9.3.19.v20170502.jar; 赠送原API文档:jetty-webapp-9.3.19.v20170502-javadoc.jar; 赠送源代码:jetty-webapp-9.3.19.v20170502-sources.jar; 赠送Maven依赖信息文件:jetty-webapp-...

    jetty-io-9.4.43.v20210629-API文档-中文版.zip

    赠送jar包:jetty-io-9.4.43.v20210629.jar; 赠送原API文档:jetty-io-9.4.43.v20210629-javadoc.jar; 赠送源代码:jetty-io-9.4.43.v20210629-sources.jar; 赠送Maven依赖信息文件:jetty-io-9.4.43.v20210629....

    jetty-sslengine-6.1.26-API文档-中英对照版.zip

    赠送jar包:jetty-sslengine-6.1.26.jar; 赠送原API文档:jetty-sslengine-6.1.26-javadoc.jar; 赠送源代码:jetty-sslengine-6.1.26-sources.jar; 赠送Maven依赖信息文件:jetty-sslengine-6.1.26.pom; 包含...

    jetty-server-8.1.8-API文档-中文版.zip

    赠送jar包:jetty-server-8.1.8.jar; 赠送原API文档:jetty-server-8.1.8-javadoc.jar; 赠送源代码:jetty-server-8.1.8-sources.jar; 赠送Maven依赖信息文件:jetty-server-8.1.8.pom; 包含翻译后的API文档:...

    jetty-util-9.4.43.v20210629-API文档-中文版.zip

    赠送jar包:jetty-util-9.4.43.v20210629.jar; 赠送原API文档:jetty-util-9.4.43.v20210629-javadoc.jar; 赠送源代码:jetty-util-9.4.43.v20210629-sources.jar; 赠送Maven依赖信息文件:jetty-util-9.4.43.v...

    jetty-sslengine-6.1.26-API文档-中文版.zip

    赠送jar包:jetty-sslengine-6.1.26.jar; 赠送原API文档:jetty-sslengine-6.1.26-javadoc.jar; 赠送源代码:jetty-sslengine-6.1.26-sources.jar; 赠送Maven依赖信息文件:jetty-sslengine-6.1.26.pom; 包含...

    jetty-6.1.26-API文档-中文版.zip

    赠送jar包:jetty-6.1.26.jar; 赠送原API文档:jetty-6.1.26-javadoc.jar; 赠送源代码:jetty-6.1.26-sources.jar; 赠送Maven依赖信息文件:jetty-6.1.26.pom; 包含翻译后的API文档:jetty-6.1.26-javadoc-API...

    jetty-http-7.4.2.v20110526.jar

    jetty-http-7.4.2.v20110526.jar jetty-http 服务jar包

    jetty-continuation-8.1.8.v20121106-API文档-中文版.zip

    赠送jar包:jetty-continuation-8.1.8.v20121106.jar; 赠送原API文档:jetty-continuation-8.1.8.v20121106-javadoc.jar; 赠送源代码:jetty-continuation-8.1.8.v20121106-sources.jar; 赠送Maven依赖信息文件:...

Global site tag (gtag.js) - Google Analytics