tomcat集群基于redis共享session解决方案

一、使用版本:apache-tomcat-7.0.56+nginx-1.8.0+redis-3.0.6

1、先给出正确的配置和jar包

所需jar包如下

tomcat-redis-session-manager1.2.jar

jedis-2.6.2.jar

tomcat-juli.jar

tomcat-juli-adapters.jar

commons-pool-1.5.4.jar

commons-pool2-2.4.1.jar

下载地址:http://download.csdn.net/detail/xiaoxiaosasasa/9403043

 

2、修改tomcat的配置文件conf/context.xml

 

单点配置

   WEB-INF/web.xml

   

   

        host="192.168.254.10"

        port="6379"

        database="0"

         maxInactiveInterval="60"/>

 

基于redis的集群配置如下:

       

    maxInactiveInterval="60"

    sentinelMaster="mymaster"

    sentinels="127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382"/>

 

二、搭建过程中遇到的问题

1、各个组件的下载地址:

Redis:http://redis.io/

JRedis: https://github.com/xetorthio/jedis

 

我用的是tomcat7和JDK7,

因此下载tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar

https://github.com/jcoleman/tomcat-redis-session-manager/downloads

 

Apache Commons Pool

http://commons.apache.org/proper/commons-pool/download_pool.cgi

 

2、配置启动

根据网站上https://github.com/jcoleman/tomcat-redis-session-manager

的实例:配置如图tomcat集群基于redis共享session解决方案_第1张图片

启动结果报找不到对应类,后面点开jar包一看原来类的包名改为了com.radiadesign.catalina.session


java.lang.ClassNotFoundException: com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at org.apache.tomcat.util.digester.ObjectCreateRule.begin(ObjectCreateRule.java:144)
        at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1288)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
        at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1343)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2786)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:648)
        at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561)
        at org.apache.catalina.startup.ContextConfig.processContextConfig(ContextConfig.java:649)
        at org.apache.catalina.startup.ContextConfig.contextConfig(ContextConfig.java:592)
        at org.apache.catalina.startup.ContextConfig.init(ContextConfig.java:850)
        at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:397)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
        at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:402)
        at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:110)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
        at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247)
        at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1897)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)

tomcat集群基于redis共享session解决方案_第2张图片

于是又将配置改为如下


再次启动,又报如下错误:

INFO: Starting Servlet Engine: Apache Tomcat/7.0.56
Jan 13, 2016 12:35:51 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/app/apache-tomcat-7.0.56/webapps/ROOT
Jan 13, 2016 12:35:51 AM org.apache.tomcat.util.digester.Digester startElement
SEVERE: Begin event threw error
java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    com/radiadesign/catalina/session/RedisSessionManager.initializeDatabaseConnection()V @28: invokespecial
  Reason:
    Type 'redis/clients/jedis/JedisPoolConfig' (current frame, stack[3]) is not assignable to 'org/apache/commons/pool/impl/GenericObjectPool$Config'
  Current Frame:
    bci: @28
    flags: { }
    locals: { 'com/radiadesign/catalina/session/RedisSessionManager' }
    stack: { 'com/radiadesign/catalina/session/RedisSessionManager', uninitialized 1, uninitialized 1, 'redis/clients/jedis/JedisPoolConfig', 'java/lang/String', integer, integer, 'java/lang/String' }
  Bytecode:
    0000000: 2abb 009f 59bb 00a0 59b7 00a1 2ab6 00a2
    0000010: 2ab6 00a3 2ab6 00a4 2ab6 00a5 b700 a6b5
    0000020: 0018 a700 134c 2bb6 00a7 bb00 3259 12a8
    0000030: 2bb7 00a9 bfb1                         
  Exception Handler Table:
    bci [0, 34] => handler: 37
  Stackmap Table:
    same_locals_1_stack_item_frame(@37,Object[#274])
    same_frame(@53)


        at java.lang.Class.getDeclaredConstructors0(Native Method)
        at java.lang.Class.privateGetDeclaredConstructors(Class.java:2585)
        at java.lang.Class.getConstructor0(Class.java:2885)
        at java.lang.Class.newInstance(Class.java:350)
        at org.apache.tomcat.util.digester.ObjectCreateRule.begin(ObjectCreateRule.java:145)
        at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1288)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
        at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1343)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2786)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:648)
        at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561)
        at org.apache.catalina.startup.ContextConfig.processContextConfig(ContextConfig.java:649)
        at org.apache.catalina.startup.ContextConfig.contextConfig(ContextConfig.java:592)
        at org.apache.catalina.startup.ContextConfig.init(ContextConfig.java:850)
        at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:397)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
        at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:402)
        at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:110)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
        at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247)
        at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1897)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)

tomcat集群基于redis共享session解决方案_第3张图片

于是就找了不少资料

通过这篇博客:http://blog.csdn.net/wkwanglei/article/details/47776871

重新下载了jar包:http://download.csdn.net/detail/xiaoxiaosasasa/9403043,然后session共享配置成功

 

更多资料:

http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831

http://www.cnblogs.com/interdrp/p/4868740.html

http://blog.csdn.net/wkwanglei/article/details/47776871

 


你可能感兴趣的:(环境搭建,架构)