使用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
随机推荐
- ioi2018集训队自选题:最短路练习题
题意:链接 定义pos[i]表示i这个值在数组里的下标. 我们先用单调栈找到每个元素左边和右边第一个比它大的元素$l_i$和$r_i$,然后建一棵二叉树,我们就叫做maxtree吧 (upd:mdzz ...
- A1035. Password
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...
- (转)git中关于fetch的使用
将远程仓库的分支及分支最新版本代码拉取到本地: 命令:git fetch 该命令执行后,不会将拉取的分支的最新代码合并到当前分支,仅仅是拉取/下载下来到本地仓库中. 首先,我们使用git branch ...
- Linux网络基本网络配置
Linux网络基本网络配置方法介绍 网络信息查看 设置网络地址: cat /etc/sysconfig/network-scripts/ifcfg-eth0 你将会看到: DEVICE=eth0 BO ...
- byteify
byteify函数 Python自带的Json库会把json文件load成Unicode对象. 如果想要变成str对象的话,就要自己去encode. def byteify(input): if is ...
- java performance tools / NetBeans Profiler / Sun BTrace / Eclipse MAT / IBM ISA
s Oracel Performace Analyzer NetBeans Profiler Eclipse MAT Sun BTrace IBM ISA end
- vuex2.0源码分析
当我们用vue在开发的过程中,经常会遇到以下问题 多个vue组件共享状态 Vue组件间的通讯 在项目不复杂的时候,我们会利用全局事件bus的方式解决,但随着复杂度的提升,用这种方式将会使得代码难以维护 ...
- ThreadLocal以及内存泄漏
ThreadLocal是什么 ThreadLocal 的作用是提供线程内的局部变量,这种变量在线程的生命周期内起作用,减少同一个线程内多个函数或者组件之间一些公共变量的传递的复杂度.但是如果滥用Thr ...
- PHP7 学习笔记(八)JetBrains PhpStorm 2017.1 x64 MySQL数据库管理工具的使用
填写基本信息 这时候我们可以看到已经连接成功的数据库了 打开一个表,我们可以很清楚的看到数据库表的数据 切换到DDL模式
- bootstrap中模态框的使用
1.代码: <%--登录模态框--%> <li><a href="#" data-toggle="modal" data-targ ...