一、规划
前端Apache:192.168.10.135
后端tomcat:192.168.10.132
192.168.10.133
二、配置Tomcat
①配置server.xml
1
2
3
4
5
|
# vi /usr/local/tomcat/conf/server.xml ##位于103行
TomcatA:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA">
TomcatB:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB">
|
②配置测试页
1
2
3
|
# cd /usr/local/tomcat/webapps/ROOT
# mv index.jsp index.jsp.bak
# vi index.jsp
|
TomcatA:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="green">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>
|
TomcatB:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="yellow">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>
|
三、基于mod_jk实现
①配置mod_jk配置文件
1
2
3
4
5
6
7
8
9
|
# vi /etc/httpd/conf/mod_jk.conf
JkWorkersFile /etc/httpd/conf/workers.properties ##指出mod_jk工作所需文件位置
JkLogFile logs/mod_jk.log ##指定日志输出文件位置
JkLogLevel debug ##指定日志级别
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
JkMount /* lbcluster1 ##指定负载均衡器名称
JkMount /status/ stat1 ##指定状态信息
|
②配置worker.properties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# vi /etc/httpd/conf/workers.properties
# workers 列表
worker.list = lbcluster1,stat1
#-------------------------------------------------
# tomcat实例1
#-------------------------------------------------
worker.TomcatA.type = ajp13 ##定向包协议
worker.TomcatA.host = 192.168.10.132 ##tomcat主机地址
worker.TomcatA.port = 8009 ##TomcatA实例端口
worker.TomcatA.lbfactor = 1 ##负载均衡权重为1
#-------------------------------------------------
# tomcat实例2
#-------------------------------------------------
worker.TomcatB.type = ajp13
worker.TomcatB.host = 192.168.10.133
worker.TomcatB.port = 8009
worker.TomcatB.lbfactor = 1
#-------------------------------------------------
# load balancer worker --负载均衡控制器
# ------------------------------------------------
worker.lbcluster1.type = lb ##负载均衡work,lb内置类
worker.lbcluster1.sticky_session = 0 ##会话是否绑定
worker.lbcluster1.balance_workers = TomcatA, TomcatB ##指定集群拥有实例
worker.stat1.type = status ##状态信息
|
③配置httpd
1
2
3
|
# vi /etc/httpd/conf/httpd.conf
LoadModule jk_module modules/mod_jk.so ##加载mod_jk模块
Include /etc/httpd/conf/mod_jk.conf ##指定mod_jk配置文件
|
④测试
输入前端Apache地址查看
刷新后继续查看
前端输入http://localhost/status/查看集群状态
⑤实现会话复制
a.在 <Engine>或 <Host>元素节点下添加以下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# vi /usr/local/tomcat/conf/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="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
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.ClusterSessionListener"/>
</Cluster>
|
b.在启用集群功能的web应用程序的web.xml中添加<distributable/>以实现集群功能
1
2
3
4
5
6
7
8
|
# vi /usr/local/tomcat/conf/web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<distributable/>
# cp /usr/local/tomcat/conf/web.xml /usr/local/tomcat/webapps/ROOT/WEB-INF/
|
详情:http://tomcat.apache.org/tomcat-9.0-doc/cluster-howto.html
c.测试
输入前端Apache地址查看
刷新后继续查看
TomcatA和TomcatB已经实现了会话共享
四、基于mod_proxy实现
①查看相关代理模块是否装载(其中balancer为集群实现模块)
1
2
3
4
5
6
7
8
9
10
11
|
# httpd -D DUMP_MODULES | grep proxy
proxy_module (shared)
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_connect_module (shared)
proxy_express_module (shared)
proxy_fcgi_module (shared)
proxy_fdpass_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_scgi_module (shared)
|
②配置httpd
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# vi /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
ServerAdmin localhost
ServerName localhost
ProxyPass / balancer://cluster1/ stickysession=jsessionid
ProxyPassReverse / balancer://cluster1/
</VirtualHost>
ProxyRequests Off
<proxy balancer://cluster1>
BalancerMember ajp://192.168.10.132:8009 loadfactor=1 route=TomcatA
BalancerMember ajp://192.168.10.133:8009 loadfactor=1 route=TomcatB
ProxySet lbmethod=bytraffic
</Proxy>
|
注:
ProxySet lbmethod={byrequests|bytraffic|bybusyness}:负载均衡的实现方式
byrequests:按照请求次数负载均衡(默认)
bytraffic:按照流量负载均衡
bybusyness:安装繁忙程度负载均衡(总是分配给活跃请求数最少的服务器)
③测试
输入TomcatA地址查看:
输入TomcatB地址查看:
输入前端Apache地址查看:
刷新继续查看: