Tomcat反向代理+负载均衡的四种方法配置

环境拓扑

环境说明:

系统环境都如下所示:

[root@nginx ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@bogon ~]# uname -rm 2.6.32-504.el6.x86_64 x86_64

此文章一共有四个实验:

1、 使用nginx服务器实现反向代理Tomcat并实现负载均衡;

2、 使用httpd的mod_proxy_http模块,实现Tomcat负载均衡;

3、 使用httpd的mod_proxy_ajp模块,实现Tomcat负载均衡;

4、 使用httpd的mod_jk模块,实现Tomcat的服务均衡。

后端Tomcat配置

四种反代+负载均衡都是使用此Tomcat部署。

Tomcat-01部署

安装jdk

[root@tomcat-1 ~]# rpm -ivh jdk-7u67-linux-x64.rpm

设置Java的环境变量

[root@tomcat-1 ~]# vim /etc/profile.d/java.sh export JAVA_HOME=/usr/java/latest #设置家目录 export PATH=$JAVA_HOME/bin:$PATH#设置命令路径 [root@tomcat-1 ~]# . /etc/profile.d/java.sh

使用-version输出Java的版本号如果可以输出那么表示Java环境部署ok

[root@tomcat-1 ~]# java -version java version "1.7.0_67" Java(TM) SE Runtime Environment (build1.7.0_67-b01) Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04,mixed mode)

安装tomcat,并添加一个应用程序实例

[root@tomcat-1 ~]#tar xf apache-tomcat-8.0.23.tar.gz -C /usr/local/ [root@tomcat-1 ~]#cd /usr/local/ [root@tomcat-1 local]# ln -s apache-tomcat-8.0.23/tomcat [root@tomcat-1 local]# cd tomcat/ [root@tomcat-1 tomcat]# mkdir -p/usr/local/tomcat/webapps/testapp/{WEB-INF,classes,lib,META-INF} [root@tomcat-1 tomcat]# vim/usr/local/tomcat/webapps/testapp/index.jsp <%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><fontcolor="red">TomcatA.xmfb.com</font></h1> <tablealign="centre" border="1"> <tr> <td>Session ID</td> <%session.setAttribute("xmfb.com","xmfb.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html>

设置tomcat环境变量

[root@tomcat-1 tomcat]# vim /etc/profile.d/tomcat.sh export CATALINA_HOME=/usr/local/tomcat export PATH=$CATALINA_HOME/bin:$PATH [root@tomcat-1 tomcat]# . /etc/profile.d/tomcat.sh

设置完成启动服务

[root@tomcat-1 tomcat]# catalina.sh start

访问testapp应用程序正常

Tomcat-02部署

除了提供的测试页面不同之外,其他设置过程都一样

[root@tomcat-2 tomcat]# vim/usr/local/tomcat/webapps/testapp/index.jsp <%@ page language="java" %> <html> <head><title>TomcatB</title></head> <body> <h1><font color="blue">TomcatB.xmfb.com</font></h1> <tablealign="centre" border="1"> <tr> <td>Session ID</td> <%session.setAttribute("xmfb.com","xmfb.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html>

设置完成访问结果如下

Nginx反向代理+负载均衡Tomcat

安装nginx

[root@nginx ~]# yum -y install nginx

启动服务

[root@nginx ~]# service nginx start

访问nginx能否正常访问

在http字段定义upstream,设置后端Tomcat主机和端口,并设置健康状态检查

[root@nginx ~]# vim /etc/nginx/nginx.conf upstreamupservers { server172.16.4.101:8080 max_fails=2 fail_timeout=1; server172.16.4.102:8080 max_fails=2 fail_timeout=1; }

转发设置将访问nginx的请求转发至upstream

[root@nginx ~]# vim /etc/nginx/conf.d/default.conf location/ { #root /usr/share/nginx/html; proxy_pass http://upservers/; indexindex.jsp index.htmlindex.htm; }

设置完成之后,访问nginx服务器的地址,就会将请求转发到后端Tomcat服务器,并且实现负载均衡

动静分离设置方法:只需要将jsp和do结尾的文件转发到后端Tomcat服务器即可

location ~* \.(jsp|do)$ { proxy_passhttp://tomcat_host:port; }

设置nginx设置完成之后,记得关闭nginx服务,以免影响下面的httpd测试

基于httpd实现反向代理+负载均衡

基于mod_proxy实现反向代理+负载均衡

修改httpd主配置文件注销中心主机

[root@httpd ~]# vim /etc/httpd/conf/httpd.conf #DocumentRoot "/var/www/html"

设置负载均衡相关信息

[root@httpd ~]# vim /etc/httpd/conf.d/mod_proxy.conf <Proxy balancer://tcsrvs> BalancerMember :8080 loadfactor=1 BalancerMember :8080 loadfactor=1 </Proxy> NameVirtualHost *:80 <VirtualHost *:80> ServerNamewww.a.com ProxyVia On ProxyRequests Off ProxyPreserveHost On <Proxy*> Orderdeny,allow Allowfrom all </Proxy> ProxyPass/status ! ProxyPass /balancer://tcsrvs/ ProxyPa***everse / balancer://tcsrvs/ <Location /> Orderdeny,allow Allowfrom all </Location> </VirtualHost>

设置完成之后启动服务,访问httpd地址就会出现负载均衡效果

基于ajp协议实现负载均衡

只要修改访问后端服务器的协议为ajp,端口为8009即可

[root@httpd~]# vim /etc/httpd/conf.d/mod_proxy.conf <Proxy balancer://tcsrvs> BalancerMember ajp://172.16.4.101:8009 loadfactor=1 BalancerMember ajp://172.16.4.102:8009 loadfactor=1 </Proxy> NameVirtualHost *:80 <VirtualHost *:80> ServerNamewww.a.com ProxyVia On ProxyRequests Off ProxyPreserveHost On <Proxy*> Orderdeny,allow Allowfrom all </Proxy> ProxyPass/status ! ProxyPass /balancer://tcsrvs/ ProxyPa***everse / balancer://tcsrvs/ <Location /> Orderdeny,allow Allowfrom all </Location> </VirtualHost>

设置完成重启httpd,即可实现负载均衡效果

设置状态页面

[root@httpd~]# vim /etc/httpd/conf.d/mod_proxy.conf <Proxy balancer://tcsrvs> BalancerMember ajp://172.16.4.101:8009 loadfactor=1 BalancerMember ajp://172.16.4.102:8009 loadfactor=1 </Proxy> NameVirtualHost *:80 <VirtualHost *:80> ServerNamewww.a.com ProxyVia On ProxyRequests Off ProxyPreserveHost On <Proxy*> Orderdeny,allow Allowfrom all </Proxy> ProxyPass/status ! ProxyPass/balancer-manager ! ProxyPass /balancer://tcsrvs/ ProxyPa***everse / balancer://tcsrvs/ <Location /> Orderdeny,allow Allowfrom all </Location> <Location /balancer-manager> SetHandler balancer-manager Proxypass! OrderDeny,Allow Allowfrom all </Location> </VirtualHost>

访问状态页面可以正常访问

基于mod_jk的负载均衡

编译mod_jk依赖httpd的开发包

[root@httpd ~]# yum -y install httpd-devel

编译安装mod_jk

tar xf tomcat-connectors-1.2.40-src.tar.gz cd tomcat-connectors-1.2.40-src/native/ whereis apxs ./configure --with-apxs=/usr/sbin/apxs make && make install

说明:如果不知道apxs文件路径,可以使用whereis查找

[root@http native]# whereis apxs apxs: /usr/sbin/apxs /usr/share/man/man8/apxs.8.gz

编译完成就有如下提示

chmod 755 /usr/lib64/httpd/modules/mod_jk.so

配置负载均衡

设置tomcat路由信息

[root@tomcat-1 ~]# vim/usr/local/tomcat/conf/server.xml <Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA"> [root@tomcat-2 ~]# vim/usr/local/tomcat/conf/server.xml <Engine name="Catalina" defaultHost="localhost"jvmRoute="TomcatB" >

修改前端httpd配置文件,设置mod_jk相关配置

[root@http conf.d]# cat mod_jk.conf LoadModule jk_modulemodules/mod_jk.so JkWorkersFile/etc/httpd/conf.d/workers.properties JkLogFilelogs/mod_jk.log JkLogLevelinfo JkMount/status ! JkMount /*tcsrvs#表示将请求转发到tcsrvs列表 JkMount/status StatA

设置workers.properties,定义负载均衡相关信息

[root@http conf.d]# cat workers.properties worker.list=tcsrvs,StatA #定义tcsrvs列表 worker.TomcatB.host=172.16.4.101 worker.TomcatB.port=8009 worker.TomcatB.type=ajp13 worker.TomcatB.lbfactor=1#设置权重 worker.TomcatA.host=172.16.4.102 worker.TomcatA.port=8009 worker.TomcatA.type=ajp13 worker.TomcatA.lbfactor=1 worker.StatA.type=status worker.tcsrvs.type=lb#代理方式为lb,也就是负载均衡 worker.tcsrvs.balance_workers=TomcatA,TomcatB #说明tcsrvs列表包含,tomcatA和TomcatB worker.tcsrvs.sticky_session=0 #关闭基于session的绑定,否则会一直定位到一台服务器,如果设置的值为1,或者不设置表示开启绑定

访问测试即可实现负载均衡功能

访问状态页面也正常