zoukankan      html  css  js  c++  java
  • Jetty安装学习并展示

    Jetty 的基本架构

    Jetty 眼下的是一个比較被看好的 Servlet 引擎,它的架构比較简单,也是一个可扩展性和很灵活的应用server,它有一个基本数据模型,这个数据模型就是 Handler,全部能够被扩展的组件都能够作为一个 Handler,加入到 Server 中,Jetty 就是帮你管理这些 Handler。

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

    图 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 stop
    Stopping Jetty: OK
    [root@localhost etc]# /usr/local/jetty_7.6.15_8100/bin/jetty.sh start
    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
    OK 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"
    fi
    
    if [ -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 stop
    Stopping Jetty: OK
    [root@localhost bin]# /usr/local/jetty_7.6.15_8200/bin/jetty.sh stop
    Stopping Jetty: OK
    [root@localhost bin]# /usr/local/jetty_7.6.15_8300/bin/jetty.sh stop
    Stopping Jetty: OK
    [root@localhost bin]# ps -eaf|grep jetty
    root     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 start
    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
    OK 2014年 05月 13日 星期二 20:28:33 CST
    [root@localhost bin]# /usr/local/jetty_7.6.15_8200/bin/jetty.sh start
    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
    OK 2014年 05月 13日 星期二 20:28:37 CST
    [root@localhost bin]# /usr/local/jetty_7.6.15_8100/bin/jetty.sh start
    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
    OK 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 做server,像淘宝的 web 旺旺就是用 Jetty 作为 Servlet 引擎。

    (2)另外由于 Jetty 的架构很easy,作为server它能够按需载入组件,这样不须要的组件能够去掉,这样无形能够降低server本身的内存开销,处理一次请求也是能够降低产生的暂时对象,这样性能也会提高。另外 Jetty 默认使用的是 NIO 技术在处理 I/O 请求上更占优势,Tomcat 默认使用的是 BIO,在处理静态资源时,Tomcat 的性能不如 Jetty。

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

  • 相关阅读:
    《淘宝网》为例,描述质量属性的六个常见属性场景
    《苏宁安全架构演进及实践》阅读总结
    《12306核心模型设计思路和架构设计》阅读心得
    放假个人总结七
    放假个人总结六
    放假个人总结五
    连接Oracle提示 ORA-28009: 应当以 SYSDBA 身份或 SYSOPER 身份建立 SYS 连接
    连接oracle无匹配协议
    CentOS 7下虚拟机没有网络
    CentOS 7安装时候没装ifconfig命令
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3991477.html
Copyright © 2011-2022 走看看