HTTP + Tomcat 实现负载均衡(httpd 使用模块: mod_proxy, mod_proxy_ajp, mod_proxy_balancer)

1、实验拓扑

在这里插入图片描述

2、各个虚机的软件安装

2.1 HTTPD

2.1.1 HTTPD 安装

[root@Tang-11 ~]# ipinfo 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.13  netmask 255.255.255.0  broadcast 192.168.1.255
--
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
[root@Tang-11 ~]# yum install httpd -y

2.1.2 HTTPD 启动

[root@Tang-11 ~]# systemctl start httpd.service
[root@Tang-11 ~]# ss -tnl
State       Recv-Q Send-Q                  Local Address:Port                                 Peer Address:Port              
LISTEN      0      128                                 *:22                                              *:*                  
LISTEN      0      100                         127.0.0.1:25                                              *:*                  
LISTEN      0      128                                :::80                                             :::*                  
LISTEN      0      128                                :::22                                             :::*                  
LISTEN      0      100                               ::1:25                                             :::*                                  

2.1.3 HTTPD 访问

在这里插入图片描述

2.1.4 HTTPD 加载的模块,确保已经加载了所需要使用的模块

[root@Tang-11 ~]# httpd -M
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:fee9:9f90. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
... ...
 systemd_module (shared)
 cgi_module (shared)

2.2 Tomcat - 2

2.2.1 Tomcat 安装

[root@Tang-6 ~]# ipinfo
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.12  netmask 255.255.255.0  broadcast 192.168.1.255
--
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
[root@Tang-6 ~]# yum list all tomcat*
Installed Packages
tomcat.noarch                                                                                     7.0.76-9.el7_6                                                                      @base
tomcat-admin-webapps.noarch                                                                       7.0.76-9.el7_6                                                                      @base
tomcat-docs-webapp.noarch                                                                         7.0.76-9.el7_6                                                                      @base
tomcat-el-2.2-api.noarch                                                                          7.0.76-9.el7_6                                                                      @base
tomcat-jsp-2.2-api.noarch                                                                         7.0.76-9.el7_6                                                                      @base
tomcat-lib.noarch                                                                                 7.0.76-9.el7_6                                                                      @base
tomcat-servlet-3.0-api.noarch                                                                     7.0.76-9.el7_6                                                                      @base
tomcat-webapps.noarch                                                                             7.0.76-9.el7_6                                                                      @base
Available Packages
tomcat-javadoc.noarch                                                                             7.0.76-9.el7_6                                                                      base 
tomcat-jsvc.noarch                                                                                7.0.76-9.el7_6                                                                      base 
tomcat-native.x86_64                                                                              1.2.21-1.el7                                                                        epel 
tomcatjss.noarch                                                                                  7.2.1-8.el7_6                                                                       base 
[root@Tang-6 ~]# yum install tomcat.noarch tomcat-admin-webapps.noarch tomcat-docs-webapp.noarch tomcat-webapps.noarch -y

2.2.2 Tomcat 启动

[root@Tang-6 ~]# systemctl start tomcat.service
[root@Tang-6 ~]# ss -tnl
State       Recv-Q Send-Q                                                Local Address:Port                                                               Peer Address:Port              
LISTEN      0      128                                                               *:22                                                                            *:*                  
LISTEN      0      100                                                       127.0.0.1:25                                                                            *:*                  
LISTEN      0      100                                                              :::8009                                                                         :::*                  
LISTEN      0      100                                                              :::8080                                                                         :::*                  
LISTEN      0      128                                                              :::22                                                                           :::*                  
LISTEN      0      100                                                             ::1:25                                                                           :::*                  
LISTEN      0      1                                                  ::ffff:127.0.0.1:8005                                                                         :::*                  

2.2.3 Tomcat 访问

在这里插入图片描述

2.3 Tomcat - 1

2.3.1 Tomcat 安装

[root@Tang-5 ~]# ipinfo 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.11  netmask 255.255.255.0  broadcast 192.168.1.255
--
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
[root@Tang-5 ~]# yum list all tomcat*
Installed Packages
tomcat.noarch                                                                                     7.0.76-9.el7_6                                                                      @base
tomcat-admin-webapps.noarch                                                                       7.0.76-9.el7_6                                                                      @base
tomcat-docs-webapp.noarch                                                                         7.0.76-9.el7_6                                                                      @base
tomcat-el-2.2-api.noarch                                                                          7.0.76-9.el7_6                                                                      @base
tomcat-jsp-2.2-api.noarch                                                                         7.0.76-9.el7_6                                                                      @base
tomcat-lib.noarch                                                                                 7.0.76-9.el7_6                                                                      @base
tomcat-servlet-3.0-api.noarch                                                                     7.0.76-9.el7_6                                                                      @base
tomcat-webapps.noarch                                                                             7.0.76-9.el7_6                                                                      @base
Available Packages
tomcat-javadoc.noarch                                                                             7.0.76-9.el7_6                                                                      base 
tomcat-jsvc.noarch                                                                                7.0.76-9.el7_6                                                                      base 
tomcat-native.x86_64                                                                              1.2.21-1.el7                                                                        epel 
tomcatjss.noarch                                                                                  7.2.1-8.el7_6                                                                       base 
[root@Tang-5 ~]# yum install tomcat.noarch tomcat-admin-webapps.noarch tomcat-docs-webapp.noarch tomcat-webapps.noarch -y

2.3.2 Tomcat 启动

[root@Tang-5 ~]# systemctl start tomcat.service
[root@Tang-5 ~]# ss -tnl
State       Recv-Q Send-Q                                                Local Address:Port                                                               Peer Address:Port              
LISTEN      0      128                                                               *:22                                                                            *:*                  
LISTEN      0      100                                                       127.0.0.1:25                                                                            *:*                  
LISTEN      0      100                                                              :::8009                                                                         :::*                  
LISTEN      0      100                                                              :::8080                                                                         :::*                  
LISTEN      0      128                                                              :::22                                                                           :::*                  
LISTEN      0      100                                                             ::1:25                                                                           :::*                  
LISTEN      0      1                                                  ::ffff:127.0.0.1:8005                                                                         :::*                  

2.3.3 Tomcat 访问

在这里插入图片描述

3、Tomcat 冷部署一个测试类应用

3.1 Tomcat - 1

3.1.1 创建相关目录

[root@Tang-6 ~]# mkdir -pv /appdata/webapps/ROOT/{lib,classes,WEB-INF}
mkdir: created directory ‘/appdata’
mkdir: created directory ‘/appdata/webapps’
mkdir: created directory ‘/appdata/webapps/ROOT’
mkdir: created directory ‘/appdata/webapps/ROOT/lib’
mkdir: created directory ‘/appdata/webapps/ROOT/classes’
mkdir: created directory ‘/appdata/webapps/ROOT/WEB-INF[root@Tang-6 ~]# mkdir /appdata/webapps/test

3.1.2 创建相关文件

[root@Tang-6 ~]# cat /appdata/webapps/test/index.jsp
<%@ page language="java" %>
<html>
	<head><title>TomcatA</title></head>
		<body>
		<h1><font color="red">Tang-6</font></h1>
		<table align="centre" border="1">
			<tr>
				<td>Session ID</td>
			<% session.setAttribute("Tang","Tang"); %>
				<td><%= session.getId() %></td>
			</tr>
			<tr>
				<td>Created on</td>
				<td><%= session.getCreationTime() %></td>
			</tr>
		</table>
	</body>
</html>
[root@Tang-6 ~]# tree /appdata/webapps/
/appdata/webapps/
├── ROOT
│   ├── classes
│   ├── lib
│   └── WEB-INF
└── test
    └── index.jsp

5 directories, 1 file

3.1.3 修改 Tomcat 配置文件,并重启服务

### 在 </Engine> </Engine> 之间添加一个 HOST ###
[root@Tang-6 ~]# vim /etc/tomcat/server.xml 
<Engine name="Catalina" defaultHost="localhost" jvmRoute="Tomcat-Tang-6">
	<Host name="localhost" appBase="/appdata/webapps" unpackWARs="true" autoDeploy="true">
    	<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
    	prefix="node1_access" suffix=".log"
        pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        <Context path="/test" docBase="test" reloadable="">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                prefix="Tang-6_test_access_" suffix=".log"
                pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        </Context>
	</Host>
</Engine>
[root@Tang-6 ~]# systemctl restart tomcat.service

3.1.4 修改 WIN7 HOSTS 文件,进行访问,并查看相关 log

在这里插入图片描述
在这里插入图片描述

3.1.5 查看相关 log

[root@Tang-6 ~]# tail /var/log/tomcat/Tang-6_test_access_2019-11-03.log 
192.168.1.6 - - [03/Nov/2019:20:42:11 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:20:42:15 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:20:42:16 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:20:42:16 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:20:42:16 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:20:42:16 +0800] "GET /test/ HTTP/1.1" 200 321

3.2 Tomcat - 2

3.2.1 创建相关目录

[root@Tang-5 ~]# mkdir -pv /appdata/webapps/ROOT/{lib,classes,WEB-INF}
mkdir: created directory ‘/appdata’
mkdir: created directory ‘/appdata/webapps’
mkdir: created directory ‘/appdata/webapps/ROOT’
mkdir: created directory ‘/appdata/webapps/ROOT/lib’
mkdir: created directory ‘/appdata/webapps/ROOT/classes’
mkdir: created directory ‘/appdata/webapps/ROOT/WEB-INF[root@Tang-5 ~]# mkdir /appdata/webapps/test

3.2.2 创建相关文件

[root@Tang-5 ~]# cat /appdata/webapps/test/index.jsp 
<%@ page language="java" %>
<html>
	<head><title>TomcatB</title></head>
		<body>
		<h1><font color="blue">Tang-5</font></h1>
		<table align="centre" border="1">
			<tr>
				<td>Session ID</td>
			<% session.setAttribute("Tang","Tang"); %>
				<td><%= session.getId() %></td>
			</tr>
			<tr>
				<td>Created on</td>
				<td><%= session.getCreationTime() %></td>
			</tr>
		</table>
	</body>
</html>	
[root@Tang-5 ~]# tree /appdata/webapps/
/appdata/webapps/
├── ROOT
│   ├── classes
│   ├── index.jsp
│   ├── lib
│   └── WEB-INF
└── test
    └── index.jsp

5 directories, 2 files

3.2.3 修改 Tomcat 配置文件,并重启服务

### 在 </Engine> </Engine> 之间添加一个 HOST ###
[root@Tang-5 ~]# vim /etc/tomcat/server.xml 
<Engine name="Catalina" defaultHost="localhost" jvmRoute="Tomcat-Tang-5">			
	<Host name="localhost" appBase="/appdata/webapps" unpackWARs="true" autoDeploy="true">
    	<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
         prefix="node1_access" suffix=".log"
         pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        <Context path="/test" docBase="test" reloadable="">
        	<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
             prefix="Tang-5_access_" suffix=".log"
             pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        </Context>
	</Host>
<Engine>
[root@Tang-5 ~]# systemctl restart tomcat.service

3.2.4 修改 WIN7 HOSTS 文件,进行访问

在这里插入图片描述
在这里插入图片描述

3.2.5 查看相关 log

[root@Tang-5 ~]# tail /var/log/tomcat/Tang-5_test_access_2019-11-03.log 
192.168.1.6 - - [03/Nov/2019:20:42:09 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:13 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:13 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:14 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:14 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:14 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:14 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:14 +0800] "GET /test/ HTTP/1.1" 200 322

4、HTTPD Server 反代设置

4.1 conf 文件修改

[root@Tang-11 ~]# ipinfo 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.13  netmask 255.255.255.0  broadcast 192.168.1.255
--
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
[root@Tang-11 ~]# cat /etc/httpd/conf.d/tomcat.conf 
<proxy balancer://tomcatservers>
	BalancerMember ajp://192.168.1.11:8009              # 注意端口号,8009AJP 使用的端口
	BalancerMember ajp://192.168.1.12:8009
	ProxySet lbmethod=byrequests
</Proxy>

<VirtualHost *:80>
	ServerName www.neotang.com
	ProxyVia On
	ProxyRequests Off
	ProxyPreserveHost Off
	<Proxy *>
		Require all granted
	</Proxy>
		ProxyPass / balancer://tomcatservers/
		ProxyPassReverse / balancer://tomcatservers/
	<Location />
		Require all granted
	</Location>
</VirtualHost>		

4.2 conf 文件语法检查,并重启服务

[root@Tang-11 ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:fee9:9f90. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@Tang-11 ~]# systemctl restart httpd.service

4.3 修改 WIN7 主机的 HOSTS 文件

在这里插入图片描述

4.4 进行访问,通过刷新网页,可以看到在轮询访问后端 Tomcat 服务器

在这里插入图片描述
在这里插入图片描述

发布了158 篇原创文章 · 获赞 7 · 访问量 9763

猜你喜欢

转载自blog.csdn.net/weixin_44983653/article/details/102889355