nginx+tomcat7+redis session共享

下載tomcat-Redis-session-manager相應的jar包html

    https://page61.ctfile.com/fs/Vtq167749542
java

--------------------------------------------------------------------------------------------------------------------------------------------------------------------web

下載完成後拷貝到$TOMCAT_HOME/lib中redis

修改兩tomcat的context.xml:tomcat

單點配置session

<Context>app

   <WatchedResource>WEB-INF/web.xml</WatchedResource>webapp

   <ValveclassName="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>jsp

   <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"ide

        host="192.168.254.10"

        port="6379"

        database="0"

         maxInactiveInterval="60"/>

</Context>

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

基於redis的集羣配置以下:

<ValveclassName="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>        

<ManagerclassName="com.orangefunction.tomcat.redissessions.RedisSessionManager" 

    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"/>

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

在tomcat/webapps/ROOT放一個index.jsp


<%@ page language="Java" %>  
<html>  
  <head><title>TomcatA</title></head>  
  <body>  
   
    <table align="centre" border="1">  
      <tr>  
        <td>Session ID</td>  
        <td><%= session.getId() %></td>  
      </tr>  
      <tr>  
        <td>Created on</td>  
        <td><%= session.getCreationTime() %></td>  
     </tr>  
    </table>  
  </body>  
</html>  
sessionID:<%=session.getId()%>   
<br>   
SessionIP:<%=request.getServerName()%>   
<br>   
SessionPort:<%=request.getServerPort()%>   
<%   
//爲了區分,第二個能夠是222  
out.println("This is Tomcat Server 1111");   
%>    

經過訪問http://127.0.0.1/index.jsp

能夠看到雖然Server從1111變爲2222,但session的建立時間沒有變化,這就完成了session共享。

相關文章
相關標籤/搜索