nginx整合tomcat集群並做session共享----測試案例


最近出於好奇心,研究了一下tomcat集群配置,並整合nginx,實現負載均衡,session共享,寫篇記錄,防止遺忘。---------菜雞的自我修煉。

 

說明:博主采用一個web項目同時部署到兩台tomcat下,(tomcat-A,tomca-B),使用nginx做反向代理,按照設置的權值,將請求分發到后台的tomcatA/tomcatB,並且實現session共享。

    

配置好本地域名指向:修改host文件:添加 127.0.0.1  www.domain.com.cn

新建項目:tiny-demo-operation 采用springmvc 配置好springmvc.xml配置文件

建立SessionShareSetController和SessionShareGetController

目錄如下:

SessionShareSetController代碼:

 1 package com.tiny.session.share.controller;  2 
 3 import javax.servlet.http.HttpServletRequest;  4 import javax.servlet.http.HttpServletResponse;  5 import javax.servlet.http.HttpSession;  6 
 7 import org.springframework.stereotype.Controller;  8 import org.springframework.web.bind.annotation.RequestMapping;  9 
10 @Controller 11 @RequestMapping("/sessionShare") 12 public class SessionShareSetController { 13 
14     @RequestMapping("/sessionSet") 15     public String sessionSet(HttpServletRequest request, 16             HttpServletResponse response) throws Exception { 17 
18         HttpSession session = request.getSession(); 19         String name = "tinyseven-demo-operation"+"---"; 20         String remote = request.getRemoteHost() + "---"
21                 + request.getRemoteAddr() + "---" + request.getRemotePort() 22                 + "---"; 23         String local = request.getLocalName() + "---" + request.getLocalAddr() 24                 + "---" + request.getLocalPort() + "---"; 25         String server = request.getServerName() + "---"
26                 + request.getServerPort() + "---"; 27         request.setAttribute("name", name); 28         request.setAttribute("remote", remote); 29         request.setAttribute("local", local); 30         request.setAttribute("server", server); 31 
32         session.setAttribute("name", name); 33         return "sessionshare/sessionSet"; 34  } 35 
36 }
View Code

SessionShareGetController代碼:

 1 package com.tiny.session.share.controller;  2 
 3 import javax.servlet.http.HttpServletRequest;  4 
 5 import org.springframework.stereotype.Controller;  6 import org.springframework.web.bind.annotation.RequestMapping;  7 
 8 @Controller  9 @RequestMapping("/sessionShare") 10 public class SessionShareGetController { 11 
12     @RequestMapping("/sessionGet") 13     public String sessionGet(HttpServletRequest request) throws Exception { 14 
15         String name = (String) request.getSession().getAttribute("name") 16                 + "---"; 17         String remote = request.getRemoteHost() + "---"
18                 + request.getRemoteAddr() + "---" + request.getRemotePort() 19                 + "---"; 20         String local = request.getLocalName() + "---" + request.getLocalAddr() 21                 + "---" + request.getLocalPort() + "---"; 22         String server = request.getServerName() + "---"
23                 + request.getServerPort() + "---"; 24         request.setAttribute("name", name); 25         request.setAttribute("remote", remote); 26         request.setAttribute("local", local); 27         request.setAttribute("server", server); 28         return "sessionshare/sessionGet"; 29 
30  } 31 }
View Code

新建jsp頁面:

目錄如下:

sessionSet.jsp部分代碼:

<body> 當前用戶設置session--server-->>${server}</br> 當前用戶設置session--remote-->>${remote}</br> 當前用戶設置session--local-->>${local}</br> 當前用戶設置session--name-->>${name}</br>
</body>
View Code

sessionGet.jsp部分代碼:

1 <body>
2     當前用戶請求的server-->>${server}</br>
3     當前用戶請求的remote-->>${remote}</br>
4     當前用戶請求的local-->>${local}</br>
5     當前用戶請求的name-->>${name}</br>
6 </body>
View Code

 

 

一、准備兩台tomcat,建立起tomcat集群。

博主使用 apache-tomcat-6.0.37

路徑分別為:E:\Server\apache-tomcat-6.0.37-node-A

                 E:\Server\apache-tomcat-6.0.37-node-B

分別修改A/B的server.xml保證兩台tomcat可以正常啟動,避免端口沖突,並且建立起兩台tomcat的集群。

1.修改A的server.xml,在所有port前面加1,例如(<Server port="8005" shutdown="SHUTDOWN">修改成<Server port="18005" shutdown="SHUTDOWN">其他類似

修改<Engine name="Catalina" defaultHost="localhost" >為<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

 

2.修改B的server.xml,在所有port前面加2,例如(<Server port="8005" shutdown="SHUTDOWN">修改成<Server port="28005" shutdown="SHUTDOWN">其他類似

修改<Engine name="Catalina" defaultHost="localhost" >為<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

此時兩台tomcat應該都可以成功啟動了。

 

3.建立起兩台tomcat的集群服務。

分別取消掉A/B server.xml文件中

<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->

 的注釋。

重新啟動A、B tomcat(運行bin中的startup.bat)發現這次啟動比上次多了以下信息,表示兩個tomcat節點已經建立起了關聯。

二、發布項目到tomcatA、tomcatB下,並且配置session共享。

1.修改tomcaA、tomcatB的server.xml文件。

<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">

<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->

<!-- Access log processes all example.
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
-->
<Context docBase="E:\javaWorkspace\tiny-demo-operation\WebRoot" reloadable="true" path="/tiny-demo-operation" crossContext="true" source="org.eclipse.jst.jee.server:tiny-demo-operation"/> 此處為添加內容

</Host>

2、修改所發布項目的web.xml文件

在web.xml文件的</web-app>之前添加

<!-- session共享配置 -->
<distributable />

 

此時當前項目在兩台tomcat可以做到session共享了,效果如下:

訪問tomcatA下的項目,設置session:

訪問tomcatB下的項目,獲取session:

三、整合nginx,實現請求分發。此處使用的是nginx1.5.0版本

1.修改nginx.conf  修改后

 1 #Nginx所用用戶和組,window下不指定  2 #user niumd niumd;  3 #工作的子進程數量(通常等於CPU數量或者2倍於CPU)  4 
 5 worker_processes 2;  6 
 7 #錯誤日志存放路徑  8 #error_log logs/error.log;  9 #error_log logs/error.log notice; 10 
11 error_log logs/error.log info; 12 
13 #指定pid存放文件 14 
15 pid logs/nginx.pid; 16 
17 events { 18  #使用網絡IO模型linux建議epoll,FreeBSD建議采用kqueue,window下不指定。 19  #use epoll; 20  #允許最大連接數 21  worker_connections 2048; 22 } 23 
24 http { 25 
26  include mime.types; 27  default_type application/octet-stream; 28  #定義日志格式 29  #log_format main '$remote_addr - $remote_user [$time_local] $request ' 30  # '"$status" $body_bytes_sent "$http_referer" ' 31  # '"$http_user_agent" "$http_x_forwarded_for"'; 32  #access_log off; 33  access_log logs/access.log; 34  client_header_timeout 3m; 35  client_body_timeout 3m; 36  send_timeout 3m; 37  client_header_buffer_size 1k; 38  large_client_header_buffers 4 4k; 39  sendfile on; 40  tcp_nopush on; 41  tcp_nodelay on; 42  #keepalive_timeout 75 20; 43  include gzip.conf; 44  include proxy.conf; 45 
46  upstream localhost { 47  #根據ip計算將請求分配各那個后端tomcat,許多人誤認為可以解決session問題,其實並不能。 48  #同一機器在多網情況下,路由切換,ip可能不同 49  #ip_hash; 50  #weigth參數表示權值,權值越高被分配到的幾率越大 51  server localhost:18080 weight=5; 52  server localhost:28080 weight=5; 53 
54  } 55     
56  server { 57     
58  listen 80; 59  server_name localhost; 60 
61  location / { 62  root E:/javaWorkspace/tiny-demo-operation/WebRoot; 63  index index.html index.htm; 64             
65  } 66         
67  location ~ \.(html|js|css|png|gif)$ { 68  root E:/javaWorkspace/tiny-demo-operation/WebRoot; 69  } 70 
71  location ~ \.(jsp|action)$ { 72  proxy_connect_timeout 3; 73  proxy_send_timeout 30; 74  proxy_read_timeout 30; 75  proxy_pass http://localhost; 76  } 77  } 78 }
View Code

2.重新啟動nginx

訪問虛擬主機下的當前項目,nginx自動實現請求分發,效果如下:

分發到tomcatB下的tiny-demo-operation,設置session中的屬性值name。

 

分發tomcatA下的tiny-demo-operation,獲取session中的屬性值name。

 

 

至此,簡單實現了nginx整合tomcat集群,實現負載均衡,session共享的測試案例。

  


  -END- 

 

 


免責聲明!

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



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