天天看點

tomcat-session-redis配置(session共享)

tomcat-session-redis配置

Session持久機制三種實作方法:

1、session綁定:始終将來自同一個源IP的請求定向至同一個RS;沒有容錯能力;有損均衡效果(sh)

2、session複制:在RS之間同步session,每個RS擁有叢集中的所有的session;對規模叢集不适用;必須RS支援(lblcr)

3、session伺服器:利用單獨部署的伺服器來統一管理叢集中的session;(單有單點故障)

nginx-tomcat-redis(session共享)配置執行個體:

環境:兩台機器:

    10.15.51.141:tomcatA

    10.15.51.222:tomcatB、nginx、redis

步驟:

1. 所需要的包,放入到tomcat目錄的lib目錄下(附件中)

commons-pool-1.6.jar

jedis-2.1.0.jar

tomcat-redis-session-manager-1.2-tomcat-7.jar

2. 配置tomcat目錄下的conf/context.xml,加入以下内容:

<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
        <Manager className="com.radiadesign.catalina.session.RedisSessionManager"
                host="10.15.51.222"       #redis位址
                port="6379"                #redis端口
                database="0"
                maxInactiveInterval="60"/> #session失效時間(秒)      

3. nginx配置:

http{
    upstream aaa {
    server 10.15.51.222:8080;
    server 10.15.51.141:8080;
    }
server {
    server_name aaa.test.com;
    listen 80;
    location {
access_log  logs/ning_access.txt;
        proxy_pass http://aaa;
        }
    }
}      

4、在兩台tomcat中添加以下内容友善測試

A提供測試:

# mkdir -pv test/WEB-INF/{classes,lib}
# vim test/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="blue">Tomcata.10.15.51.222.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("ning.com","ning.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>      

B提供測試:

# mkdir -pv test/WEB-INF/{classes,lib}
# vim test/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="blue">TomcatB.10.15.51.141.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("ning.com","ning.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>