博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
windows下nginx+tomcat集群,实现session复制共享
阅读量:7224 次
发布时间:2019-06-29

本文共 2480 字,大约阅读时间需要 8 分钟。

  hot3.png

两台不同服务器  ip1、ip2。ip1安装tomcat1,端口8005,8081,8009、nginx,提供nginx外网端口80,ip2安装tomcat2,端口8006,8082,8010。

1、安装apach tomcat。

2、安装ng,下载解压安装到ip1,根据需要修改配置文件nginx.conf:

[plain] 

  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] 

  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 都一样。即可。

(菜鸟勿喷。)

转载于:https://my.oschina.net/glenxu/blog/529320

你可能感兴趣的文章
django 坑~~
查看>>
Python 函数
查看>>
Linux64位程序中的漏洞利用
查看>>
Shell 编程(实例二)
查看>>
dp --- 二维dp + 最大上升子序列
查看>>
Android - TabHost 选项卡功能用法详解
查看>>
数据结构03-栈和队列
查看>>
【代码随笔集】点击对象的获取
查看>>
Cmder + Babun 打造 Windows 好用的终端工具
查看>>
Source code for Bayesian based CS and blind debluring
查看>>
dubbo源码—service export
查看>>
八大排序算法
查看>>
高德地图-控件
查看>>
梦断代码--阅读笔记2
查看>>
css3新增的属性 - 分享
查看>>
python之路---07 join() fromkeys() 深浅拷贝
查看>>
web2py官方文档翻译02
查看>>
vector容器的简单用法
查看>>
Python3-join()和split()
查看>>
大型网站架构演变和知识体系
查看>>