Tomcat 负载均衡
http://wty.name/tomcat-load-balancing/
Tomcat的负载均衡需要apache服务器的加入来实现。配置使用的是apache-tomcat-6.0.32免安装版本,apache使用的是apache_2.2.19-win32-x86-no_ssl.msi。本文apache的端口为9000,节点的tomcat端口为9001,9002。详细配置如下:
1、在Apache安装目录下找到conf/httpd.conf文件,去掉以下文本前的注释符(#)以便让Apache在启动时自动加载代理(proxy)
模块。
LoadModule proxy_module modules / mod_proxy . so
LoadModule proxy_ajp_module modules / mod_proxy_ajp . so
LoadModule proxy_balancer_module modules / mod_proxy_balancer . so
LoadModule proxy_connect_module modules / mod_proxy_connect . so
LoadModule proxy_ftp_module modules / mod_proxy_ftp . so
LoadModule proxy_http_module modules / mod_proxy_http . so
2、找到DirectoryIndex,在index.html后加上index.jsp。
< IfModule dir_module >
DirectoryIndex index . html index . jsp
< / IfModule >
3、找到Include conf/extra/httpd-vhosts.conf,去掉前面的注释符。
# Virtual hosts
Include conf / extra / httpd - vhosts . conf
4、找到Listen 80,在下面增加一行。
5、在文档最后加上以下内容。
ProxyRequests Off
< proxy balancer : //tc>
BalancerMember ajp : //127.0.0.1:9011 loadfactor=1 route=jvm1
BalancerMember ajp : //127.0.0.1:9012 loadfactor=1 route=jvm2
< / proxy >
ProxyRequests Off 是告诉Apache需要使用反向代理,ip地址和端口唯一确定了tomcat节点和配置的ajp接受端口。loadfactor是负载因子,Apache会按负载因子的比例向后端tomcat节点转发请求,负载因子越大,对应的tomcat服务器就会处理越多的请求,如两个tomcat都是1,Apache就按1:1的比例转发,如果是2和1就按2:1的比例转发。这样就可以使配置更灵活,例如可以给性能好的服务器增加处理工作的比例,如果采取多台服务器,只需要修改ip地址和端口就可以了。route参数对应后续tomcat配置中的引擎路径(jvmRoute)。
6、打开conf/extra/httpd-vhosts.conf,配置虚拟站点,在文档最后加上以下内容。
< VirtualHost * : 9000 >
ServerAdmin 管理员邮箱
ServerName localhost
ServerAlias localhost
ProxyPass /
balancer : //tc/ stickysession=jsessionid nofailover=On
ProxyPassReverse /
balancer : //tc/
ErrorLog "logs/tc-error.log"
CustomLog "logs/tc-access.log"
common
< / VirtualHost >
到此为止,apache的配置工作已完成。
7、配置第一个tomcat节点,打开apache-tomcat-9001\bin\startup.bat,加入以下内容:
set JAVA_HOME = C : \ Program Files \ Java \ jdk1 . 6.0_25
8、打开apache-tomcat-9001\conf\server.xml,修改或加入以下内容:
< Server port = "9015" shutdown = "SHUTDOWN" >
< Connector port = "9001" protocol = "HTTP/1.1"
connectionTimeout = "20000"
redirectPort = "8103" / >
< Connector port = "9011" protocol = "AJP/1.3" redirectPort = "8103" / >
< Engine name = "Catalina" defaultHost = "localhost" jvmRoute = "jvm1" >
< Cluster className = "org.apache.catalina.ha.tcp.SimpleTcpCluster" / >
9、打开apache-tomcat-9001\conf\conf\context.xml。
将以下字段
更改为
< Context distributable = "true" >
这一步是为tomcat中的所有应用都需要Session共享。
10、配置第二个tomcat节点,打开apache-tomcat-9002\bin\startup.bat,加入以下内容:
set JAVA_HOME = C : \ Program Files \ Java \ jdk1 . 6.0_25
11、打开apache-tomcat-9002\conf\server.xml,修改或加入以下内容:
< Server port = "9025" shutdown = "SHUTDOWN" >
< Connector port = "9002" protocol = "HTTP/1.1"
connectionTimeout = "20000"
redirectPort = "8203" / >
< Connector port = "9012" protocol = "AJP/1.3" redirectPort = "8203" / >
< Engine name = "Catalina" defaultHost = "localhost" jvmRoute = "jvm2" >
< Cluster className = "org.apache.catalina.ha.tcp.SimpleTcpCluster" / >
12、打开apache-tomcat-9002\conf\conf\context.xml。
将以下字段
更改为
< Context distributable = "true" >
这一步是为tomcat中的所有应用都需要Session共享。
13、测试一下是否配置成功。在webapps目录下新建test目录,在test目录下新建test.jsp文件,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<% @ page
contentType = "text/html; charset=GBK" %>
<% @ page
import = "java.util.*" %>
< html > < head > < title > shiyang < / title > < / head >
< body >
服务信息:
<%
out . println ( request . getLocalAddr ( ) +
" : " +
request . getLocalPort ( ) + "<br/>" ) ; %>
<%
out . println ( "<br> ID " +
session . getId ( ) + "<br/>" ) ;
String dataName
= request . getParameter ( "dataName" ) ;
if ( dataName !=
null &&
dataName . length ( ) >
0 ) {
String dataValue
= request . getParameter ( "dataValue" ) ;
session . setAttribute ( dataName , dataValue ) ;
}
out . print ( "<b>Session 列表</b><br/>" ) ;
Enumeration e
= session . getAttributeNames ( ) ;
while ( e . hasMoreElements ( ) ) {
String name
= ( String ) e . nextElement ( ) ;
String value
= session . getAttribute ( name ) . toString ( ) ;
out . println ( name
+ " = "
+ value + "<br/>" ) ;
System . out . println ( name
+ " = "
+ value ) ;
}
%>
< form action = "test.jsp" method = "POST" >
名称: < input type = text size = 20 name = "dataName" >
< br / >
值: < input type = text size = 20 name = "dataValue" >
< br / >
< input type = submit value = "提交" >
< / form >
< / body >
< / html >
先启动Apache服务,再启动两台tomcat,分别点startup.bat批处理。访问http://localhost/test/test.jsp。可以看到包括服务器地址,端口,session等信息在内的页面。