设为首页 收藏本站
查看: 1324|回复: 0

[经验分享] jetty-如何让一个应用响应一个特定端口

[复制链接]

尚未签到

发表于 2017-2-27 07:49:47 | 显示全部楼层 |阅读模式
  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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-347614-1-1.html 上篇帖子: eclipse + maven + jetty + spring web 开发环境简要笔记 下篇帖子: jetty启动后无法修改静态文件的问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表