使用apache和nginx代理实现tomcat负载均衡及集群配置详解
实验环境:
1、nginx的代理功能
nginx proxy:
eth0: 192.168.8.48
vmnet2 eth1: 192.168.10.10
tomcat server1:
vmnet2 eth0: 192.168.10.20
tomcat server2:
vmnet2 eth0: 192.168.10.30
# yum install -y nginx-1.8.1-1.el6.ngx.x86_64.rpm
# vim /etc/nginx/conf.d/default.conf
location / {
root /web/htdocs;
index index.jsp index.html index.htm;
}
location ~* \.(jsp|do|action)$ {
proxy_pass http://192.168.10.20;
}
location ~* \.(jpg|jpeg|gif|png|pdf|doc|rar|exe|zip|)$ {
proxy_pass http://192.168.10.30;
}
2、apache的代理功能
①http方式的代理
# cd /etc/httpd/conf.d/
# vim mod_proxy.conf
加入如下内容:
ProxyRequests off
ProxyPreserveHost on
ProxyPass / http://192.168.10.20/
ProxyPassReverse / http://192.168.l0.20/
<Location />
Order Allow,Deny
Allow from all
</Location>
②ajp方式的代理
ProxyVia on
ProxyRequests off
ProxyPreserveHost on
ProxyPass / ajp://192.168.10.20/
ProxyPassReverse / ajp://192.168.l0.20/
<Location />
Order Allow,Deny
Allow from all
</Location>
②、配置apache基于mod_jk的负载均衡
a.安装apxs插件
# yum install -y httpd-devel
# tar xf tomcat-connectors-1.2.37-src.tar.gz
# cd tomcat-connectors-1.2.37-src/native
# ./configure --with-apxs=/usr/sbin/apxs
# make && make install
配置相关文件
# cd /etc/httpd/conf.d
# vim mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel notice
JkMount /* TomcatA
JkMount /status/ statA
# vim workers.properties
worker.list=TomcatA,statA
worker.TomcatA.type=ajp13
worker.ToccatA.port=8009
worker.TomcatA.host=192.168.10.20
worker.TomcatA.lbfactor=1
worker.statA.type=status
访问如下地址,可以看到tomcat server1的状态
http://192.168.8.48/status/
③使用mod_jk配置负载均衡服务器
配置代理服务器
# cd /etc/httpd/conf.d
# cat mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel notice
JkMount /* lbcA
JkMount /status/ statA
# cat workers.properties
worker.list=lbcA,statA
worker.TomcatA.type=ajp13
worker.TomcatA.port=8009
worker.TomcatA.host=192.168.10.20
worker.TomcatA.lbfactor=1
worker.TomcatB.type=ajp13
worker.TomcatB.port=8009
worker.TomcatB.host=192.168.10.30
worker.TomcatB.lbfactor=1
worker.lbcA.type=lb
worker.lbcA.sticky_session=1
worker.lbcA.balance_workers=TomcatA,TomcatB
worker.statA.type=status
配置后端tomcat
# cd /usr/local/tomcat/webapps/
# mkdir testapp
# cd testapp/
# mkdir -pv WEB-INF/{classes,lib}
mkdir: created directory `WEB-INF'
mkdir: created directory `WEB-INF/classes'
mkdir: created directory `WEB-INF/lib'
# vim index.jsp
TOMCATA服务器
index.jsp:
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.chinasoft.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("chinasoft.com.com","chinasoft.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
TOMCATB服务器
index.jsp:
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB.chinasoft.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("chinasoft.com","chinasoft.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
测试是否OK
# curl http://192.168.10.20:8080/testapp/index.jsp
# curl http://192.168.10.30:8080/testapp/index.jsp
配置session会话绑定(会破坏负载均衡效果):
负载均衡,且实现会话绑定要注意给每个tomcat实例的egine容器一个jvmRoute属性!此名称要跟前端调度模块使用名称保持一致!
另外,在mod_proxy实现负载均衡的会话绑定时,还要使用sticksession=JSESSIONID(字符要大写)!
worker.properties:
worker.TomcatB.lbfactor=1
在后端tomcat服务器server.xml文件中定义jvmRoute:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA">

④.apache mod_proxy实现基于http的负载均衡配置:
# mv mod_jk.conf mod_jk.conf.bak
# mv mod_proxy.conf.bak mod_proxy.conf
# vim mod_proxy.conf
ProxyVia on
ProxyRequests off
ProxyPreserveHost on
<Proxy balancer://lb>
BalancerMember http://192.168.10.20:8080 loadfactor=1 route=TomcatA
BalancerMember http://192.168.10.30:8080 loadfactor=1 route=TomcatB
</Proxy>
ProxyPass / balancer://lb/
ProxyPassReverse / balancer://lb/
<Location />
Order Allow,Deny
Allow from all
</Location>
⑤.配置session会话的持久性
ProxyVia on
ProxyRequests off
ProxyPreserveHost on
<Proxy balancer://lb>
BalancerMember http://192.168.10.20:8080 loadfactor=1 route=TomcatA
BalancerMember http://192.168.10.30:8080 loadfactor=1 route=TomcatB
</Proxy>
ProxyPass / balancer://lb/ stickysession=JSESSIONID
ProxyPassReverse / balancer://lb/
<Location />
Order Allow,Deny
Allow from all
</Location>
⑥.配置管理接口
ProxyVia on
ProxyRequests off
ProxyPreserveHost on
<Proxy balancer://lb>
BalancerMember http://192.168.10.20:8080 loadfactor=1 route=TomcatA
BalancerMember http://192.168.10.30:8080 loadfactor=1 route=TomcatB
</Proxy>
<Location /lbmanager>
SetHandler balancer-manager
</Location>
ProxyPass /lbmanager !
ProxyPass / balancer://lb/
ProxyPassReverse / balancer://lb/
<Location />
Order Allow,Deny
Allow from all
</Location>
访问:http://192.168.8.48/lbmanager,可以看到负载均衡的状态界面
集群配置:
负载均衡器:
192.168.10.10:
# vim mod_proxy.conf
ProxyVia on
ProxyRequests off
ProxyPreserveHost on <Proxy balancer://lb>
BalancerMember http://192.168.10.20:8080 loadfactor=1 route=TomcatA
BalancerMember http://192.168.10.30:8080 loadfactor=1 route=TomcatB
</Proxy> <Location /lbmanager>
SetHandler balancer-manager
</Location> ProxyPass /lbmanager !
ProxyPass / balancer://lb/
ProxyPassReverse / balancer://lb/ <Location />
Order Allow,Deny
Allow from all
</Location>
192.168.10.20:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8"> <Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/> <Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.1.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.10.20"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
192.168.10.30:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8"> <Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/> <Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.1.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.10.30"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
并在各集群节点上添加路由信息:
route add -net 228.1.0.4 netmask 255.255.255.255 dev eth0
并在:
web.xml节点上添加:
<distributable/>
可以看到session id没有变化
使用apache和nginx代理实现tomcat负载均衡及集群配置详解的更多相关文章
- apache的tomcat负载均衡和集群配置 "
略看了一下,感觉太复杂,要配置的东西太多,因此在这里写出一种更简洁的方法. 要集群tomcat主要是解决SESSION共享的问题,因此我利用memcached来保存session,多台TOMCAT服务 ...
- 基于apache的tomcat负载均衡和集群配置
最近不是很忙,用零碎时间做点小小的实验. 以前公司采用F5负载均衡交换机,F5将请求转发给多台服务器,每台服务器有多个webserver实例,每个webserver分布在多台服务器,交叉式的分布集群. ...
- apache + tomcat 负载均衡分布式集群配置
Tomcat集群配置学习篇-----分布式应用 现目前基于javaWeb开发的应用系统已经比比皆是,尤其是电子商务网站,要想网站发展壮大,那么必然就得能够承受住庞大的网站访问量:大家知道如果服务器访问 ...
- 【线上测试之后的应用】基于MySQL+MHA+Haproxy构建高可用负载均衡数据库集群(详解)
这里我们先介绍一下MHA是什么,其次就是它的应用与测试,同时为了大家呈现了数据备份案例,最后总结了使用情况以及注意事项和解决办法 一.MHA 概述 MHA(Master High Availabili ...
- 基于apache的tomcat负载均衡和集群配置session共享
接上面的话题接着继续完善.为什么没接到上篇呢?原因很简单太长的文章不爱看!就像有人写了上千行的方法一样,不是逼得没办法谁爱看谁看,反正我不看. 期间我没有一次配置成功,从失败的开始说起, 1.准备ja ...
- 基于nginx的tomcat负载均衡和集群
要集群tomcat主要是解决SESSION共享的问题,因此我利用memcached来保存session,多台TOMCAT服务器即可共享SESSION了. 你可以自己写tomcat的扩展来保存SESSI ...
- 基于Apache的Tomcat负载均衡和集群(2)
反向代理负载均衡 (Apache+JK+Tomcat) 使用代理服务器可以将请求转发给内部的Web服务器,让代理服务器将请求均匀地转发给多台内部Web服务器之一上,从而达到负载均衡的目的.这种代理方式 ...
- Apache + Tomcat集群配置详解 (1)
一.软件准备 Apache 2.2 : http://httpd.apache.org/download.cgi,下载msi安装程序,选择no ssl版本 Tomcat 6.0 : http://to ...
- (转)使用LVS实现负载均衡原理及安装配置详解
使用LVS实现负载均衡原理及安装配置详解 原文:https://www.cnblogs.com/liwei0526vip/p/6370103.html
随机推荐
- 【POJ1179】Polygon 区间DP
这道题是典型的环形石子归并模型,破环成链后时间复杂度为\(O(n^3)\) 不过,因为题目中所给的数字可能是负数,仅仅记录区间内合并之后的最大值并不满足动态规划的最优子结构性质.因此,还需要额外记录下 ...
- (转)Java程序员的面试经历和题库
背景:最近我在找工作,前期就像打了鸡血的一样,隔一段时间没有面试,就又松懈了下来,看到别人写的面经,感觉就像打脸一般,以后要多多总结前人的经验,时刻保持压力状态才是. 作者:nuaazhaofeng2 ...
- 关于:HTTP Header -> Content-Type: text/plain Cache-Control: no-cache IE浏览器弹出错误下载对话
下午遇到一个很奇怪的现象,一个网址: http://192.168.1.3/login?action=a&fr=b.com 注意网址后面的参数形式,action参数在前,最后一个参数值的尾部含 ...
- python之配置日志的几种方式
作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用它们的配置函数: 2)创建一个日志配置文 ...
- 斯坦福大学公开课机器学习:Neural Networks,representation: non-linear hypotheses(为什么需要做非线性分类器)
如上图所示,如果用逻辑回归来解决这个问题,首先需要构造一个包含很多非线性项的逻辑回归函数g(x).这里g仍是s型函数(即 ).我们能让函数包含很多像这的多项式,当多项式足够多时,那么你也许能够得到可以 ...
- Windows7 64下搭建Caffe+python接口环境
参考链接: http://www.cnblogs.com/yixuan-xu/p/5858595.html http://www.cnblogs.com/zf-blog/p/6139044.html ...
- linux 出现ping,错误提示:connect :network is unreachable
今天克隆Centos7后 修改IP地址 修改前: IP:172.16.0.198 默认网关:172.16.0.254 修改后: IP:172.16.1.100 默认网关:172.16.0.25 ...
- 7、Python-引用传递与值传递
在 Python 中一切皆为对象,类型属于对象,与JAVA不同,Python 中变量是没有确定类型的 在 Python 中都是引用传递 不可变类型 a = 1 b = a print(str(id(a ...
- HDU - 4901 The Romantic Hero(dp)
https://vjudge.net/problem/HDU-4901 题意 给n个数,构造两个集合,使第一个集合的异或和等于第二个集合的相与和,且要求第一个集合的元素下标都小于第二个集合的元素下标. ...
- 从匿名函数(闭包特性)到 PHP 设计模式之容器模式
匿名函数(匿名函数) 匿名函数,也叫闭包函数,它允许临时创建一个没有指定名称的函数,常用作回调函数参数的值,也可以作为变量的值来使用.具体的使用见以下示例代码: /* 示例一:声明一个简单匿名函数,并 ...