APACHE+TOMCAT的简单HA配置
APACHE+TOMCAT的简单HA配置,实现负载均衡和故障转移,不考虑后端TOMCAT的SESSION。一般用于基于HTTP协议的接口服务。
环境
节点1:RHL6.4 192.168.9.237
节点2:RHL6.4 192.168.9.238
2.在两个节点分别安装TOMCAT,作为后端服务。
3.第1台机器上安装apache作为集群WEB服务器。
环境参数:JDK1.6以上,httpd-2.4.16 Tomcat-6.0.44
下载tomcat war包
http://pan.baidu.com/s/1eQqDxho
1.Tomcat安装和配置
登录节点1,安装tomcat
[root@tomcatha1 ~]# tar -xvf apache-tomcat-6.0.44.tar.gz
部署测试工程cluster到TOMCAT
下载测试工程WAR包:tomcatha.war
[root@tomcatha1 ~]# cd apache-tomcat-6.0.44
[root@tomcatha1 apache-tomcat-6.0.44]# ll
total 116
drwxr-xr-x 2 root root 4096 Sep 30 10:35 bin
drwxr-xr-x 2 root root 4096 May 8 20:21 conf
drwxr-xr-x 2 root root 4096 Sep 30 10:35 lib
-rw-r--r-- 1 root root 56653 May 8 20:21 LICENSE
drwxr-xr-x 2 root root 4096 May 8 20:21 logs
-rw-r--r-- 1 root root 761 May 8 20:21 NOTICE
-rw-r--r-- 1 root root 9124 May 8 20:21 RELEASE-NOTES
-rw-r--r-- 1 root root 16229 May 8 20:21 RUNNING.txt
drwxr-xr-x 2 root root 4096 Sep 30 10:35 temp
drwxr-xr-x 7 root root 4096 May 8 20:21 webapps
drwxr-xr-x 2 root root 4096 May 8 20:21 work
[root@tomcatha1 apache-tomcat-6.0.44]# cd webapps/
[root@tomcatha1 webapps]# ll
total 20
drwxr-xr-x 10 root root 4096 Sep 30 10:35 docs
drwxr-xr-x 5 root root 4096 Sep 30 10:35 examples
drwxr-xr-x 5 root root 4096 Sep 30 10:35 host-manager
drwxr-xr-x 5 root root 4096 Sep 30 10:35 manager
drwxr-xr-x 3 root root 4096 Sep 30 10:35 ROOT
[root@tomcatha1 webapps]# mkdir cluster
[root@tomcatha1 webapps]# cd cluster/
[root@tomcatha1 cluster]# rz
rz waiting to receive.
zmodem trl+C ?
100% 4 KB 4 KB/s 00:00:01 0 Errors
[root@tomcatha1 cluster]# ll
total 8
-rw-r--r-- 1 root root 4733 Sep 29 15:55 tomcatha.war
[root@tomcatha1 cluster]#
修改端口及war包方式
端口:
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
改为:
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
war包:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
改为:
<Host name="localhost" appBase="webapps"
unpackWARs="false" autoDeploy="false"
xmlValidation="false" xmlNamespaceAware="false">
路径:
在Host上加入一行:
<Context path="/tomcatha" docBase="/root/apache-tomcat-6.0.44/webapps/cluster/tomcatha.war" />
</Host>
</Engine>
启动tomcat:
[root@tomcatha1 conf]# cd /root/apache-tomcat-6.0.44/bin/
[root@tomcatha1 bin]# sh startup.sh
Using CATALINA_BASE: /root/apache-tomcat-6.0.44
Using CATALINA_HOME: /root/apache-tomcat-6.0.44
Using CATALINA_TMPDIR: /root/apache-tomcat-6.0.44/temp
Using JRE_HOME: /usr
Using CLASSPATH: /root/apache-tomcat-6.0.44/bin/bootstrap.jar
[root@tomcatha1 bin]# ps -ef | grep java
root 10245 1 63 10:46 pts/1 00:00:03 /usr/bin/java -Djava.util.logging.config.file=/root/apache-tomcat-6.0.44/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/root/apache-tomcat-6.0.44/endorsed -classpath /root/apache-tomcat-6.0.44/bin/bootstrap.jar -Dcatalina.base=/root/apache-tomcat-6.0.44 -Dcatalina.home=/root/apache-tomcat-6.0.44 -Djava.io.tmpdir=/root/apache-tomcat-6.0.44/temp org.apache.catalina.startup.Bootstrap start
root 10264 1959 0 10:46 pts/1 00:00:00 grep java
测试节点TOMCAT部署
浏览器中访问 http://192.168.9.237:8081/tomcatha/
可以正常访问:
拷贝节点1部署好的TOMCAT到节点2
[root@tomcatha1 ~]# zip -r tomcat.zip apache-tomcat-6.0.44
[root@tomcatha1 ~]# scp tomcat.zip 192.168.9.238:/root
root@192.168.9.238's password:
tomcat.zip 100% 7323KB 7.2MB/s 00:00
[root@tomcatha1 ~]#
登录节点2部署TOMCAT
[root@tomcatha2 ~]# unzip tomcat.zip
启动和测试节点2
[root@tomcatha2 ~]# cd /root/apache-tomcat-6.0.44/bin/
[root@tomcatha2 bin]# sh startup.sh
Using CATALINA_BASE: /root/apache-tomcat-6.0.44
Using CATALINA_HOME: /root/apache-tomcat-6.0.44
Using CATALINA_TMPDIR: /root/apache-tomcat-6.0.44/temp
Using JRE_HOME: /usr
Using CLASSPATH: /root/apache-tomcat-6.0.44/bin/bootstrap.jar
[root@tomcatha2 bin]# ps -ef | grep java
root 9911 1 48 14:18 pts/0 00:00:03 /usr/bin/java -Djava.util.logging.config.file=/root/apache-tomcat-6.0.44/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/root/apache-tomcat-6.0.44/endorsed -classpath /root/apache-tomcat-6.0.44/bin/bootstrap.jar -Dcatalina.base=/root/apache-tomcat-6.0.44 -Dcatalina.home=/root/apache-tomcat-6.0.44 -Djava.io.tmpdir=/root/apache-tomcat-6.0.44/temp org.apache.catalina.startup.Bootstrap start
root 9930 1679 0 14:18 pts/0 00:00:00 grep java
[root@tomcatha2 bin]#
浏览器中访问 http://192.168.9.238:8081/tomcatha/
Apache安装
[root@tomcatha1 ~]# yum install httpd
Apache配置
检查LoadModule配置,vi打开/etc/httpd/conf/httpd.conf主配置文件
加入:
# Virtual hosts
Include conf/httpd-vhosts.conf
配置http-vhost.conf以host方式配置代理转发
以VI方式打开/etc/httpd/conf/httpd-vhosts.conf
删除或注释掉原有的VirtualHost配置,加入如下配置
[root@tomcatha1 conf]# cat httpd-vhosts.conf
<VirtualHost *:80>
ErrorLog "logs/localhost-error_log"
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/localhost-access_log vcommon
ProxyRequests Off
ProxyPreserveHost on
# apache+tomcat cluster
ProxyPass / balancer://tomcat-cluster/
ProxyPassReverse / balancer://tomcat-cluster/
<Proxy balancer://tomcat-cluster/>
BalancerMember http://192.168.9.237:8081 loadfactor=1
BalancerMember http://192.168.9.238:8081 loadfactor=1
ProxySet lbmethod=bybusyness
</Proxy>
</VirtualHost>
[root@tomcatha1 conf]#
检查配置,启动apache
[root@tomcatha1 conf]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.9.237 for ServerName
[ OK ]
[root@tomcatha1 conf]#
测试集群
1.浏览器访问测试:http://192.168.9.237/tomcatha/
2.测试Load Balance
反复刷新浏览器,测试是否后端服务器是否切换。
首次访问:
刷新页面:
3.测试Failover
关闭其中一台后,反复刷新界面,测试是否可以正常使用。
APACHE+TOMCAT的简单HA配置,实现负载均衡和故障转移,不考虑后端TOMCAT的SESSION。一般用于基于HTTP协议的接口服务。
环境
节点1:RHL6.4 192.168.9.237
节点2:RHL6.4 192.168.9.238
2.在两个节点分别安装TOMCAT,作为后端服务。
3.第1台机器上安装apache作为集群WEB服务器。
环境参数:JDK1.6以上,httpd-2.4.16 Tomcat-6.0.44
下载tomcat war包
http://pan.baidu.com/s/1eQqDxho
1.Tomcat安装和配置
登录节点1,安装tomcat
[root@tomcatha1 ~]# tar -xvf apache-tomcat-6.0.44.tar.gz
部署测试工程cluster到TOMCAT
下载测试工程WAR包:tomcatha.war
[root@tomcatha1 ~]# cd apache-tomcat-6.0.44
[root@tomcatha1 apache-tomcat-6.0.44]# ll
total 116
drwxr-xr-x 2 root root 4096 Sep 30 10:35 bin
drwxr-xr-x 2 root root 4096 May 8 20:21 conf
drwxr-xr-x 2 root root 4096 Sep 30 10:35 lib
-rw-r--r-- 1 root root 56653 May 8 20:21 LICENSE
drwxr-xr-x 2 root root 4096 May 8 20:21 logs
-rw-r--r-- 1 root root 761 May 8 20:21 NOTICE
-rw-r--r-- 1 root root 9124 May 8 20:21 RELEASE-NOTES
-rw-r--r-- 1 root root 16229 May 8 20:21 RUNNING.txt
drwxr-xr-x 2 root root 4096 Sep 30 10:35 temp
drwxr-xr-x 7 root root 4096 May 8 20:21 webapps
drwxr-xr-x 2 root root 4096 May 8 20:21 work
[root@tomcatha1 apache-tomcat-6.0.44]# cd webapps/
[root@tomcatha1 webapps]# ll
total 20
drwxr-xr-x 10 root root 4096 Sep 30 10:35 docs
drwxr-xr-x 5 root root 4096 Sep 30 10:35 examples
drwxr-xr-x 5 root root 4096 Sep 30 10:35 host-manager
drwxr-xr-x 5 root root 4096 Sep 30 10:35 manager
drwxr-xr-x 3 root root 4096 Sep 30 10:35 ROOT
[root@tomcatha1 webapps]# mkdir cluster
[root@tomcatha1 webapps]# cd cluster/
[root@tomcatha1 cluster]# rz
rz waiting to receive.
zmodem trl+C ?
100% 4 KB 4 KB/s 00:00:01 0 Errors
[root@tomcatha1 cluster]# ll
total 8
-rw-r--r-- 1 root root 4733 Sep 29 15:55 tomcatha.war
[root@tomcatha1 cluster]#
修改端口及war包方式
端口:
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
改为:
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
war包:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
改为:
<Host name="localhost" appBase="webapps"
unpackWARs="false" autoDeploy="false"
xmlValidation="false" xmlNamespaceAware="false">
路径:
在Host上加入一行:
<Context path="/tomcatha" docBase="/root/apache-tomcat-6.0.44/webapps/cluster/tomcatha.war" />
</Host>
</Engine>
启动tomcat:
[root@tomcatha1 conf]# cd /root/apache-tomcat-6.0.44/bin/
[root@tomcatha1 bin]# sh startup.sh
Using CATALINA_BASE: /root/apache-tomcat-6.0.44
Using CATALINA_HOME: /root/apache-tomcat-6.0.44
Using CATALINA_TMPDIR: /root/apache-tomcat-6.0.44/temp
Using JRE_HOME: /usr
Using CLASSPATH: /root/apache-tomcat-6.0.44/bin/bootstrap.jar
[root@tomcatha1 bin]# ps -ef | grep java
root 10245 1 63 10:46 pts/1 00:00:03 /usr/bin/java -Djava.util.logging.config.file=/root/apache-tomcat-6.0.44/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/root/apache-tomcat-6.0.44/endorsed -classpath /root/apache-tomcat-6.0.44/bin/bootstrap.jar -Dcatalina.base=/root/apache-tomcat-6.0.44 -Dcatalina.home=/root/apache-tomcat-6.0.44 -Djava.io.tmpdir=/root/apache-tomcat-6.0.44/temp org.apache.catalina.startup.Bootstrap start
root 10264 1959 0 10:46 pts/1 00:00:00 grep java
测试节点TOMCAT部署
浏览器中访问 http://192.168.9.237:8081/tomcatha/
可以正常访问:
拷贝节点1部署好的TOMCAT到节点2
[root@tomcatha1 ~]# zip -r tomcat.zip apache-tomcat-6.0.44
[root@tomcatha1 ~]# scp tomcat.zip 192.168.9.238:/root
root@192.168.9.238's password:
tomcat.zip 100% 7323KB 7.2MB/s 00:00
[root@tomcatha1 ~]#
登录节点2部署TOMCAT
[root@tomcatha2 ~]# unzip tomcat.zip
启动和测试节点2
[root@tomcatha2 ~]# cd /root/apache-tomcat-6.0.44/bin/
[root@tomcatha2 bin]# sh startup.sh
Using CATALINA_BASE: /root/apache-tomcat-6.0.44
Using CATALINA_HOME: /root/apache-tomcat-6.0.44
Using CATALINA_TMPDIR: /root/apache-tomcat-6.0.44/temp
Using JRE_HOME: /usr
Using CLASSPATH: /root/apache-tomcat-6.0.44/bin/bootstrap.jar
[root@tomcatha2 bin]# ps -ef | grep java
root 9911 1 48 14:18 pts/0 00:00:03 /usr/bin/java -Djava.util.logging.config.file=/root/apache-tomcat-6.0.44/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/root/apache-tomcat-6.0.44/endorsed -classpath /root/apache-tomcat-6.0.44/bin/bootstrap.jar -Dcatalina.base=/root/apache-tomcat-6.0.44 -Dcatalina.home=/root/apache-tomcat-6.0.44 -Djava.io.tmpdir=/root/apache-tomcat-6.0.44/temp org.apache.catalina.startup.Bootstrap start
root 9930 1679 0 14:18 pts/0 00:00:00 grep java
[root@tomcatha2 bin]#
浏览器中访问 http://192.168.9.238:8081/tomcatha/
Apache安装
[root@tomcatha1 ~]# yum install httpd
Apache配置
检查LoadModule配置,vi打开/etc/httpd/conf/httpd.conf主配置文件
加入:
# Virtual hosts
Include conf/httpd-vhosts.conf
配置http-vhost.conf以host方式配置代理转发
以VI方式打开/etc/httpd/conf/httpd-vhosts.conf
删除或注释掉原有的VirtualHost配置,加入如下配置
[root@tomcatha1 conf]# cat httpd-vhosts.conf
<VirtualHost *:80>
ErrorLog "logs/localhost-error_log"
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/localhost-access_log vcommon
ProxyRequests Off
ProxyPreserveHost on
# apache+tomcat cluster
ProxyPass / balancer://tomcat-cluster/
ProxyPassReverse / balancer://tomcat-cluster/
<Proxy balancer://tomcat-cluster/>
BalancerMember http://192.168.9.237:8081 loadfactor=1
BalancerMember http://192.168.9.238:8081 loadfactor=1
ProxySet lbmethod=bybusyness
</Proxy>
</VirtualHost>
[root@tomcatha1 conf]#
检查配置,启动apache
[root@tomcatha1 conf]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.9.237 for ServerName
[ OK ]
[root@tomcatha1 conf]#
测试集群
1.浏览器访问测试:http://192.168.9.237/tomcatha/
2.测试Load Balance
反复刷新浏览器,测试是否后端服务器是否切换。
首次访问:
刷新页面:
3.测试Failover
关闭其中一台后,反复刷新界面,测试是否可以正常使用。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29500582/viewspace-1813508/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29500582/viewspace-1813508/