纯属转载同事YHM的博客,只为自己好找一点

 

tomcat自带session共享功能 不必非集成第三方软件 只需配置即可 http://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html

为什么选择Nginx?

Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性:
在连接高并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。

配置结构图

1、下载地址

http://nginx.org/en/download.html ,这里我们推荐下载稳定版(stable versions),本文采用nginx/Windows-1.12.0。

2、目录结构

Nginx-
| conf 配置目录
| contrib
| docs 文档目录
| logs 日志目录
| temp 临时文件目录
| html 静态页面目录
|_ nginx.exe 主程序
window下安装Nginx极其简单,解压缩到一个无空格的英文目录即可(个人习惯,担心中文出问题),双击nginx启动,这里我安装到:D:\nginx目录,下面涉及到的tomcat也安装在此目录。

 

Nginx  命令

start nginx
nginx -s stop
nginx -s quit

3、nginx.conf配置

Nginx配置文件默认在conf目录,主要配置文件为nginx.conf

[plain] view plain copy
  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.   
  20.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  21.     #                  '$status $body_bytes_sent "$http_referer" '  
  22.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  23.   
  24.     #access_log  logs/access.log  main;  
  25.   
  26.     sendfile        on;  
  27.     #tcp_nopush     on;  
  28.   
  29.     #keepalive_timeout  0;  
  30.     keepalive_timeout  65;  
  31.   
  32.     #gzip  on;  
  33.   
  34.     upstream suroot{  
  35.         server 127.0.0.1:8090 max_fails=2 fail_timeout=30s;  
  36.         server 127.0.0.1:9091 max_fails=2 fail_timeout=30s;  
  37.     }  
  38.       
  39.     server {  
  40.         listen       80;  
  41.         server_name  localhost;  
  42.   
  43.         #charset koi8-r;  
  44.   
  45.         #access_log  logs/host.access.log  main;  
  46.   
  47.         location / {  
  48.             root   html;  
  49.             index  index.html index.htm index.jsp login.jsp; #定义首页索引文件的名称  
  50.             proxy_pass http://suroot/;#请求转向suroot定义的服务器列表  
  51.             #以下是一些反向代理的配置可删除.  
  52.             proxy_redirect off;  
  53.             proxy_set_header   Host $host;  
  54.             proxy_set_header   X-Real-IP $remote_addr;  
  55.             proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;  
  56.             client_max_body_size   10m;#允许客户端请求的最大单文件字节数  
  57.             client_body_buffer_size   128k;#缓冲区代理缓冲用户端请求的最大字节数,  
  58.             proxy_connect_timeout   600;#nginx跟后端服务器连接超时时间(代理连接超时)  
  59.             proxy_send_timeout   600; #后端服务器数据回传时间(代理发送超时)  
  60.             proxy_read_timeout   600; #连接成功后,后端服务器响应时间(代理接收超时)  
  61.             proxy_buffer_size   8k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小  
  62.             proxy_buffers   4 64k;#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置  
  63.             proxy_busy_buffers_size   128k;#高负荷下缓冲大小(proxy_buffers*2)  
  64.             proxy_temp_file_write_size  128k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传  
  65.         }  
  66.   
  67.         #error_page  404              /404.html;  
  68.   
  69.         # redirect server error pages to the static page /50x.html  
  70.         #  
  71.         error_page   500 502 503 504  /50x.html;  
  72.         location = /50x.html {  
  73.             root   html;  
  74.         }  
  75.   
  76.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  77.         #  
  78.         #location ~ \.php$ {  
  79.         #    proxy_pass   http://127.0.0.1;  
  80.         #}  
  81.   
  82.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  83.         #  
  84.         #location ~ \.php$ {  
  85.         #    root           html;  
  86.         #    fastcgi_pass   127.0.0.1:9000;  
  87.         #    fastcgi_index  index.php;  
  88.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  89.         #    include        fastcgi_params;  
  90.         #}  
  91.   
  92.         # deny access to .htaccess files, if Apache's document root  
  93.         # concurs with nginx's one  
  94.         #  
  95.         #location ~ /\.ht {  
  96.         #    deny  all;  
  97.         #}  
  98.     }  
  99.   
  100.   
  101.     # another virtual host using mix of IP-, name-, and port-based configuration  
  102.     #  
  103.     #server {  
  104.     #    listen       8000;  
  105.     #    listen       somename:8080;  
  106.     #    server_name  somename  alias  another.alias;  
  107.   
  108.     #    location / {  
  109.     #        root   html;  
  110.     #        index  index.html index.htm;  
  111.     #    }  
  112.     #}  
  113.   
  114.   
  115.     # HTTPS server  
  116.     #  
  117.     #server {  
  118.     #    listen       443 ssl;  
  119.     #    server_name  localhost;  
  120.   
  121.     #    ssl_certificate      cert.pem;  
  122.     #    ssl_certificate_key  cert.key;  
  123.   
  124.     #    ssl_session_cache    shared:SSL:1m;  
  125.     #    ssl_session_timeout  5m;  
  126.   
  127.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  128.     #    ssl_prefer_server_ciphers  on;  
  129.   
  130.     #    location / {  
  131.     #        root   html;  
  132.     #        index  index.html index.htm;  
  133.     #    }  
  134.     #}  
  135.   
  136. }  


4、Tomcat配置

对于tomcat大家都很熟悉,只需要修改server.xml配置文件即可;

这里我们以apache-tomcat-8.5.5为例,分别在server目录,解压缩并命名为:apache-tomcat-8.5.5-8090、apache-tomcat-8.5.5-8091

第一处端口修改:
Xml代码 :

[html] view plain copy
  1. <!--  修改port端口:8006 俩个tomcat不能重复,端口随意,别太小-->    
  2. <Server port="8006" shutdown="SHUTDOWN">  

第二处端口修改:
Xml代码 :

[html] view plain copy
  1. <!-- port="8090" tomcat监听端口,随意设置,别太小 -->    
  2. <Connector port="8090" protocol="HTTP/1.1"     
  3.                connectionTimeout="20000"     
  4.                redirectPort="8443" />  


第三处端口修改:

[html] view plain copy
  1. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  

 

第四处session共享配置

[html] view plain copy
  1. <!-- 
  2. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> 
  3. -->  
  4.   <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"  
  5.            channelSendOptions="6">  
  6.   
  7.     <Manager className="org.apache.catalina.ha.session.BackupManager"  
  8.              expireSessionsOnShutdown="false"  
  9.              notifyListenersOnReplication="true"  
  10.              mapSendOptions="6"/>  
  11.     <!--  
  12.     <Manager className="org.apache.catalina.ha.session.DeltaManager"  
  13.              expireSessionsOnShutdown="false"  
  14.              notifyListenersOnReplication="true"/>  
  15.     -->  
  16.     <Channel className="org.apache.catalina.tribes.group.GroupChannel">  
  17.       <Membership className="org.apache.catalina.tribes.membership.McastService"  
  18.                   address="228.0.0.4"  
  19.                   port="45564"  
  20.                   frequency="500"  
  21.                   dropTime="3000"/>  
  22.       <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"  
  23.                 address="auto"  
  24.                 port="5000"  
  25.                 selectorTimeout="100"  
  26.                 maxThreads="6"/>  
  27.   
  28.       <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">  
  29.         <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>  
  30.       </Sender>  
  31.       <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>  
  32.       <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/>  
  33.       <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>  
  34.     </Channel>  
  35.   
  36.     <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"  
  37.            filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>  
  38.   
  39.     <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"  
  40.               tempDir="/tmp/war-temp/"  
  41.               deployDir="/tmp/war-deploy/"  
  42.               watchDir="/tmp/war-listen/"  
  43.               watchEnabled="false"/>  
  44.   
  45.     <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>  
  46.   </Cluster>  


官方文档地址:http://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html

其次验证tomcat,启动两个tomcat,不出现端口冲突即为成功。

 

java项目web.xml需要配置加入

[html] view plain copy
  1. <distributable/>  

验证页面session是否相同

[java] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3.   String path = request.getContextPath();  
  4.   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9. <head>  
  10.   <base href="<%=basePath%>">  
  11.   
  12.   <title>My JSP 'index.jsp' starting page</title>  
  13.   <meta http-equiv="pragma" content="no-cache">  
  14.   <meta http-equiv="cache-control" content="no-cache">  
  15.   <meta http-equiv="expires" content="0">  
  16.   <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.   <meta http-equiv="description" content="This is my page">  
  18.   <!--  
  19.   <link rel="stylesheet" type="text/css" href="styles.css">  
  20.   -->  
  21. </head>  
  22.   
  23. <body>  
  24.   SessionID:<%=session.getId()%>  
  25.   <BR>  
  26.   SessionIP:<%=request.getServerName()%>  
  27.   <BR>  
  28.   SessionPort:<%=request.getServerPort()%>  
  29.   <%  
  30.     out.println("This is Tomcat Server 11111");  
  31.   %>  
  32. </body>  
  33. </html>  



 

参考文章地址:http://www.jianshu.com/p/47a94a3bff34