阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Jetty安装学习并展示

102次阅读
没有评论

共计 14340 个字符,预计需要花费 36 分钟才能阅读完成。

Jetty 的基本架构

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

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

推荐阅读:

使用 Jetty 作为嵌入式服务器 http://www.linuxidc.com/Linux/2013-07/86983.htm

Jetty 源码分析 http://www.linuxidc.com/Linux/2013-10/90986.htm

图 1. Jetty 的基本架构
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

  1. vim /usr/local/jetty_7.6.15_8100/etc/jetty.xml
  2. <Callname=“addConnector”>
  3. <Arg>
  4. <Newclass=“org.eclipse.jetty.server.nio.SelectChannelConnector”>
  5. <Setname=“host”><Propertyname=“jetty.host”/></Set>
  6. <Setname=“port”><Propertyname=“jetty.port”default=“8100”/></Set>
  7. <Setname=“maxIdleTime”>300000</Set>
  8. <Setname=“Acceptors”>2</Set>
  9. <Setname=“statsOn”>false</Set>
  10. <Setname=“confidentialPort”>8443</Set>
  11. <Setname=“lowResourcesConnections”>20000</Set>
  12. <Setname=“lowResourcesMaxIdleTime”>5000</Set>
  13. </New>
  14. </Arg>
  15. </Call> 

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-05/101993p2.htm 

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

  1. [root@localhost etc]# /usr/local/jetty_7.6.15_8100/bin/jetty.sh stop
  2. Stopping Jetty: OK
  3. [root@localhost etc]# /usr/local/jetty_7.6.15_8100/bin/jetty.sh start
  4. Starting 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.log
  5. OK 2014 年 05 月 13 日 星期二 15:53:09 CST
  6. [root@localhost etc]#

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

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

  1. vim /usr/local/jetty_7.6.15_8200/etc/jetty.xml
  2. <Callname=“addConnector”>
  3. <Arg>
  4. <Newclass=“org.eclipse.jetty.server.nio.SelectChannelConnector”>
  5. <Setname=“host”><Propertyname=“jetty.host”/></Set>
  6. <Setname=“port”><Propertyname=“jetty.port”default=“8200”/></Set>
  7. <Setname=“maxIdleTime”>300000</Set>
  8. <Setname=“Acceptors”>2</Set>
  9. <Setname=“statsOn”>false</Set>
  10. <Setname=“confidentialPort”>8443</Set>
  11. <Setname=“lowResourcesConnections”>20000</Set>
  12. <Setname=“lowResourcesMaxIdleTime”>5000</Set>
  13. </New>
  14. </Arg>
  15. </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 强行停止进程:

  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 &
  2. 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 &
  3. 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
改成

  1. if [-z “$JETTY_PID”]
  2. then
  3. JETTY_PID=“$JETTY_RUN/jetty_8200.pid”
  4. fi
  5. if [-z “$JETTY_STATE”]
  6. then
  7. JETTY_STATE=$JETTY_HOME/jetty.state


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

  1. [root@localhost bin]# pwd
  2. /usr/local/jetty_7.6.15_8200/bin
  3. [root@localhost bin]# /usr/local/jetty_7.6.15_8100/bin/jetty.sh stop
  4. Stopping Jetty: OK
  5. [root@localhost bin]# /usr/local/jetty_7.6.15_8200/bin/jetty.sh stop
  6. Stopping Jetty: OK
  7. [root@localhost bin]# /usr/local/jetty_7.6.15_8300/bin/jetty.sh stop
  8. Stopping Jetty: OK
  9. [root@localhost bin]# ps -eaf|grep jetty
  10. root 20205 11980 0 20:28 pts/2 00:00:00 grep jetty
  11. [root@localhost bin]# /usr/local/jetty_7.6.15_8300/bin/jetty.sh start
  12. Starting 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.log
  13. OK 2014 年 05 月 13 日 星期二 20:28:33 CST
  14. [root@localhost bin]# /usr/local/jetty_7.6.15_8200/bin/jetty.sh start
  15. Starting 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.log
  16. OK 2014 年 05 月 13 日 星期二 20:28:37 CST
  17. [root@localhost bin]# /usr/local/jetty_7.6.15_8100/bin/jetty.sh start
  18. Starting 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.log
  19. OK 2014 年 05 月 13 日 星期二 20:28:41 CST
  20. [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,如下图:

Jetty 安装学习并展示

 

附加总结:

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

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

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

Jetty 的详细介绍:请点这里
Jetty 的下载地址:请点这里

Jetty 的基本架构

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

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

推荐阅读:

使用 Jetty 作为嵌入式服务器 http://www.linuxidc.com/Linux/2013-07/86983.htm

Jetty 源码分析 http://www.linuxidc.com/Linux/2013-10/90986.htm

图 1. Jetty 的基本架构
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

  1. vim /usr/local/jetty_7.6.15_8100/etc/jetty.xml
  2. <Callname=“addConnector”>
  3. <Arg>
  4. <Newclass=“org.eclipse.jetty.server.nio.SelectChannelConnector”>
  5. <Setname=“host”><Propertyname=“jetty.host”/></Set>
  6. <Setname=“port”><Propertyname=“jetty.port”default=“8100”/></Set>
  7. <Setname=“maxIdleTime”>300000</Set>
  8. <Setname=“Acceptors”>2</Set>
  9. <Setname=“statsOn”>false</Set>
  10. <Setname=“confidentialPort”>8443</Set>
  11. <Setname=“lowResourcesConnections”>20000</Set>
  12. <Setname=“lowResourcesMaxIdleTime”>5000</Set>
  13. </New>
  14. </Arg>
  15. </Call> 

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-05/101993p2.htm 

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计14340字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中