日期:2014-05-17  浏览次数:20778 次

Nginx+Tomcat在Windows下做负载均衡

一、 为什么需要对 Tomcat 服务器做负载均衡:

Tomcat 服务器作为一个 Web 服务器,其并发数在 300-500 之间,如果有超过 500 的并发数便会出现 Tomcat 不能响应新的请求的情况,严重影响网站的运行。另外,在访问量大的情况下, Tomcat 的线程数会不断增加。由于 Tomcat 自身对内存的占用有控制,当对内存的占用达到最大值时便会出现内存溢出,对网站的访问严重超时等现象,这时便需要重新启动 Tomcat 以释放占用的内存,这样做便会阻断网站运行。

所以对 Tomcat 做负载均衡便很有必要。目前可以和 Tomcat 做负载均衡的主流服务器是 Apache ,但是 Nginx 由于功能多、配置简单等优点逐渐成为很多负载均衡服务器的首选。 Nginx 的并发数可达到 50000 ,所以理论上可以和 Tomcat 以 1:100 的比例来配置,这样便可以很好的解决网站并发瓶颈问题。

二、 Nginx+Tomcat 在 Windows 下负载均衡配置方法:

Nginx+Tomcat 在 Windows 下做负载均衡相对在 Linux 下简单不少,因为不论是 Nginx 还是 Tomcat 只需要下载 Windows 下的安装包解压到某个目录下,然后就可以做配置了。

?????????????? 我选择的 Nginx 是 nginx-0.8.49 这个版本, Tomcat 选择的是 apache-tomcat-6.0.26 。

???????? 下面就是配置过程 :

???????? 1. 将 nginx-0.8.49.rar 压缩包直接解压到某个目录下 ( 如 D:/ 负载均衡 /nginx) 。

?????? 2. 将 apache-tomcat-6.0.26.rar 压缩包解压,因为是做负载均衡,所以至少要解压两个。 ( 如分别解压到 D:/ 负载均衡 /tomcat_1 和 D:/ 负载均衡 /tomcat_2 下面 ) 。

???????? 3. 将要发布的项目发布到两个 Tomcat 根目录下的 webapps 下,保证两个 Tomcat 下面的项目名称相同。

???????? 4. 修改其中一个 Tomcat 的配置文件,该配置文件位于 Tomcat 目录下的 /conf/ 下,文件名为 server.xml ,修改其中的 <Server port="8005" shutdown="SHUTDOWN"> 为 <Server port="8006" shutdown="SHUTDOWN"> ,修改其中的

<Connector port="8080" protocol="HTTP/1.1"

?????????????? connectionTimeout="20000"

?????????????? redirectPort="8443" />

<Connector port="8088" protocol="HTTP/1.1"

?????????????? connectionTimeout="20000"

?????????????? redirectPort="8443" />

修改后的文件如下:

<?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="8006" shutdown="SHUTDOWN">

?

? <!--APR library loader. Documentation at /docs/apr.html -->

? <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

? <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->

? <Listener className="org.apache.catalina.core.JasperListener" />

? <!-- Prevent memory leaks due to use of particular java/javax APIs-->

? <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />

? <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->

? <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />

? <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

?

? <!-- 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 na