1、演示环境:

IP

操作系统

部署程序

192.168.1.143

CentOS 7.6

Apache

192.168.1.144

CentOS 7.6

Tomcat

192.168.1.145

CentOS 7.6

Tomcat

2、配置192.168.1.144节点的主机名:

# vim /etc/hosts --> 192.168.1.144 TomcatA.qiuyue.com TomcatA

# vim /etc/hostname --> TomcatA

# hostnamectl set-hostname TomcatA

# hostname TomcatA

# logout

Ctrl + Shift + r

# hostname

3、配置192.168.1.145节点的主机名:

# vim /etc/hosts --> 192.168.1.145 TomcatB.qiuyue.com TomcatB

# vim /etc/hostname --> TomcatB

# hostnamectl set-hostname TomcatB

# hostname TomcatB

# logout

Ctrl + Shift + r

# hostname

4、192.168.1.144节点自定义Tomcat Host虚拟主机:

# cd /usr/local/tomcat/conf

# cp server.xml server.xml.bak

# vim server.xml</Host></Engine>之间新增如下代码:

<Host name="TomcatA.qiuyue.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true">

<Context path="" docBase="ROOT" reloadable="true" />

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs" prefix="TomcatA_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

5、192.168.1.145节点自定义Tomcat Host虚拟主机:

# cd /usr/local/tomcat/conf

# cp server.xml server.xml.bak

# vim server.xml</Host></Engine>之间新增如下代码:

<Host name="TomcatB.qiuyue.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true">

<Context path="" docBase="ROOT" reloadable="true" />

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs" prefix="TomcatB_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

6、192.168.1.144节点创建如下目录及测试页:

# mkdir -pv /data/{webapps,logs}

# mkdir -pv /data/webapps/ROOT

# vim /data/webapps/ROOT/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>

<head><title>TomcatA</title></head>

<body>

<h1><font color="red">TomcatA.qiuyue.com</font></h1>

<table border="1">

<tr>

<td>Session ID</td>

<% session.setAttribute("qiuyue.com","qiuyue.com"); %>

<td><%= session.getId() %></td>

</tr>

<tr>

<td>Created ON</td>

<td><%= session.getCreationTime() %></td>

</tr>

</table>

</body>

</html>

7、192.168.1.145节点创建如下目录及测试页:

# mkdir -pv /data/{webapps,logs}

# mkdir -pv /data/webapps/ROOT

# vim /data/webapps/ROOT/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>

<head><title>TomcatB</title></head>

<body>

<h1><font color="blue">TomcatB.qiuyue.com</font></h1>

<table border="1">

<tr>

<td>Session ID</td>

<% session.setAttribute("qiuyue.com","qiuyue.com"); %>

<td><%= session.getId() %></td>

</tr>

<tr>

<td>Created ON</td>

<td><%= session.getCreationTime() %></td>

</tr>

</table>

</body>

</html>

8、192.168.1.144节点新增jvmRoute参数:# vim /usr/local/tomcat/conf/server.xml

<Engine name="Catalina" defaultHost="localhost">修改为

<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA">

9、192.168.1.145节点新增jvmRoute参数:# vim /usr/local/tomcat/conf/server.xml

<Engine name="Catalina" defaultHost="localhost">修改为

<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB">

10、分别启动192.168.1.144节点和192.168.1.145节点上的Tomcat

# catalina.sh stop  # catalina.sh configtest  # catalina.sh start  # ss -tunlp | grep -w :8080

11、配置192.168.1.143节点/etc/hosts文件:# vim /etc/hosts

192.168.1.144 TomcatA.qiuyue.com TomcatA

192.168.1.145 TomcatB.qiuyue.com TomcatB

12、192.168.1.143节点中使用curl命令访问:

# curl http://TomcatA.qiuyue.com:8080         //能正常显示文本内容

# curl http://TomcatB.qiuyue.com:8080         //能正常显示文本内容

13、192.168.1.143节点安装Apache实现反代、动静分离和负载均衡:

# yum -y install httpd

# cd /etc/httpd/conf

# cp httpd.conf httpd.conf.bak

# vim httpd.conf

Ø  新增代码:ServerName localhost:80

Ø  注释代码:DocumentRoot "/var/www/html"

# httpd -t  # systemctl start httpd.service  # ss -tunlp | grep -w :80

# httpd -M | grep proxy

image.png

(1)配置status状态页面:

# vim /etc/httpd/conf/httpd.conf,末尾新增如下代码:

<Location /server-status>

SetHandler server-status

Require all granted

</Location>

备注:此处未做访问控制,status状态页面用于测试反代功能

# httpd -t  # systemctl reload httpd.service  # ss -tunlp | grep -w :80

本地浏览器访问:

192.168.1.143

image.png

192.168.1.143/server-status

image.png

(2)配置虚拟主机:# vim /etc/httpd/conf.d/vhosts.conf

<Proxy balancer://testcluster>

BalancerMember http://TomcatA.qiuyue.com:8080 loadfactor=1 route=TomcatA

BalancerMember http://TomcatB.qiuyue.com:8080 loadfactor=1 route=TomcatB

</Proxy>

<VirtualHost *:80>

ServerName web.qiuyue.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost On

<Proxy *>

Require all granted

</Proxy>

ProxyPass /server-status !

ProxyPass / balancer://testcluster/

ProxyPa***everse / balancer://testcluster/

<Location />

Require all granted

</Location>

</VirtualHost>

# httpd -t  # systemctl reload httpd.service  # ss -tunlp | grep -w :80

备注:常用指令说明

Ø  ProxyVia On:每个请求和响应报文的HTTP首部均添加Via,用于在多级代理中控制代理请求的流向

Ø  ProxyRequests Off:关闭Apache正向代理功能,开启反代功能必须关闭正向代理功能

Ø  ProxyPreserveHost On:启用此功能,代理会将用户请求报文中的Host:行发送给后端服务器,而不再使用ProxyPass指定的服务器地址,如果想在反代中支持虚拟主机,则需要开启此项

Ø  ProxyPass PATH !|URL:将后端服务器某URL与当前服务器的某虚拟路径关联起来作为提供服务的路径,PATH为当前服务器上的某虚拟路径,URL为后端服务器上某URL路径,使用此指令时必须将ProxyRequests的值设置为Off,而且如果PATH/结尾,则对应的URL也必须以/结尾,反之亦然

Ø  ProxyPa***everse:让Apache调整HTTP重定向响应报文中的LocationContent-LocationURI标签所对应的URL,在反代环境中必须使用此指令避免重定向报文绕过Proxy服务器

(3)本地浏览器访问:

192.168.1.143

image.png

192.168.1.143/server-status

image.png

192.168.1.143/index.jsp,访问的是默认虚拟主机

image.png

(4)修改192.168.1.144节点Tomcat默认虚拟主机:

# catalina.sh stop

# vim server.xml

<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA">修改为

<Engine name="Catalina" defaultHost="TomcatA.qiuyue.com" jvmRoute="TomcatA">

# catalina.sh configtest  # catalina.sh start  # ss -tunlp | grep -w :8080

(5)修改192.168.1.145节点Tomcat默认虚拟主机:

# catalina.sh stop

# vim server.xml

<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB">修改为

<Engine name="Catalina" defaultHost="TomcatB.qiuyue.com" jvmRoute="TomcatB">

# catalina.sh configtest  # catalina.sh start  # ss -tunlp | grep -w :8080

(6)本地浏览器重新访问192.168.1.143/index.jsp

image.png

image.png

轮询显示,Session ID一直在变:

image.png

image.png

14、配置proxy_balancer_module内置的manager# vim /etc/httpd/conf.d/vhosts.conf

<Proxy balancer://testcluster>

BalancerMember http://TomcatA.qiuyue.com:8080 loadfactor=1 route=TomcatA

BalancerMember http://TomcatB.qiuyue.com:8080 loadfactor=1 route=TomcatB

</Proxy>

<VirtualHost *:80>

ServerName web.qiuyue.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost On

<Proxy *>

Require all granted

</Proxy>

ProxyPass /server-status !

ProxyPass /balancer-manager !

ProxyPass / balancer://testcluster/

ProxyPa***everse / balancer://testcluster/

<Location />

Require all granted

</Location>

<Location /balancer-manager>

SetHandler balancer-manager

Require all granted

</Location>

</VirtualHost>

# httpd -t  # systemctl reload httpd.service  # ss -tunlp | grep -w :80

本地浏览器访问:192.168.1.143/balancer-manager

image.png

15、实现session会话保持:# vim /etc/httpd/conf.d/vhosts.conf

Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED

<Proxy balancer://testcluster>

BalancerMember http://TomcatA.qiuyue.com:8080 loadfactor=1 route=TomcatA

BalancerMember http://TomcatB.qiuyue.com:8080 loadfactor=1 route=TomcatB

ProxySet lbmethod=bybusyness stickysession=ROUTEID

</Proxy>

<VirtualHost *:80>

ServerName web.qiuyue.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost On

<Proxy *>

Require all granted

</Proxy>

ProxyPass /server-status !

ProxyPass /balancer-manager !

ProxyPass / balancer://testcluster/

ProxyPa***everse / balancer://testcluster/

<Location />

Require all granted

</Location>

<Location /balancer-manager>

SetHandler balancer-manager

Require all granted

</Location>

</VirtualHost>

备注:lbmethodApache实现负载均衡的调度方法,默认为byrequests,即基于权重将统计请求个数进行调度;bytraffic则基于权重的流量计数调度;bybusyness通过考量每个后端服务器的当前负载进行调度。

# httpd -t  # systemctl reload httpd.service  # ss -tunlp | grep -w :80

本地浏览器访问:

192.168.1.143无论如何刷新,页面都不会改变

Google Chrome浏览器F12

image.png

备注:关闭浏览器,重新打开并访问192.168.1.143,会基于新的Cookie值进行会话保持

192.168.1.143/balancer-manager

image.png