nginx+tomcat7+redis session共享

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

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

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

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

修改兩tomcat的context.xml:

單點配置

<Context>

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

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

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

        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共享。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章