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-8080\webapps\ROOT、apache-tomcat-7.0.82-8082\webapps\ROOT、apache-tomcat-7.0.82-8083\webapps\ROOT目錄下,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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM