基於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模塊(暫留)
準備工做--》
一、 爲了不用戶直接訪問後端Tomcat實例,影響負載均衡的效果,建議在Tomcat 7的各實例上禁用HTTP/1.1鏈接器。
二、爲每個Tomcat 7實例的引擎添加jvmRoute參數,並經過其爲當前引擎設置全局唯一標識符。以下所示。須要注意的是,每個實例的jvmRoute的值均不能相同。
三、前端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