Jetty安装学习并展示

来源:互联网 发布:淘宝怎么导出客户资料 编辑:程序博客网 时间:2024/04/27 20:38

Jetty 的基本架构

Jetty 目前的是一个比较被看好的 Servlet 引擎,它的架构比较简单,也是一个可扩展性和非常灵活的应用服务器,它有一个基本数据模型,这个数据模型就是 Handler,所有可以被扩展的组件都可以作为一个 Handler,添加到 Server 中,Jetty 就是帮你管理这些 Handler。

下图是 Jetty 的基本架构图,整个 Jetty 的核心组件由 Server 和 Connector 两个组件构成,整个 Server 组件是基于 Handler 容器工作的,它类似与 Tomcat 的 Container 容器,Jetty 与 Tomcat 的比较在后面详细介绍。Jetty 中另外一个比不可少的组件是 Connector,它负责接受客户端的连接请求,并将请求分配给一个处理队列去执行。

图 1. Jetty 的基本架构
 

开始部署安装:

1 Jetty下载地址:
http://wiki.eclipse.org/Jetty/Howto/Install_Jetty


2 添加运行jetty账号
useradd -m jetty
usermod -a -G nagcmd jetty

3 解压缩(解压缩)
解压缩直接可以使用,不需要configre也不需要make了。
mv jetty-distribution-7.6.15.v20140411 /usr/local/jetty

查看README.txt
cat /usr/local/jetty/README.txt
可以看到一些RUNNING的方法:
......
RUNNING JETTY
=============
The run directory is either the top-level of a binary release
or jetty-distribution/target/assembly-prep directory when built from
source.
To run with the default options:
  java -jar start.jar
To see the available options and the default arguments
provided by the start.ini file:
  java -jar start.jar --help
To run with extra configuration file(s) appended, eg SSL
  java -jar start.jar etc/jetty-ssl.xml
To run with properties
  java -jar start.jar jetty.port=8081
To run with extra configuration file(s) prepended, eg logging & jmx
  java -jar start.jar --pre=etc/jetty-logging.xml --pre=etc/jetty-jmx.xml
To run without the args from start.ini
  java -jar start.jar --ini OPTIONS=Server,websocket etc/jetty.xml etc/jetty-deploy.xml etc/jetty-ssl.xml
to list the know OPTIONS:
  java -jar start.jar --list-options
java -jar /usr/local/jetty_7.6.15_8100/start.jar --pre=etc/jetty-logging.xml --pre=etc/jetty-jmx.xml jetty.port=8100

我需要在启动3个jetty服务,一个服务对应一个web应用,所以直接copy3个解压缩包
 cp -r jetty jetty_8100
 cp -r jetty jetty_8200
 cp -r jetty jetty_8300
 
4, 分别启动3个应用,带上jetty.port端口:
 nohup java -jar /usr/local/jetty_7.6.15_8100/start.jar --pre=etc/jetty-logging.xml --pre=etc/jetty-jmx.xml jetty.port=8100 &
 nohup java -jar /usr/local/jetty_7.6.15_8200/start.jar --pre=etc/jetty-logging.xml --pre=etc/jetty-jmx.xml jetty.port=8200 &
 nohup java -jar /usr/local/jetty_7.6.15_8300/start.jar --pre=etc/jetty-logging.xml --pre=etc/jetty-jmx.xml jetty.port=8300 &
 
 问题在于,用这种方法start,却没有相应的办法去stop;
 比如java -jar /usr/local/jetty_7.6.15_8200/start.jar -DSTOP.PORT=8200 -DSTOP.KEY=magic --stop 的办法没有能够关闭掉jetty进程,只能手工kill ID,这种不是太保险。
 改端口如下:
将<Set name="port"><Property name="jetty.port" default="8100"/></Set>中的8080改成8100

vim /usr/local/jetty_7.6.15_8100/etc/jetty.xml    <Call name="addConnector">      <Arg>          <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">            <Set name="host"><Property name="jetty.host" /></Set>            <Set name="port"><Property name="jetty.port" default="8100"/></Set>            <Set name="maxIdleTime">300000</Set>            <Set name="Acceptors">2</Set>            <Set name="statsOn">false</Set>            <Set name="confidentialPort">8443</Set>            <Set name="lowResourcesConnections">20000</Set>            <Set name="lowResourcesMaxIdleTime">5000</Set>          </New>      </Arg>    </Call>

 

 5,进入/bin/目录,发现有jetty.sh脚本可以启动
启动start :
/usr/local/jetty_7.6.15_8100/bin/jetty.sh start
停止stop :
/usr/local/jetty_7.6.15_8100/bin/jetty.sh stop

[root@localhost etc]# /usr/local/jetty_7.6.15_8100/bin/jetty.sh stopStopping Jetty: OK[root@localhost etc]# /usr/local/jetty_7.6.15_8100/bin/jetty.sh startStarting Jetty: 2014-05-13 15:53:05.744:INFO::Redirecting stderr/stdout to /usr/local/jetty_7.6.15_8100/logs/2014_05_13.stderrout.logOK 2014年 05月 13日 星期二 15:53:09 CST[root@localhost etc]# 


有报错如下:
Starting Jetty: Already Running!!

改端口如下:
将<Set name="port"><Property name="jetty.port" default="8100"/></Set>中的8080改成8100

vim /usr/local/jetty_7.6.15_8200/etc/jetty.xml    <Call name="addConnector">      <Arg>          <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">            <Set name="host"><Property name="jetty.host" /></Set>            <Set name="port"><Property name="jetty.port" default="8200"/></Set>            <Set name="maxIdleTime">300000</Set>            <Set name="Acceptors">2</Set>            <Set name="statsOn">false</Set>            <Set name="confidentialPort">8443</Set>            <Set name="lowResourcesConnections">20000</Set>            <Set name="lowResourcesMaxIdleTime">5000</Set>          </New>      </Arg>    </Call>

依次改成8100,8200,8300再列出停止启动命令如下:
/usr/local/jetty_7.6.15_8100/bin/jetty.sh stop
/usr/local/jetty_7.6.15_8200/bin/jetty.sh stop
/usr/local/jetty_7.6.15_8300/bin/jetty.sh stop

/usr/local/jetty_7.6.15_8300/bin/jetty.sh start
/usr/local/jetty_7.6.15_8200/bin/jetty.sh start
/usr/local/jetty_7.6.15_8100/bin/jetty.sh start

启动了8100,再起8200还是报一样的错误

Starting Jetty: Already Running!!

java -jar /usr/local/jetty_7.6.15_8300/start.jar --pre=etc/jetty-logging.xml --pre=etc/jetty-jmx.xml  --stop STOP.PORT=8300 STOP.KEY=1

nohup java -jar /usr/local/jetty_7.6.15_8100/start.jar --pre=etc/jetty-logging.xml --pre=etc/jetty-jmx.xml jetty.port=8100 &

 

6,再换java -jar方式启动试试
nohup java -jar /usr/local/jetty_7.6.15_8200/start.jar  jetty.port=8200 &
java -jar /usr/local/jetty_7.6.15_8200/start.jar --STOP.PORT=8200 --STOP.KEY=magic --stop 
java -jar /usr/local/jetty_7.6.15_8200/start.jar -DSTOP.PORT=8200 -DSTOP.KEY=magic --stop

下述方法能启动3个jetty,但是无法正常stop,--stop参数没有成功,jetty进程仍然在后台运行,只能kill强行停止进程:

nohup java -jar /usr/local/jetty_7.6.15_8100/start.jar --pre=etc/jetty-logging.xml --pre=etc/jetty-jmx.xml jetty.port=8100 &nohup java -jar /usr/local/jetty_7.6.15_8200/start.jar --pre=etc/jetty-logging.xml --pre=etc/jetty-jmx.xml jetty.port=8200 &nohup java -jar /usr/local/jetty_7.6.15_8300/start.jar --pre=etc/jetty-logging.xml --pre=etc/jetty-jmx.xml jetty.port=8300 &


7,去看看 jetty.sh脚本,check下
[root@localhost bin]# /usr/local/jetty_7.6.15_8100/bin/jetty.sh check
Checking arguments to Jetty:
JETTY_HOME     =  /usr/local/jetty_7.6.15_8100
JETTY_CONF     =  /usr/local/jetty_7.6.15_8100/etc/jetty.conf
JETTY_RUN      =  /var/run
JETTY_PID      =  /var/run/jetty.pid
JETTY_PORT     = 
JETTY_LOGS     = 
START_INI      =  /usr/local/jetty_7.6.15_8100/start.ini
CONFIGS        =  etc/jetty-logging.xml etc/jetty-started.xml
JAVA_OPTIONS   =  -Djetty.state=/usr/local/jetty_7.6.15_8100/jetty.state -Djetty.home=/usr/local/jetty_7.6.15_8100 -Djava.io.tmpdir=/tmp
JAVA           =  /usr/java/jdk1.6.0_45/bin/java
CLASSPATH      =  .:/usr/java/jdk1.6.0_45/lib/tools.jar:/usr/java/jdk1.6.0_45/lib/dt.jar
RUN_CMD        =  /usr/java/jdk1.6.0_45/bin/java -Djetty.state=/usr/local/jetty_7.6.15_8100/jetty.state -Djetty.home=/usr/local/jetty_7.6.15_8100 -Djava.io.tmpdir=/tmp -jar /usr/local/jetty_7.6.15_8100/start.jar etc/jetty-logging.xml etc/jetty-started.xml

看到JETTY_PID      =  /var/run/jetty.pid,突然意识到,启动8200如果也是这样JETTY_PID      =  /var/run/jetty.pid一个pid的话,那肯定跟8100是冲突的,难怪每次只能启动一个jetty.sh,需要去看下jetty.sh的脚本里面是在哪里设置/var/run/jetty.pid的,找到了修改下带上后缀数字。

vim jetty.sh
在第343行 将 jetty.pid 改成 jetty_8200.pid
改成

if [ -z "$JETTY_PID" ]then  JETTY_PID="$JETTY_RUN/jetty_8200.pid"fiif [ -z "$JETTY_STATE" ]then  JETTY_STATE=$JETTY_HOME/jetty.state


  
8,最后正常关闭启动如下:

[root@localhost bin]# pwd/usr/local/jetty_7.6.15_8200/bin[root@localhost bin]# /usr/local/jetty_7.6.15_8100/bin/jetty.sh stopStopping Jetty: OK[root@localhost bin]# /usr/local/jetty_7.6.15_8200/bin/jetty.sh stopStopping Jetty: OK[root@localhost bin]# /usr/local/jetty_7.6.15_8300/bin/jetty.sh stopStopping Jetty: OK[root@localhost bin]# ps -eaf|grep jettyroot     20205 11980  0 20:28 pts/2    00:00:00 grep jetty[root@localhost bin]# /usr/local/jetty_7.6.15_8300/bin/jetty.sh startStarting Jetty: 2014-05-13 20:28:32.146:INFO::Redirecting stderr/stdout to /usr/local/jetty_7.6.15_8300/logs/2014_05_13.stderrout.logOK 2014年 05月 13日 星期二 20:28:33 CST[root@localhost bin]# /usr/local/jetty_7.6.15_8200/bin/jetty.sh startStarting Jetty: 2014-05-13 20:28:34.416:INFO::Redirecting stderr/stdout to /usr/local/jetty_7.6.15_8200/logs/2014_05_13.stderrout.logOK 2014年 05月 13日 星期二 20:28:37 CST[root@localhost bin]# /usr/local/jetty_7.6.15_8100/bin/jetty.sh startStarting Jetty: 2014-05-13 20:28:38.527:INFO::Redirecting stderr/stdout to /usr/local/jetty_7.6.15_8100/logs/2014_05_13.stderrout.logOK 2014年 05月 13日 星期二 20:28:41 CST[root@localhost bin]# 


9,打开 http://192.xxx.xxx.xx:8100/cargo-jetty-deployer/报错如下

HTTP ERROR 400

Problem accessing /cargo-jetty-deployer/. Reason:

    Command / is unknown
Powered by Jetty://

 

10,部署一个简单的jetty应用:
[root@localhost webapps]# mkdir test1
[root@localhost webapps]# ll
总计 27100
-rw-r--r-- 1 root root    10220 05-13 20:44 cargo-jetty-7-and-onwards-deployer-1.4.8.war
-rw-r--r-- 1 root root 26839664 05-14 15:22 imClient.war
drwxr-xr-x 3 root root     4096 03-31 22:05 META-INF
-rw-r--r-- 1 root root    14578 05-13 15:20 spdy.war
drwxr-xr-x 2 root root     4096 05-14 16:05 test1
-rw-r--r-- 1 root root   763052 05-13 15:20 test.war
-rw-r--r-- 1 root root    60014 05-14 13:31 webim_server.jar
drwxr-xr-x 3 root root     4096 03-31 22:05 WEB-INF
[root@localhost webapps]#

[root@localhost test1]# cd test1
[root@localhost test1]# vim hello.jsp
<html>
<body>
<h4>simple demo test</h4>
<%--echo hello world--%>
<%@page language="java"%>
<%="Hello World,The first jetty demo page of timman in pl"%>
</body>
</html>

重新启动jetty
[root@localhost test1]# /usr/local/jetty_7.6.15_8100/bin/jetty.sh restart
Stopping Jetty: OK
Starting Jetty: 2014-05-14 16:08:25.445:INFO::Redirecting stderr/stdout to /usr/local/jetty_7.6.15_8100/logs/2014_05_14.stderrout.log
. . . OK 2014年 05月 14日 星期三 16:08:38 CST
[root@localhost test1]#


11,查看效果显示:在浏览器里面输入网址: http://192.xxx.xxx.xx:8100/test1/hello.jsp
会在页面显示如下:
simple demo test

Hello World,The first jetty demo page of timman in pl,如下图:

 

附加总结:

(1):单纯比较 Tomcat 与 Jetty 的性能意义不是很大,只能说在某种使用场景下,它表现的各有差异。因为它们面向的使用场景不尽相同。

从架构上来看 Tomcat 在处理少数非常繁忙的连接上更有优势,也就是说连接的生命周期如果短的话,Tomcat 的总体性能更高。
而 Jetty 刚好相反,Jetty 可以同时处理大量连接而且可以长时间保持这些连接。例如像一些 web 聊天应用非常适合用 Jetty 做服务器,像淘宝的 web 旺旺就是用 Jetty 作为 Servlet 引擎。

(2)另外由于 Jetty 的架构非常简单,作为服务器它可以按需加载组件,这样不需要的组件可以去掉,这样无形可以减少服务器本身的内存开销,处理一次请求也是可以减少产生的临时对象,这样性能也会提高。另外 Jetty 默认使用的是 NIO 技术在处理 I/O 请求上更占优势,Tomcat 默认使用的是 BIO,在处理静态资源时,Tomcat 的性能不如 Jetty。

 

参考网址: http://www.ibm.com/developerworks/cn/java/j-lo-jetty/

9 0
原创粉丝点击