【Tomcat】Tomcat Session在Redis共享


參考的優秀文章

簡單地配置Tomcat Session在Redis共享

我使用的是現有的框架,見:Redis-backed non-sticky session store for Apache Tomcat
Tomcat使用的是apache-tomcat-7.0.69

一、首先,下載好所需的包,放入Tomcat的lib目錄下,我使用的是:

  • jedis-2.8.2.jar
  • tomcat-redis-session-manager1.2.jar
  • commons-pool2-2.4.2.jar

二、需在%TOMCAT_HOME%/conf/context.xml加配置。ValueManager節點的配置是新添加的。

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
	
	<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="192.168.1.9" 
         port="6379" 
         database="0" 
         maxInactiveInterval="60" />

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>

三、既然要將Session放在Redis中,那當然要搭建Redis了。可參考:【Redis】Redis的基本安裝及使用

四、編寫應用包含寫、讀Session的JSP文件,然后部署在兩個Tomcat中。
這里分別是寫、讀、刪Session的兩個JSP:

<%@ page import="java.util.Date"%>
<%@ 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=UTF-8">
<title>Write session</title>
</head>
<body>
Server : Server 1 (因需將頁面部署在不同Tomcat,以測試能讀取另一台Tomcat設置的Session,請自行設服務器標識)<br />
Write session
<%
request.getSession().setAttribute("userId", new Date().getTime());
%>
</body>
</html>
<%@ 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=UTF-8">
<title>Read session</title>
</head>
<body>
Server : Server 1 (因需將頁面部署在不同Tomcat,以測試能讀取另一台Tomcat設置的Session,請自行設服務器標識)<br />
<%=request.getSession().getAttribute("userId")%>
</body>
</html>
<%@ page import="java.util.Date"%>
<%@ 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=UTF-8">
<title>Write session</title>
</head>
<body>
Server : Server 1 (因需將頁面部署在不同Tomcat,以測試能讀取另一台Tomcat設置的Session,請自行設服務器標識)<br />
Remove session
<%
request.getSession().removeAttribute("userId");
%>
</body>
</html>

五、訪問一個Tomcat的write.jsp后,再訪問另一個Tomcat的read.jsp,看是否能讀取另一個Tomcat寫入的Session。另外,可以用Redis自帶的客戶端查看是否寫入了鍵值,我的日志如下:

127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> 
127.0.0.1:6379> keys *
1) "5A42DFAD0F66D7D87C85498628168A03"
127.0.0.1:6379> 
127.0.0.1:6379> get 5A42DFAD0F66D7D87C85498628168A03
"\xac\xed\x00\x05sr\x00Dcom.orangefunction.tomcat.redissessions.SessionSerializationMetadataB\xd9\xd9\xf7v\xa2\xdbL\x03\x00\x01[\x00\x15sessionAttributesHasht\x00\x02[Bxpw\x14\x00\x00\x00\x10_\xcf\xe5\xbe\x94N\x8a\xa7\a=66_\xf6\x15\xdcxsr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x01V\x8e\xa4&\x9asq\x00~\x00\x03\x00\x00\x01V\x8e\xa4&\x9asr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexq\x00~\x00\x04\x00\x00\a\bsr\x00\x11java.lang.Boolean\xcd r\x80\xd5\x9c\xfa\xee\x02\x00\x01Z\x00\x05valuexp\x01q\x00~\x00\nsq\x00~\x00\x03\x00\x00\x01V\x8e\xa4&\xa3t\x00 5A42DFAD0F66D7D87C85498628168A03sq\x00~\x00\a\x00\x00\x00\x01t\x00\x06userIdsq\x00~\x00\x03\x00\x00\x01V\x8e\xa4&\xa3w\b\x00\x00\x01V\x8e\xa4&\x9a"
127.0.0.1:6379> 

RedisSessionManager的類圖


關於PersistentManager、FileStore、JDBCStore參見【Session】Tomcat中Session持久化到文件系統或數據庫


免責聲明!

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



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