基于mod_proxy模块
配置内容如下:
准备工作--》检查模块
# httpd -D DUMP_MODULES
……………………
proxy_module (shared)
proxy_balancer_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_connect_module (shared)
前端apache配置内容-->192.168.9.130
# cat /etc/httpd/conf.d/proxy_tomcat.conf
ProxyVia On
#关闭正向代理
ProxyRequests Off
#前端向后端转发的时候支持虚拟主机
ProxyPreserveHost On
<Proxy balancer
:
/
/lbcluster
>
#这里基于HTTP/1.1协议,也可以基于AJP/1.3,需要修改对应的后端tomcat服务器的
连接器属性,
端口也要和tomcat中定义的一致.
BalancerMember http
:
/
/
192
.
168
.
6
.
128
:
80
loadfactor
=
1
BalancerMember http
:
/
/
192
.
168
.
6
.
129
:
80
loadfactor
=
1
#设定负载均衡算法(byrequests基于权重,类似haproxy的roundrobin)(bybusiness基于当前负载情况,类似于haproxy的leastconn最少连接)(bytraffic基于后端流量)
ProxySet lbmethod
=
byrequests
<
/
Proxy
>
#如果访问的是http://ip/lbmanager,则不代理,balancer-manager可以保留代理状态信息
<
Location
/
lbmanager
>
#强制所有匹配的文件被一个指定的处理器处理
SetHandler balancer
-
manager
Proxypass
!
Order Deny,Allow
Allow from all
<
/
Location
>
#代理集群
#代理(
这里代理后的路径最后有个/,一定不能省略
)
ProxyPass
/
balancer
:
/
/
lbcluster/
#作用在于原请求之后追加上redirect的路径
ProxyPassReverse
/
balancer
:
/
/
lbcluster/
#2.2控制机制,2.4使用Require all granted
<
Location
*
>
Order allow,deny
Allow from all
<
/
Location
>
<
Location
/
>
Order allow,deny
Allow from all
<
/
Location
>
后端tomcat配置-->
所有节点--》
#vim server.xml
<Connector port
=
"80" protocol
=
"HTTP/1.1"
connectionTimeout
=
"20000"
redirectPort
=
"8443"
/
>
#mkdir -pv $CATALINA_HOME/webapps/testapp/WEB-INF/{lib,classes}
添加分布式配置文件
#cp
$CATALINA_HOME/conf/web.xml
$CATALINA_HOME/webapps/testapp/WEB-INF
#vim
$CATALINA_HOME/webapps/testapp/WEB-INF/web.xml
添加
节点A--》
192
.
168
.
6
.
128
在server.xml的Engine段中添加如下内容(也可以在Host中添加),这里主要修改本节点心跳地址和端口
#vim server.xml
<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
=
"224.0.0.4" port
=
"45564" frequency
=
"500" dropTime
=
"3000"
/
>
<Receiver className
=
"org.apache.catalina.tribes.transport.nio.NioReceiver" address
=
"192.168.6.128" 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"
/
>
<Interceptor className
=
"org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"
/
>
<
/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
>
#vim $CATALINA_HOME/webapps/testapp/index.jsp
<
%@ page language
=
"java"
%
>
<html
>
<head
>
<title
>TomcatA
<
/title
>
<
/head
>
<body
>
<h1
>
<font color
=
"red"
>TomcatA
<
/font
>
<
/h1
>
<table align
=
"centre" border
=
"1"
>
<tr
>
<td
>Session ID
<
/td
>
<
% session.setAttribute(
"abc",
"abc");
%
>
<td
>
<
%
= session.getId()
%
>
<
/td
>
<
/tr
>
<tr
>
<td
>Created on
<
/td
>
<td
>
<
%
= session.getCreationTime()
%
>
<
/td
>
<
/tr
>
<
/table
>
<
/body
>
<
/html
>
节点B--》
192
.
168
.
6
.
129
在server.xml的Engine段中添加如下内容(也可以在Host中添加)
#vim server.xml
<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
=
"224.0.0.4" port
=
"45564" frequency
=
"500" dropTime
=
"3000"
/
>
<Receiver className
=
"org.apache.catalina.tribes.transport.nio.NioReceiver" address
=
"192.168.6.129" port
=
"4001" 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"
/
>
<Interceptor className
=
"org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"
/
>
<
/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
>
#vim $CATALINA_HOME/webapps/testapp/index.jsp
<
%@ page language
=
"java"
%
>
<html
>
<head
>
<title
>TomcatB
<
/title
>
<
/head
>
<body
>
<h1
>
<font color
=
"blue"
>TomcatB
<
/font
>
<
/h1
>
<table align
=
"centre" border
=
"1"
>
<tr
>
<td
>Session ID
<
/td
>
<
% session.setAttribute(
"abc",
"abc");
%
>
<td
>
<
%
= session.getId()
%
>
<
/td
>
<
/tr
>
<tr
>
<td
>Created on
<
/td
>
<td
>
<
%
= session.getCreationTime()
%
>
<
/td
>
<
/tr
>
<
/table
>
<
/body
>
<
/html
>
测试--》访问http://192.168.9.130/testapp
如果正常,则会刷新过程中分别显示TomcatA和TomcatB,但是会话一致
说实话,我做到最后会话不知为什么不能保持,日志中已经互相检测到,但是会话疑似没有复制,这个
(暂留)
中间遇到的问题如下:
Deployer className选项可能会造成以下错误
严重
: FarmWarDeployer can only work as host cluster subelement
!
如果配置完毕后,发现不能启动tomcat,则需要将组播地址添加到路由里,并添加到/etc/crontab
route add
-net
224.
0.
0.
4 netmask
255.
255.
255.
255 dev eth0
基于mod_jk模块(暂留)
准备工作--》
1、 为了避免用户直接访问后端Tomcat实例,影响负载均衡的效果,建议在Tomcat 7的各实例上禁用HTTP/1.1连接器。
2、为每一个Tomcat 7实例的引擎添加jvmRoute参数,并通过其为当前引擎设置全局惟一标识符。如下所示。需要注意的是,每一个实例的jvmRoute的值均不能相同。
3、前端apache如果使用的是mod_jk模式,则tomcat引擎一定要定义jvmroute参数;如果前端apache使用的是mod_proxy模式,则不需要定义。
tomcat引擎的开头示例如下--->
<Engine name
=”Standalone” defaultHost
=”localhost”
jvmRoute=” TomcatA ”
>
配置过程--》
配置apache,修改/etc/httpd/extra/httpd-jk.conf为如下内容:
LoadModule jk_module modules
/mod_jk.so
JkWorkersFile
/etc
/httpd
/extra
/workers.properties
JkLogFile logs
/mod_jk.log
JkLogLevel debug
#访问根下的任意文件,都转到集群1上
JkMount
/
* lbcluster1
#访问根下的jkstatus,都转到stat1上
JkMount
/jkstatus
/ stat1
编辑/etc/httpd/extra/workers.properties,添加如下内容:
worker.list
= lbcluster1,stat1
#实例类型
worker.TomcatA.type
= ajp13
#实例内定义的虚拟主机的访问ip
worker.TomcatA.host
=
172.
16.
100.
1
#实例与连接器之间的端口
worker.TomcatA.port
=
8009
#实例权重
worker.TomcatA.lbfactor
=
5
worker.TomcatB.type
= ajp13
worker.TomcatB.host
=
172.
16.
100.
2
worker.TomcatB.port
=
8009
worker.TomcatB.lbfactor
=
5
#集群类型
worker.lbcluster1.type
= lb
#会话绑定
worker.lbcluster1.sticky_session
=
1
#调度方法
worker.lbcluster1.method
= R
#集群里含有的实例
worker.lbcluster1.balance_workers
= TomcatA, TomcatB
worker.stat1.type
= status