Jetty Session Persistence By Redis

Copy jar 到$JETTY_HOME/lib/ext目录下

-rw-rw-r--. 1 conversant conversant  100193 Sep 11 17:34 commons-pool-1.5.5.jar
-rw-rw-r--. 1 conversant conversant  228268 Sep 11 17:34 jackson-core-asl-1.9.3.jar
-rw-rw-r--. 1 conversant conversant  773019 Sep 11 17:34 jackson-mapper-asl-1.9.3.jar
-rw-r--r--. 1 conversant conversant  125841 Sep 11 17:34 jedis-2.0.0.jar
-rw-rw-r--. 1 conversant conversant 1991094 Sep 11 17:34 jetty-session-redis-2.4.ga-SNAPSHOT-all.jar
-rw-r--r--. 1 conversant conversant   93525 Sep 11 17:34 logback-access-1.1.2.jar
-rw-r--r--. 1 conversant conversant  270747 Sep 11 17:34 logback-classic-1.1.2.jar
-rw-r--r--. 1 conversant conversant  427729 Sep 11 17:34 logback-core-1.1.2.jar
-rw-rw-r--. 1 conversant conversant  361155 Sep 11 17:34 trove-1.0.2.jar
-rw-rw-r--. 1 conversant conversant   24956 Sep 11 17:34 xpp3_min-1.1.4c.jar
-rw-rw-r--. 1 conversant conversant  431406 Sep 11 17:34 xstream-1.3.1.jar

 

准备jetty-cluster.xml

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- =============================================================== -->
<!-- Documentation of this file format can be found at:              -->
<!-- http://wiki.eclipse.org/Jetty/Reference/jetty.xml_syntax        -->
<!--                                                                 -->
<!-- Additional configuration files are available in $JETTY_HOME/etc -->
<!-- and can be mixed in. See start.ini file for the default         -->
<!-- configuration files.                                            -->
<!--                                                                 -->
<!-- For a description of the configuration mechanism, see the       -->
<!-- output of:                                                      -->
<!--   java -jar start.jar -?                                        -->
<!-- =============================================================== -->

<!-- =============================================================== -->
<!-- Configure a Jetty Server instance with an ID "Server"           -->
<!-- Other configuration files may also configure the "Server"       -->
<!-- ID, in which case they are adding configuration to the same     -->
<!-- instance.  If other configuration have a different ID, they     -->
<!-- will create and configure another instance of Jetty.            -->
<!-- Consult the javadoc of o.e.j.server.Server for all              -->
<!-- configuration that may be set here.                             -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

   <!-- =========================================================== -->
    <!-- Configure the Server Thread Pool.                           -->
    <!-- The server holds a common thread pool which is used by      -->
    <!-- default as the executor used by all connectors and servlet  -->
    <!-- dispatches.                                                 -->
    <!--                                                             -->
    <!-- Configuring a fixed thread pool is vital to controlling the -->
    <!-- maximal memory footprint of the server and is a key tuning  -->
    <!-- parameter for tuning.  In an application that rarely blocks -->
    <!-- then maximal threads may be close to the number of 5*CPUs.  -->
    <!-- In an application that frequently blocks, then maximal      -->
    <!-- threads should be set as high as possible given the memory  -->
    <!-- available.                                                  -->
    <!--                                                             -->
    <!-- Consult the javadoc of o.e.j.util.thread.QueuedThreadPool   -->
    <!-- for all configuration that may be set here.                 -->
    <!-- =========================================================== -->
    <Arg name="threadpool">
      <New id="threadpool" class="org.eclipse.jetty.util.thread.QueuedThreadPool">
        <Arg name="minThreads" type="int"><Property name="threads.min" default="10"/></Arg>
        <Arg name="maxThreads" type="int"><Property name="threads.max" default="200"/></Arg>
        <Arg name="idleTimeout" type="int"><Property name="threads.timeout" default="60000"/></Arg>
        <Set name="detailedDump">false</Set>
      </New>
    </Arg>

    <!-- =========================================================== -->
    <!-- Add shared Scheduler instance                               -->
    <!-- =========================================================== -->
    <Call name="addBean">
      <Arg>
        <New class="org.eclipse.jetty.util.thread.ScheduledExecutorScheduler"/>
      </Arg>
    </Call>

    <!-- =========================================================== -->
    <!-- Http Configuration.                                         -->
    <!-- This is a common configuration instance used by all         -->
    <!-- connectors that can carry HTTP semantics (HTTP, HTTPS, SPDY)-->
    <!-- It configures the non wire protocol aspects of the HTTP     -->
    <!-- semantic.                                                   -->
    <!--                                                             -->
    <!-- This configuration is only defined here and is used by      -->
    <!-- reference from the jetty-http.xml, jetty-https.xml and      -->
    <!-- jetty-spdy.xml configuration files which instantiate the    -->
    <!-- connectors.                                                 -->
    <!--                                                             -->
    <!-- Consult the javadoc of o.e.j.server.HttpConfiguration       -->
    <!-- for all configuration that may be set here.                 -->
    <!-- =========================================================== -->
    <New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
      <Set name="secureScheme">https</Set>
      <Set name="securePort"><Property name="jetty.https.port" default="8443" /></Set>
      <Set name="outputBufferSize">32768</Set>
      <Set name="requestHeaderSize">8192</Set>
      <Set name="responseHeaderSize">8192</Set>
      <Set name="sendServerVersion">true</Set>
      <Set name="sendDateHeader">false</Set>
      <Set name="headerCacheSize">512</Set>

      <!-- Uncomment to enable handling of X-Forwarded- style headers
      <Call name="addCustomizer">
        <Arg><New class="org.eclipse.jetty.server.ForwardedRequestCustomizer"/></Arg>
      </Call>
      -->
    </New>

    <Call name="addConnector">
        <Arg>
          <New class="org.eclipse.jetty.server.ServerConnector">
            <Arg name="server"><Ref refid="Server" /></Arg>
            <Arg name="factories">
              <Array type="org.eclipse.jetty.server.ConnectionFactory">
                <Item>
                  <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                    <Arg name="config"><Ref refid="httpConfig" /></Arg>
                  </New>
                </Item>
              </Array>
            </Arg>
            <Set name="host"><Property name="jetty.host" /></Set>
            <Set name="port"><Property name="jetty.port" default="8080" /></Set>
            <Set name="idleTimeout"><Property name="http.timeout" default="30000"/></Set>
          </New>
        </Arg>
      </Call>

      <Call id="httpsConnector" name="addConnector">
        <Arg>
          <New class="org.eclipse.jetty.server.ServerConnector">
            <Arg name="server"><Ref refid="Server" /></Arg>
              <Arg name="factories">
                <Array type="org.eclipse.jetty.server.ConnectionFactory">
                  <Item>
                    <New class="org.eclipse.jetty.server.SslConnectionFactory">
                      <Arg name="next">http/1.1</Arg>
                      <Arg name="sslContextFactory"><Ref refid="sslContextFactory"/></Arg>
                    </New>
                  </Item>
                  <Item>
                    <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                      <Arg name="config"><Ref refid="sslHttpConfig"/></Arg>
                    </New>
                  </Item>
                </Array>
              </Arg>
              <Set name="host"><Property name="jetty.host" /></Set>
              <Set name="port"><Property name="jetty.https.port" default="8443" /></Set>
              <Set name="idleTimeout">30000</Set>
            </New>
        </Arg>
      </Call>

    <!-- =========================================================== -->
    <!-- Set the default handler structure for the Server            -->
    <!-- A handler collection is used to pass received requests to   -->
    <!-- both the ContextHandlerCollection, which selects the next   -->
    <!-- handler by context path and virtual host, and the           -->
    <!-- DefaultHandler, which handles any requests not handled by   -->
    <!-- the context handlers.                                       -->
    <!-- Other handlers may be added to the "Handlers" collection,   -->
    <!-- for example the jetty-requestlog.xml file adds the          -->
    <!-- RequestLogHandler after the default handler                 -->
    <!-- =========================================================== -->
    <Set name="handler">
      <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
        <Set name="handlers">
         <Array type="org.eclipse.jetty.server.Handler">
             <!--
           <Item>
             <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
           </Item>
           <Item>
             <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
           </Item>
            -->
            <Item>
               <New class="org.eclipse.jetty.webapp.WebAppContext">
                    <Set name="contextPath">/</Set>
                    <Set name="war"><SystemProperty name="project.home" default="."/>/webapps</Set>
                    <Set name="defaultsDescriptor"><SystemProperty name="project.home" default="."/>/conf/jetty/webdefault.xml</Set>
                    <Set name="extractWAR">true</Set>
                    <Set name="copyWebDir">false</Set>
                    <Call name="addServlet">
                         <Arg>org.eclipse.jetty.servlet.DefaultServlet</Arg>
                         <Arg>/</Arg>
                    </Call>
                   <Set name="sessionHandler">
                       <New class="org.eclipse.jetty.server.session.SessionHandler">
                           <Arg>
                               <New class="com.ovea.jetty.session.redis.RedisSessionManager">
                                   <Arg>session/redis</Arg>
                                   <Arg>
                                       <New class="com.ovea.jetty.session.serializer.JsonSerializer"/>
                                   </Arg>
                                   <!-- set the interval in seconds to force session persistence event if it didn‘t changed. Default to 60 seconds -->
                                   <Set name="saveInterval">20</Set>
                                   <!-- set the cookie domain -->
                                   <Set name="sessionDomain">ssdev.swiftsync.com.sg</Set>
                                   <!-- set the cookie path -->
                                   <Set name="sessionPath">/</Set>
                                   <!-- set the cookie max age in seconds. Default is -1 (no max age). 1 day = 86400 seconds -->
                                   <Set name="maxCookieAge">86400</Set>
                                   <!-- set the interval in seconds to refresh the cookie max age. Default to 0. This number should be lower than the session expirity time. -->
                                   <Set name="refreshCookieAge">300</Set>
                               </New>
                           </Arg>
                       </New>
                   </Set>

               </New>
           </Item>
         </Array>
        </Set>
      </New>
    </Set>

    <!-- =========================================================== -->
    <!-- extra server options                                        -->
    <!-- =========================================================== -->
    <Set name="stopAtShutdown">true</Set>
    <Set name="stopTimeout">5000</Set>
    <Set name="dumpAfterStart"><Property name="jetty.dump.start" default="false"/></Set>
    <Set name="dumpBeforeStop"><Property name="jetty.dump.stop" default="true"/></Set>

</Configure>

 

 

准备jetty-session.xml,并且修改如下配置:

<Arg>192.168.1.12</Arg> <Arg type="int">8663</Arg>

 

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- =============================================================== -->
<!-- Configure the Jetty Request Log                                 -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
    <!--
        Configure session id management
    -->
    <Set name="sessionIdManager">
        <New class="com.ovea.jetty.session.redis.RedisSessionIdManager">
            <Arg>
                <Ref id="Server"/>
            </Arg>
            <Arg>session/redis</Arg>
            <!-- time interval to check for expired sessions in redis cache, in milliseconds. Defaults to 1 min -->
            <Set name="scavengerInterval">30000</Set>
            <!-- cluster node name -->
            <Set name="workerName">
                <SystemProperty name="jetty.node" default="web1"/>
            </Set>
        </New>
    </Set>

    <!--
        Provides a Redis Pool for session management on server and each webapp
    -->
    <New class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg>session/redis</Arg>
        <Arg>
            <New class="redis.clients.jedis.JedisPool">
                <Arg>
                    <New class="org.apache.commons.pool.impl.GenericObjectPool$Config">
                        <Set type="int" name="minIdle">5</Set>
                        <Set type="int" name="maxActive">15</Set>
                        <Set type="boolean" name="testOnBorrow">true</Set>
                    </New>
                </Arg>
                <Arg>192.168.1.12</Arg>
                <Arg type="int">8663</Arg>
            </New>
        </Arg>
    </New>

</Configure>

 

 

Modify jetty 启动脚本

JETTY_ARGS="lib=$JETTY_HOME/lib $APP_HOME/conf/jetty/jetty-cluster.xml $APP_HOME/conf/jetty/jetty-requestlog.xml $APP_HOME/conf/jetty/jetty-session.xml OPTIONS=jsp"

 

 

参考资料

 

https://github.com/Ovea/jetty-session-redis

时间: 2024-09-28 01:17:09

Jetty Session Persistence By Redis的相关文章

nginx+tomcat7+redis实现session共享(单点redis)

本文主要介绍redis的安装和配置.nginx和tomcat已经写过一篇很详细的文档.请参考:http://francis905.blog.51cto.com/3048056/1716740 实验环境:3台虚拟机(pc1:nginx1.6.3,pc2:tomcat7+redis2.6.13,pc3:tomcat7+redis2.6.13) 实验目的:通过redis实现session的共享 [注]棕色为必须做的 其他作为资料延伸 cd /usr/local/src tar zxvf redis-2

将php的session存放到redis里面

通常我们将session存储到memcached,但是有时候老大让你将session存储到Redis怎么办,因此有必要了解下如何将session存到redis. step1.服务器上需要安装redis扩展(以php7为例) # 以安装php7为例,简单起见直接安装了全部的包 yum install php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbst

session 保存到 redis 简单实现

参考资料: [session保存到redis简单实现]http://blog.csdn.net/ppt0501/article/details/46700221 [Redis学习]http://blog.csdn.net/can007/article/details/19848559

将Session放入Redis

默认情况下,我们的PHP是以文件的形式保存Session数据,所以,每次读写会话信息,就需要去访问硬盘. 为了解决会话信息夸域名问题,即为了实现同一时刻只能一个地方登录,同时也解决读写会话信息必须访问磁盘问题,我想到了,将Session保存到Redis中. 下面贴代码: sessionRedis::setSessionHandler(); class sessionRedis{ public static $redis; public static function sessionOpen($s

配置 PHP 的 Session 存储到 Redis

配置 PHP 的 Session 存储到 Redis PHP 的会话默认是以文件的形式存在的,可以配置到 NoSQL 中,即提高了访问速度,又能很好地实现会话共享,,,爽歪歪! 配置方式如下: 方法一:修改 php.ini 的设置 ? 1 2 session.save_handler = redis session.save_path = "tcp://127.0.0.1:6379" 修改完之后,重启一下 php-fpm. 方式二:通过 ini_set() 函数设置 ? 1 2 ini

Django session存储到redis数据库

把session存储到redis数据库,需要在setting中配置 django-redis 中文文档 http://django-redis-chs.readthedocs.io/zh_CN/latest/#cache-backend 在使用django1.6+的时候,默认会吧session存放在数据库django_session表里.如果要把session放在内存中,就应该在settings.py 中配置SESSION_ENGINE = "django.contrib.sessions.ba

22_redis缓存配置及设置把session存储在redis中

django配置redis缓存 1. 安装django-redis包 pip install -i https://pypi.douban.com/simple django-redis 2. 在settings.py 文件中,指定redis缓存 # 配置redis缓存 CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION":

Session服务器之Redis

Session服务器之Redis Redis与Memcached的区别内存利用率:使用简单的key value (键值对)存储的话,Mermcached 的内存利用率更高,而如果Redis采用hash结构来做key-value存储,由于其组合式的压缩,其内存利用率会高于Memcached..性能对比:由于Redis 只使用单核,而Memcached可以使用多核,所以平均每一个核上Redis在存储小数据时比Memcached性能更高.而在100k以上的数据中,Memcached性能要高于Redis

php Session存储到Redis的方法

当然要写先安装php的扩展,可参考这篇文章:Redis及PHP扩展安装 修改php.ini的设置 复制代码 代码如下: session.save_handler = redis session.save_path = “tcp://127.0.0.1:6379″ 修改后重启php-fpm或nginx,phpinfo() session redis 如果不想修改php.ini可这样 复制代码 代码如下: ini_set(“session.save_handler”,”redis”); ini_se