zoukankan      html  css  js  c++  java
  • nginx实现高性能负载均衡的Tomcat集群

    1. 安装软件:

    nginx

    两个apache-tomcat

    安装过程省略。

    2.配置两个tomcat的http端口,第一个为18080,第二个为28080

    注意:需要把server.xml文件中所有的端口都改为1xxxx或者2xxxx,不然就会有端口冲突,导致有一个tomcat启动不了。

    如:

    <?xml version='1.0' encoding='utf-8'?>
    <!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements. See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License. You may obtain a copy of the License at
    
    http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    -->
    <!-- Note: A "Server" is not itself a "Container", so you may not
    define subcomponents such as "Valves" at this level.
    Documentation at /docs/config/server.html
    -->
    <Server port="28005" shutdown="SHUTDOWN">
    <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
    <!-- Security listener. Documentation at /docs/config/listeners.html
    <Listener className="org.apache.catalina.security.SecurityListener" />
    -->
    <!--APR library loader. Documentation at /docs/apr.html -->
    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
    <!-- Prevent memory leaks due to use of particular java/javax APIs-->
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
    
    <!-- Global JNDI resources
    Documentation at /docs/jndi-resources-howto.html
    -->
    <GlobalNamingResources>
    <!-- Editable user database that can also be used by
    UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
    type="org.apache.catalina.UserDatabase"
    description="User database that can be updated and saved"
    factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
    pathname="conf/tomcat-users.xml" />
    </GlobalNamingResources>
    
    <!-- A "Service" is a collection of one or more "Connectors" that share
    a single "Container" Note: A "Service" is not itself a "Container",
    so you may not define subcomponents such as "Valves" at this level.
    Documentation at /docs/config/service.html
    -->
    <Service name="Catalina">
    
    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
    maxThreads="150" minSpareThreads="4"/>
    -->
    
    
    <!-- A "Connector" represents an endpoint by which requests are received
    and responses are returned. Documentation at :
    Java HTTP Connector: /docs/config/http.html
    Java AJP Connector: /docs/config/ajp.html
    APR (HTTP/AJP) Connector: /docs/apr.html
    Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="28888" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="28443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
    port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" />
    -->
    <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
    This connector uses the NIO implementation with the JSSE engine. When
    using the JSSE engine, the JSSE configuration attributes must be used.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
    maxThreads="150" SSLEnabled="true">
    <SSLHostConfig>
    <Certificate certificateKeystoreFile="conf/keystore-rsa.pem"
    type="RSA" />
    </SSLHostConfig>
    </Connector>
    -->
    <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
    This connector uses the APR/native implementation. When using the
    APR/native implementation or the OpenSSL engine with NIO or NIO2 then
    the OpenSSL configuration attributes must be used.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
    maxThreads="150" SSLEnabled="true" >
    <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
    <SSLHostConfig honorCipherOrder="false" >
    <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
    certificateFile="conf/localhost-rsa-chain.pem"
    type="RSA" />
    </SSLHostConfig>
    </Connector>
    -->
    
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="28009" protocol="AJP/1.3" redirectPort="28443" />
    
    
    <!-- An Engine represents the entry point (within Catalina) that processes
    every request. The Engine implementation for Tomcat stand alone
    analyzes the HTTP headers included with the request, and passes them
    on to the appropriate Host (virtual host).
    Documentation at /docs/config/engine.html -->
    
    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">
    
    <!--For clustering, please take a look at documentation at:
    /docs/cluster-howto.html (simple how to)
    /docs/config/cluster.html (reference documentation) -->
    <!--
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    -->
    
    <!-- Use the LockOutRealm to prevent attempts to guess user passwords
    via a brute-force attack -->
    <Realm className="org.apache.catalina.realm.LockOutRealm">
    <!-- This Realm uses the UserDatabase configured in the global JNDI
    resources under the key "UserDatabase". Any edits
    that are performed against this UserDatabase are immediately
    available for use by the Realm. -->
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
    resourceName="UserDatabase"/>
    </Realm>
    
    <Host name="localhost" appBase="webapps"
    unpackWARs="true" autoDeploy="true">
    
    <!-- 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
    Note: The pattern used is equivalent to using pattern="common" -->
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
    prefix="localhost_access_log" suffix=".txt"
    pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    
    </Host>
    </Engine>
    </Service>
    
    <Service name="Catalina">
    
    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
    maxThreads="150" minSpareThreads="4"/>
    -->
    
    
    <!-- A "Connector" represents an endpoint by which requests are received
    and responses are returned. Documentation at :
    Java HTTP Connector: /docs/config/http.html
    Java AJP Connector: /docs/config/ajp.html
    APR (HTTP/AJP) Connector: /docs/apr.html
    Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="28080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="28443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
    port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" />
    -->
    <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
    This connector uses the NIO implementation with the JSSE engine. When
    using the JSSE engine, the JSSE configuration attributes must be used.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
    maxThreads="150" SSLEnabled="true">
    <SSLHostConfig>
    <Certificate certificateKeystoreFile="conf/keystore-rsa.pem"
    type="RSA" />
    </SSLHostConfig>
    </Connector>
    -->
    <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
    This connector uses the APR/native implementation. When using the
    APR/native implementation or the OpenSSL engine with NIO or NIO2 then
    the OpenSSL configuration attributes must be used.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
    maxThreads="150" SSLEnabled="true" >
    <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
    <SSLHostConfig honorCipherOrder="false" >
    <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
    certificateFile="conf/localhost-rsa-chain.pem"
    type="RSA" />
    </SSLHostConfig>
    </Connector>
    -->
    
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="28009" protocol="AJP/1.3" redirectPort="28443" />
    
    
    <!-- An Engine represents the entry point (within Catalina) that processes
    every request. The Engine implementation for Tomcat stand alone
    analyzes the HTTP headers included with the request, and passes them
    on to the appropriate Host (virtual host).
    Documentation at /docs/config/engine.html -->
    
    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">
    
    <!--For clustering, please take a look at documentation at:
    /docs/cluster-howto.html (simple how to)
    /docs/config/cluster.html (reference documentation) -->
    <!--
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    -->
    
    <!-- Use the LockOutRealm to prevent attempts to guess user passwords
    via a brute-force attack -->
    <Realm className="org.apache.catalina.realm.LockOutRealm">
    <!-- This Realm uses the UserDatabase configured in the global JNDI
    resources under the key "UserDatabase". Any edits
    that are performed against this UserDatabase are immediately
    available for use by the Realm. -->
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
    resourceName="UserDatabase"/>
    </Realm>
    
    <Host name="localhost" appBase="webapps"
    unpackWARs="true" autoDeploy="true">
    
    <!-- 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
    Note: The pattern used is equivalent to using pattern="common" -->
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
    prefix="localhost_access_log" suffix=".txt"
    pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    
    </Host>
    </Engine>
    </Service>
    
    </Server>
    View Code

    3..写个简单的页面分别放到两个tomcat下,如图

    18080下的test.html内容如下:

    <html>
    <head>
    <meto http-equiv="Content-Type" content="text/html:charSet=GBK">
    <title>一个简单的页面</title>
    </head>
    <body>
    the port is 18080
    </body>
    </html>
    View Code

    28080下的test.html内容如下:

    <html>
    <head>
    <meto http-equiv="Content-Type" content="text/html:charSet=GBK">
    <title>一个简单的页面</title>
    </head>
    <body>
    hello this is 28080 ha ha ha ....
    </body>
    </html>
    View Code

    4.正常启动两个tomcat,测试是否正常启动:

    访问http://localhost:18080/myhtml/test.html和http://localhost:28080/myhtml/test.html

    如果能访问到对应的测试页面,测启动正常。

    5.配置nginx,如下:

    #user  nobody;
    
    worker_processes  1; #工作进程的个数,一般与计算机的cpu核数一致
    
     
    
    #error_log  logs/error.log;
    
    #error_log  logs/error.log  notice;
    
    #error_log  logs/error.log  info;
    
     
    
    #pid        logs/nginx.pid;
    
     
    
     
    
    events {
    
        worker_connections  1024; #单个进程最大连接数(最大连接数=连接数*进程数)
    
    }
    
     
    
     
    
    http {
    
        include       mime.types; #文件扩展名与文件类型映射表
    
        default_type  application/octet-stream; #默认文件类型
    
     
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    
        #                  '$status $body_bytes_sent "$http_referer" '
    
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
     
    
        #access_log  logs/access.log  main;
    
     
    
        sendfile        on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    
        #tcp_nopush     on;
    
     
    
        #keepalive_timeout  0;
    
        keepalive_timeout  65; #长连接超时时间,单位是秒
    
     
    
        gzip  on; #启用Gizp压缩
    
     
    
        #服务器的集群
    
        upstream  netitcast.com { #服务器集群名字
    
            #服务器配置列表,weight是权重的意思,权重越大,分配的概率越大
    
            server  127.0.0.1:18080  weight=1;
    
            server  127.0.0.1:28080  weight=2;
    
        }
    
    
    #当前的Nginx的配置
        server {
    
            listen       8080; #监听80端口,可以改成其他端口 
    
            server_name  localhost; #当前服务的域名,如果请求为localhost:8080,则交给http://netitcast.com的nginx集群来处理
    
     
    
            #charset koi8-r;
    
     
    
            #access_log  logs/host.access.log  main;
    
     
    
            location / {
    
                proxy_pass  http://netitcast.com;  #与 服务器集群名字 一致
    
                proxy_redirect  default;
    
            }
    
     
    
            #error_page  404              /404.html;
    
     
    
            # redirect server error pages to the static page /50x.html
    
            #
    
            error_page   500 502 503 504  /50x.html;
    
            location = /50x.html {
    
                root   html;
    
            }
    
     
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    
            #
    
            #location ~ .php$ {
    
            #    proxy_pass   http://127.0.0.1;
    
            #}
    
     
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    
            #
    
            #location ~ .php$ {
    
            #    root           html;
    
            #    fastcgi_pass   127.0.0.1:9000;
    
            #    fastcgi_index  index.php;
    
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    
            #    include        fastcgi_params;
    
            #}
    
     
    
            # deny access to .htaccess files, if Apache's document root
    
            # concurs with nginx's one
    
            #
    
            #location ~ /.ht {
    
            #    deny  all;
    
            #}
    
        }
    
     
    
     
    
        # another virtual host using mix of IP-, name-, and port-based configuration
    
        #
    
        #server {
    
        #    listen       8000;
    
        #    listen       somename:8080;
    
        #    server_name  somename  alias  another.alias;
    
     
    
        #    location / {
    
        #        root   html;
    
        #        index  index.html index.htm;
    
        #    }
    
        #}
    
     
    
     
    
        # HTTPS server
    
        #
    
        #server {
    
        #    listen       443 ssl;
    
        #    server_name  localhost;
    
     
    
        #    ssl_certificate      cert.pem;
    
        #    ssl_certificate_key  cert.key;
    
     
    
        #    ssl_session_cache    shared:SSL:1m;
    
        #    ssl_session_timeout  5m;
    
     
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
    
        #    ssl_prefer_server_ciphers  on;
    
     
    
        #    location / {
    
        #        root   html;
    
        #        index  index.html index.htm;
    
        #    }
    
        #}
    
        include servers/*;
    
    }
    View Code

    6.到此配置完成,下面开始演示负载均衡,

     首先启动nginx,

    localhost:nginx mhx$ sudo nginx

    在浏览器中访问 http://localhost:8080,如果出现Nginx界面,则说明启动成功.

    如:

    7.测试负载均衡:

    访问:http://localhost:8080/myhtml/test.html多次

    页面出现内容:

    the port is 18080       (出现频率占比1/3)

    或者

    hello this is 28080 ha ha ha ....         (出现频率占比2/3)

  • 相关阅读:
    php 人员权限管理(RBAC)
    CSS3的@keyframes用法详解
    phpcms安装与使用
    PDO 数据访问抽象层
    ajax的分页查询
    php 增删改查范例(3)
    php 增删改查范例(2)
    maven部署项目流程(区分环境)
    springboot分环境打包(maven动态选择环境)
    Guava Cache -- MapMaker.makeComputingMap测试
  • 原文地址:https://www.cnblogs.com/code4app/p/11314304.html
Copyright © 2011-2022 走看看