• Nginx+Tomcat 集群部署


    1.Nginx + Tomcat 集群部署 简单配置

      1 #user  nobody;
      2 worker_processes  4;#工作进程的个数
      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     gzip on;
     35     upstream netitcast.com{
     36         # 定下请求ip_hash;
     37         ip_hash;
     38         server 127.0.0.1:8181 weight=1;#服务器配置 代理分配  weight:分配权重 down:不参与负载均衡
     39         server 192.168.0.105:8181 weight=1;#
     40     }
     41 
     42     server {
     43         listen       8888;
     44         server_name  localhost;
     45 
     46         #charset koi8-r;
     47 
     48         #access_log  logs/host.access.log  main;
     49 
     50         location / {
     51             root   html;
     52             index  index.html index.htm;
     53             # 配置
     54             proxy_pass http://netitcast.com;
     55         }
     56 
     57         #error_page  404              /404.html;
     58 
     59         # redirect server error pages to the static page /50x.html
     60         #
     61         error_page   500 502 503 504  /50x.html;
     62         location = /50x.html {
     63             root   html;
     64         }
     65 
     66         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
     67         #
     68         #location ~ .php$ {
     69         #    proxy_pass   http://127.0.0.1;
     70         #}
     71 
     72         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
     73         #
     74         #location ~ .php$ {
     75         #    root           html;
     76         #    fastcgi_pass   127.0.0.1:9000;
     77         #    fastcgi_index  index.php;
     78         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
     79         #    include        fastcgi_params;
     80         #}
     81 
     82         # deny access to .htaccess files, if Apache's document root
     83         # concurs with nginx's one
     84         #
     85         #location ~ /.ht {
     86         #    deny  all;
     87         #}
     88     }
     89 
     90 
     91     # another virtual host using mix of IP-, name-, and port-based configuration
     92     #
     93     #server {
     94     #    listen       8000;
     95     #    listen       somename:8080;
     96     #    server_name  somename  alias  another.alias;
     97 
     98     #    location / {
     99     #        root   html;
    100     #        index  index.html index.htm;
    101     #    }
    102     #}
    103 
    104 
    105     # HTTPS server
    106     #
    107     #server {
    108     #    listen       443 ssl;
    109     #    server_name  localhost;
    110 
    111     #    ssl_certificate      cert.pem;
    112     #    ssl_certificate_key  cert.key;
    113 
    114     #    ssl_session_cache    shared:SSL:1m;
    115     #    ssl_session_timeout  5m;
    116 
    117     #    ssl_ciphers  HIGH:!aNULL:!MD5;
    118     #    ssl_prefer_server_ciphers  on;
    119 
    120     #    location / {
    121     #        root   html;
    122     #        index  index.html index.htm;
    123     #    }
    124     #}
    125 
    126 }

    2.关于Session 的问题

      2.1Tomcat 提供了Cluster 的组件实现session 复制 ,Clurster 的配置

        <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="8">
          <!--
           复制方式:
            org.apache.catalina.ha.session.DeltaManager:复制所有的session 到所有的节点(节点太多,耗费资源)
            org.apache.catalina.ha.session.BackupManager:    
    
            
            expireSessionsOnShutdown:一个程序被关闭时候,是否要销毁所有session
            notifyListenersOnReplication:session复制和移动的时候通知
          -->
          <Manager className="org.apache.catalina.ha.session.DeltaManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"/>
    
          <Channel className="org.apache.catalina.tribes.group.GroupChannel">
            <Membership className="org.apache.catalina.tribes.membership.McastService"
                        address="228.0.0.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="auto"
                      port="4002"
                      autoBind="100"
                      selectorTimeout="5000"
                      maxThreads="6"/>
            
            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            </Sender>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
          </Channel>
    
          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=""/>
          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
    
          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                    tempDir="/tmp/war-temp/"
                    deployDir="/tmp/war-deploy/"
                    watchDir="/tmp/war-listen/"
                    watchEnabled="false"/>
    
          <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>

               注意:使用Cluster的组件实现Session复制的时候,一定要在程序中web.xml加上这样一句告诉tomcat这个程序是分布式        

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
        id="WebApp_ID" version="2.5">
      <display-name>Test</display-name>
       <!--
            分布式
      -->
      <distributable/>
    </web-app>

    测试Session复制和Nginx集群是否成功页面

     1 <%@ page contentType="text/html; charset=GBK" %>
     2 <%@ page import="java.util.*" %>
     3 <html><head><title>Cluster App Test</title></head>
     4 <body>
     5 Server Info:
     6 <%
     7 out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
     8 <%
     9   out.println("<br> ID " + session.getId()+"<br>");
    10   // 如果有新的 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.println("<b>Session 列表</b><br>");
    17   System.out.println("============================");
    18   Enumeration e = session.getAttributeNames();
    19   while (e.hasMoreElements()) {
    20      String name = (String)e.nextElement();
    21      String value = session.getAttribute(name).toString();
    22      out.println( name + " = " + value+"<br>");
    23          System.out.println( name + " = " + value);
    24    }
    25 %>
    26   <form action="index.jsp" method="POST">
    27     名称:<input type=text size=20 name="dataName">
    28      <br>
    29     值:<input type=text size=20 name="dataValue">
    30      <br>
    31     <input type=submit>
    32    </form>
    33 </body>
    34 </html>

    到了这里我有两个疑问没有解决,

    1.使用Cluster虽然Session复制成功了,但是在网页上使用iframe 框架时候会出现失效

    2.文件上传的问题

           

  • 相关阅读:
    java反编译工具
    javascript
    微信小程序
    微信小程序
    微信小程序
    微信小程序
    微信小程序
    微信小程序
    微信小程序
    微信小程序
  • 原文地址:https://www.cnblogs.com/ChangFen/p/7492051.html
Copyright © 2020-2023  润新知