nginx整合tomcat叢集並做session共用----測試案例,nginxtomcat

來源:互聯網
上載者:User

nginx整合tomcat叢集並做session共用----測試案例,nginxtomcat

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

 

說明:博主採用一個web項目同時部署到兩台tomcat下,(tomcat-A,tomca-B),使用nginx做反向 Proxy,按照設定的權值,將請求分發到背景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 @Controller11 @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共用的測試案例。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.