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

[经验分享] Jetty应用部署方式汇总

[复制链接]

尚未签到

发表于 2017-2-26 07:41:17 | 显示全部楼层 |阅读模式
  Jetty应用部署方式大概有如下几种:
  测试环境:JDK1.6,jetty-distribution-8.1.8.v20121106
  1,直接将打包后的war文件放到${JETTY_HOME}/webapps,Jetty启动时通过org.eclipse.jetty.deploy.providers.WebAppProvider去将${JETTY_HOME}/webapps里的合法web包找到并部署。当然你也可以将解压后的web包以文件夹的方式部署(在没有修改上下文的默认情况下,上下文是当前文件夹的名称。如文件夹名字是“aaa”,则上下文也为“aaa”,如果文件夹名称是“aaa.war”,则上下文是“aaa.war”,而不是“aaa”)。
  2,通过修改${JETTY_HOME}/contents下默认自带的test.xml文件来部署你的应用。当然你可添加一个类似于test.xml文件来部署。
  test.xml内容如下:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<!-- ==================================================================
Configure and deploy the test web application in $(jetty.home)/webapps/test
Note. If this file did not exist or used a context path other that /test
then the default configuration of jetty.xml would discover the test
webapplication with a WebAppDeployer.  By specifying a context in this
directory, additional configuration may be specified and hot deployments
detected.
===================================================================== -->
<Configure class="org.eclipse.jetty.webapp.WebAppContext">

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- Required minimal context configuration :                        -->
<!--  + contextPath                                                  -->
<!--  + war OR resourceBase                                          -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<Set name="contextPath">/</Set>
<Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test.war</Set>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- Optional context configuration                                  -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<Set name="extractWAR">true</Set>
<Set name="copyWebDir">false</Set>
<Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set>
<Set name="overrideDescriptor"><SystemProperty name="jetty.home" default="."/>/contexts/test.d/override-web.xml</Set>
<!-- virtual hosts
<Set name="virtualHosts">
<Array type="String">
<Item>www.myVirtualDomain.com</Item>
<Item>localhost</Item>
<Item>127.0.0.1</Item>
</Array>
</Set>
-->
<!-- disable cookies
<Get name="sessionHandler">
<Get name="sessionManager">
<Set name="usingCookies" type="boolean">false</Set>
</Get>
</Get>
-->
<Get name="securityHandler">
<Set name="loginService">
<New class="org.eclipse.jetty.security.HashLoginService">
<Set name="name">Test Realm</Set>
<Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set>
<!-- To enable reload of realm when properties change, uncomment the following lines -->
<!-- changing refreshInterval (in seconds) as desired                                -->
<!--
<Set name="refreshInterval">5</Set>
<Call name="start"></Call>
-->
</New>
</Set>
<Set name="authenticator">
<New class="org.eclipse.jetty.security.authentication.FormAuthenticator">
<Set name="alwaysSaveUri">true</Set>
</New>
</Set>
<Set name="checkWelcomeFiles">true</Set>
</Get>
<!-- Non standard error page mapping -->
<!--
<Get name="errorHandler">
<Call name="addErrorPage">
<Arg type="int">500</Arg>
<Arg type="int">599</Arg>
<Arg type="String">/dump/errorCodeRangeMapping</Arg>
</Call>
</Get>
-->
<!-- Add context specific logger
<Set name="handler">
<New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler">
<Set name="requestLog">
<New id="RequestLogImpl" class="org.eclipse.jetty.server.NCSARequestLog">
<Set name="filename"><Property name="jetty.logs" default="./logs"/>/test-yyyy_mm_dd.request.log</Set>
<Set name="filenameDateFormat">yyyy_MM_dd</Set>
<Set name="append">true</Set>
<Set name="LogTimeZone">GMT</Set>
</New>
</Set>
</New>
</Set>
-->
</Configure>

  其中<Set name="contextPath">/aaa</Set>指应用上下文,而  <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test.war</Set>则指web包所在地,它可以指向一个任意非Jetty下的web文件夹。如:<Set name="war">E:/workspace/demo/src/main/webapp</Set>
DSC0000.gif 这两种方式的部署在${JETTY_HOME}/etc/jetty-webapps.xml中都有定义 :


<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<!-- =============================================================== -->
<!-- Add a WebAppProvider to the deployment manager                  -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- This scans the webapps directory for war files and directories  -->
<!-- to deploy.                                                      -->
<!-- This configuration must be used with jetty-deploy.xml, which    -->
<!-- creates the deployment manager instance                         -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Ref id="DeploymentManager">
<Call id="webappprovider" name="addAppProvider">
<Arg>
<New class="org.eclipse.jetty.deploy.providers.WebAppProvider">
<Set name="monitoredDirName"><Property name="jetty.home" default="." />/webapps</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</Set>
<Set name="extractWars">true</Set>
</New>
</Arg>
</Call>
</Ref>
</Configure>

  从这里可以看到,webapps和contexts的路径都是可以自定义的,但不推荐,惯例编程才是最好的!
  下面再扩展几种方式,虽然看起来不太一样,但实际上都是一样的,只是有的采用程序代码来描述部署信息,有的采用工具生成部署信息。
  3,Java程序部署Jetty应用。

package com.jerval.demo;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.webapp.WebAppContext;
public class Main {
public static void main(String[] args) throws Exception {
Server server = new Server();
Connector connector = new SelectChannelConnector();
connector.setPort(9080);
server.setConnectors(new Connector[] {connector});
WebAppContext context = new WebAppContext("E:/workspace/aaa/WebContent", "/aaa");
server.setHandler(context);
server.setStopAtShutdown(true);
server.setSendServerVersion(true);
server.start();
server.join();
}
}

  pomxml:

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.10.v20130312</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>8.1.10.v20130312</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.10.v20130312</version>
</dependency>
  4,通过Jetty Eclipse插件run-jetty-run来部署应用并启动Jetty。
  插件下载地址:http://run-jetty-run.googlecode.com/svn/trunk/updatesite 。完成后当你在Eclipse里的web应用上右键“Run As”时,你会看到“Run Jetty”,点击即可运行Jetty。你也可以通过“Run Configuration...”来修改Jetty默认配置。
  5,通过Maven插件来部署应用并启动Jetty:

<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.5.v20120716</version>
<configuration>
<stopPort>9966</stopPort>
<stopKey>foo</stopKey>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>9080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
<webApp>
<contextPath>/aaa</contextPath>
</webApp>
</configuration>
</plugin>
  然后使用命令:mvn jetty:run来完成Maven打包部署应用并启动Jetty。配置中提供了修改端口的方式,当然你也可以通过命令来修改:mvn -Djetty.port=9080 jetty:run 。
  6,通过Jetty WTP Plugin来部署应用并启动Jetty:
  下载Jetty WTP Plugin:http://download.eclipse.org/jetty/updates/jetty-wtp. 通过Eclipse的添加插件方式来安装.更请参考http://wiki.eclipse.org/Jetty_WTP_Plugin/Jetty_WTP_Install

运维网声明 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-347195-1-1.html 上篇帖子: 修改maven+jetty启动端口 下篇帖子: maven2中配置jetty
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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