zoukankan      html  css  js  c++  java
  • Tomcat7基于redis的session共享

    一,项目需求

    因开发人员在登录后台时需要反复认证,tomcat反复切换,所以给运维组提出需求,解决session共享问题。

    二,解决方法

    环境:基于Centos6.8   

             Jdk 版本   java version "1.7.0_99"         Tomcat版本号:Server number:  7.0.82.0     

             Redis版本号:redis-3.2.0

    1,安装redis

    过于简单,此处略去。

    2,安装jdk,安装tomcat,配置两个tomcat

    在本机中配置有三个Tomcat,分别为:apache-tomcat-7.0.82-8080   apache-tomcat-7.0.82-8082  apache-tomcat-7.0.82-8083   (不同的Tomcat需要修改端口号,否则会冲突报错)

    编制这三个index.jsp页面,分别放入apache-tomcat-7.0.82-8080webappsROOT、apache-tomcat-7.0.82-8082webappsROOT、apache-tomcat-7.0.82-8083webappsROOT目录下,index.jsp页面内容如下:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>获取session id</title>
    </head>
    <body>
        Session Id : <%= request.getSession().getId() %>
    </body>
    </html>

    tomcat7-8081访问地址:http://localhost:8080,浏览显示内容:Session Id : A86BC413D12339380DD7B0079C50D9EB
    tomcat7-8082访问地址:http://localhost:8082,浏览显示内容:Session Id : 8982F60C7FF1ED2171A1BBCF8BD528JL

    tomcat7-8082访问地址:http://localhost:8083,浏览显示内容:Session Id : 8982F60C7FF1ED2171A1BBCF8BD8HJKM

     

    3,拷贝Toncat需要的jar

    将如下几个jar拷贝到${TOMCAT_HOME}/lib下   例如:/root/tomcat/apache-tomcat-7.0.82-8080/lib

    需要下载这几个jar包。

    tomcat-redis-session-manager-VERSION.jar 
    jedis-2.5.2.jar 
    commons-pool2-2.2.jar

    4,配置Tomcat

    编辑${TOMCAT_HOME}/conf/context.xml,在context中加入

    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
           host="localhostxxx"
           port="6379"
    password="xxxxxx"
    database="0" maxInactiveInterval="60" />

    其中host和port及password为redis的ip和端口和密码

    至此配置完成,tomcat会使用redis来托管session。

    5、启动tomcat并且测试

    分别启动3个Tomcat,在终端看到了如下信息,表明redis的session manager初始化成功。

    信息: Deployment of web application directory /root/tomcat/apache-tomcat-7.0.82-8080/webapps/docs has finished in 108 ms
    三月 17, 2018 5:13:03 下午 org.apache.catalina.startup.HostConfig deployDirectory
    信息: Deploying web application directory /root/tomcat/apache-tomcat-7.0.82-8080/webapps/ROOT
    三月 17, 2018 5:13:03 下午 org.apache.catalina.session.ManagerBase setMaxInactiveInterval
    警告: Manager.setMaxInactiveInterval() is deprecated and calls to this method are ignored. Session timeouts should be configured in web.xml or via Context.setSessionTimeout(int timeoutInMinutes)
    三月 17, 2018 5:13:04 下午 org.apache.catalina.startup.TldConfig execute
    信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs werefound in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    三月 17, 2018 5:13:04 下午 com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
    信息: Attached to RedisSessionHandlerValve
    三月 17, 2018 5:13:04 下午 com.orangefunction.tomcat.redissessions.RedisSessionManager initializeSerializer
    信息: Attempting to use serializer :com.orangefunction.tomcat.redissessions.JavaSerializer
    三月 17, 2018 5:13:04 下午 com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
    信息: Will expire sessions after 1800 seconds
    三月 17, 2018 5:13:04 下午 org.apache.catalina.startup.HostConfig deployDirectory
    信息: Deployment of web application directory /root/tomcat/apache-tomcat-7.0.82-8080/webapps/ROOT has finished in 225 ms
    三月 17, 2018 5:13:04 下午 org.apache.coyote.AbstractProtocol start
    信息: Starting ProtocolHandler ["http-bio-8080"]
    三月 17, 2018 5:13:04 下午 org.apache.coyote.AbstractProtocol start
    信息: Starting ProtocolHandler ["ajp-bio-8009"]
    三月 17, 2018 5:13:04 下午 org.apache.catalina.startup.Catalina start
    信息: Server startup in 2228 m

    打开浏览器,输入http://localhost:8080回车,
    打开浏览器,输入http://localhost:8082回车,

    打开浏览器,输入http://localhost:8083回车

    获取的SESSIONID是同一个,说明成功了。。。

    启动redis自身的客户端:redis-cli   -h 127.0.0.1 -p xxx
    执行"keys *",会看到SESSIONID:
    执行"get D5E4019A04709CD68F94378211DA1B60",得到SESSIONID的值。

     

    经测试,只要redis不重启,用户session就不会丢失。虽然session保存到了redis中,但是如果redis挂掉,session也会丢失。为了解决此问题,可对redis进行集群。

    友情链接:

    https://www.cnblogs.com/linjiqin/p/5761281.html

  • 相关阅读:
    leetcode_1423. 可获得的最大点数
    leetcode_剑指 Offer 06. 从尾到头打印链表
    leetcode_剑指 Offer 05. 替换空格
    leetcode_49. 字母异位词分组
    leetcode_73. 矩阵置零
    leetcode_26. 删除排序数组中的重复项
    jstack查看JVM堆栈信息
    如何画一张架构图
    百年孤独家谱
    阿尔萨斯(Arthas)入门
  • 原文地址:https://www.cnblogs.com/lixinliang/p/8591185.html
Copyright © 2011-2022 走看看