windows下nginx+tomcat集羣,實現session複製共享

兩臺不同服務器  ip1、ip2。ip1安裝tomcat1,端口8005,8081,8009、nginx,提供nginx外網端口80,ip2安裝tomcat2,端口8006,8082,8010。

1、安裝apach tomcat。

2、安裝ng,下載解壓安裝到ip1,根據需要修改配置文件nginx.conf:

[plain] view plaincopy
  1. #user  nobody;  
  2. worker_processes  1;  
  3.   
  4. #error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. #error_log  logs/error.log  info;  
  7.   
  8. #pid        logs/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     worker_connections  1024;  
  13. }  
  14.   
  15.   
  16. http {  
  17.     include       mime.types;  
  18.     default_type  application/octet-stream;  
  19.     sendfile        on;  
  20.     keepalive_timeout  65;  
  21.   
  22.     upstream portal-driver{  
  23.     server ip1:8081 max_fails=0;  
  24.         server ip2:8082 max_fails=0;  
  25.     }  
  26.       
  27.   
  28.     server {  
  29.         listen       80;  
  30.         server_name  localhost;  
  31.   
  32.         location /drive{  
  33.             root   html;  
  34.             index  index.html index.htm;  
  35.             proxy_pass  http://portal-driver;  
  36.             proxy_redirect  default;          
  37.       }  
  38.   
  39.         location / {  
  40.             root   html;  
  41.             index  index.html index.htm;  
  42.         }  
  43.   
  44.         error_page   500 502 503 504  /50x.html;  
  45.         location = /50x.html {  
  46.             root   html;  
  47.         }  
  48.   
  49.     }  
  50.   
  51. }  
此時,如果tomcat1、2啓動,使用http://nginx.ip訪問,查看ng日誌,會發現有時候請求ip1服務器,有時候請求的是ip2。這就已經達到了ng負載的功能。

windows下nginx命令:

關閉:nginx.exe -s stop

開啓:start nging.exe

重啓 nginx.exe - s reload

注,但是此時由於tomcat1、tomcat2部署同一套工程,如果有數據直接寫往session,而且不是使用的cookie存放session的key(由於訪問地址使用ip,並不適用域名,所以沒法使用cookie),會造成兩次訪問session id不一致。此時就需要做session複製或共享。需要以下操作:

3、修改tomcat下的conf,server.xml文件(兩個tomcat修改一致):

將tomcat集羣註釋cluster打開。

修改節點<Engine name="Catalina" defaultHost="localhost" jvmRoute="driver1">。(tomcat1跟tomcat2指定jvmRoute命名要一致)

4、在工程代碼中web.xml添加節點<distributable/> 即可。


測試:

index.jsp:

[html] view plaincopy
  1. <%@ page contentType="text/html; charset=GBK" %>   
  2. <%@ page import="java.util.*" %>    
  3. <html>  
  4.     <head>  
  5.         <title>Cluster App Test</title>  
  6.     </head>   
  7.     <body>  
  8.     Server Info: <%  out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>   
  9.     <%      
  10.     out.println("<br> ID " + session.getId()+"<br>");   // 如果有新的 Session 屬性設置      
  11.     String dataName = request.getParameter("dataName");     
  12.         if (dataName != null && dataName.length() > 0) {   
  13.             String dataValue = request.getParameter("dataValue");  
  14.             session.setAttribute(dataName, dataValue);     
  15.         }      
  16.         out.print("<b>Session 列表</b>");      
  17.         Enumeration e = session.getAttributeNames();     
  18.         while (e.hasMoreElements()) {         
  19.             String name = (String)e.nextElement();         
  20.             String value = session.getAttribute(name).toString();        
  21.             out.println( name + " = " + value+"<br>");             
  22.             System.out.println( name + " = " + value);      
  23.         }   
  24.      %>      
  25.      <form action="index.jsp" method="POST">        
  26.          名稱:<input type=text size=20 name="dataName"> <br>        
  27.          值:<input type=text size=20 name="dataValue"> <br>        
  28.          <input type=submit>      
  29.      </form>   
  30.      </body>   
  31. </html>  


多次訪問,發現Server Info的ip跟端口一直處於ip1,ip2輪詢。但是下面的sessionid 都一樣。即可。

(菜鳥勿噴。)

發佈了97 篇原創文章 · 獲贊 41 · 訪問量 73萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章