Tomcat multi-instance deployment and load balancing and dynamic and static separation of nginx+tomcat

Tomcat multi-instance deployment

Install jdk, tomcat (see the previous blog for the process)

Configure tomcat environment variables

[root@localhost ~]# vim /etc/profile.d/tomcat.sh


#tomcat1
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1

#tomcat2
export CATALINA_HOME2=/usr/local/tomcat/tomcat2
export CATALINA_BASE2=/usr/local/tomcat/tomcat2
export TOMCAT_HOME2=/usr/local/tomcat/tomcat2






[root@localhost ~]# source /etc/profile.d/tomcat.sh

Modify the server.xml file in tomcat1 or tomcat2, requiring each tomcat instance to be configured without duplicate port numbers

[root@localhost ~]# vim /usr/local/tomcat/tomcat2/conf/server.xml


#22行,修改Server prot,默认为8005 -> 修改为8006
<Server port="8006" shutdown="SHUTDOWN">

#69行,修改Connector port,HTTP/1.1  默认为8080 -> 修改为8081
<Connector port="8081" protocol="HTTP/1.1"		

#116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />	
第一个连接器默认监听8080端口,负责建立HTTP连接。在通过浏览器访问Tomcat服务器的Web应用时,使用的就是这个连接器。
第二个连接器默认监听8009端口,负责和其他的HTTP服务器建立连接。
在把Tomcat与其他HTTP服务器集成时,需要用到这个连接器。

第三个连接器
port="8010":指定AJP连接器监听的端口号。在这个示例中,AJP连接器监听在8010端口上。

protocol="AJP/1.3":指定连接器所使用的协议。这里设置为AJP/1.3,表示使用AJP协议的版本1.3。

redirectPort="8443":指定重定向端口。当AJP连接器接收到HTTP请求时,如果请求是通过HTTPS(加密)访问的,
则会将请求重定向到8443端口。

AJP连接器用于将静态资源和动态请求从前端Web服务器(如Apache HTTP Server)转发到Tomcat服务器。
这样可以将Tomcat服务器隐藏在防火墙之后,提高安全性,同时提供更高的性能,特别是在处理动态请求时。
常见的AJP连接器配置是为了将Tomcat与Apache HTTP Server或Nginx等前端服务器集成,以实现负载均衡、反向代理等功能

Modify the startup.sh and shutdown.sh files in each tomcat instance, and add tomcat environment variables

[root@localhost ~]# vim /usr/local/tomcat/tomcat1/bin/startup.sh 

##添加以下内容
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1



[root@localhost ~]# vim /usr/local/tomcat/tomcat1/bin/shutdown.sh

##添加以下内容
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
[root@localhost ~]# vim /usr/local/tomcat/tomcat2/bin/startup.sh 


export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2



[root@localhost ~]# vim /usr/local/tomcat/tomcat2/bin/shutdown.sh


export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2

Start/restart /bin/startup.sh in each tomcat 

#启动tomcat1
/usr/local/tomcat/tomcat1/bin/startup.sh 

#启动tomcat2
/usr/local/tomcat/tomcat2/bin/startup.sh 



netstat -natp | grep java

Browser Access Test

Nginx+tomcat achieves dynamic and static separation


static: static page dynamic: dynamic page

Different requests can be initiated for static pages and dynamic pages, and different response results will be produced.

ngix reverse proxy - negative balance - > tomcat as backend server - web static nginx - > static request nginx - > dynamic page - > tomcat

The principle of Nginx to achieve load balancing

Nginx implements load balancing through a reverse proxy to realize that the Nginx server is used as the front end, the Tomcat server is used as the back end, and web page requests are forwarded by the Nginx service. But instead of forwarding all web requests, static page requests are processed by the Ncinx server itself, and dynamic page requests are forwarded to the back-end Tomcat server for processing.

Tomcat is a lightweight application server, and the number of acceptable visits may be insufficient, so we need multiple Tomcat servers. And Tomcat's concurrency processing ability is weak (about one-sixth of Nginx), so when proxying in the direction of Nginx is needed, reasonable call allocation should be made


Main configuration items for Nginx to achieve load balancing 

upstream 服务池名 {   }
作用:配置后端服务器池,以提供响应数据

proxy_pass http:// 服务池名
作用:配置将访问请求转发给后端服务器池的服务器处理

Advantages of the combination of Nginx+Tomcat load balancing

Nginx static processing advantages: the efficiency of Nginx in processing static pages is much higher than that of Tomcat. The request volume of Tomcat is 1000 times, while that of Nginx is 6000 times. The throughput per second of Tomcat is 0.6M, and the throughput per second of Nginx The volume is 3.6M, and the ability of Nginx to handle static resources is 6 times that of Tomcat

The principle of dynamic and static separation: the server receives requests from the client, including both static resources and dynamic resources. The static resources are served by Nginx, and the dynamic resources are forwarded to the backend by Nginx.

Nginx+Tomcat implements load balancing instance

Deploy nginx loader

#关闭防火墙和安全机制

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0


#安装必要组件
[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make

注意要提前装好epel源否则stream模块的组件会无法装载

准备已经安装好的nginx



Based on the previous multi-instance deployment of tomcat, just deploy a tomcat on the third server

Build the third tomcat

#关闭防火墙和安全机制

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

#软件包的方式安装jdk环境和tomcat

[root@localhost ~]# tar -zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/


[root@localhost ~]# vim /etc/profile

export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin:$PATH
 

[root@localhost ~]# source /etc/profile

#tomcat安装

[root@localhost ~]# tar zxvf apache-tomcat-9.0.16.tar.gz
 
[root@localhost ~]# mv /opt/apache-tomcat-9.0.16/ /usr/local/tomcat

#重启tomcat服务
/usr/local/tomcat/bin/shutdown.sh 

/usr/local/tomcat/bin/startup.sh
#查看端口
[root@localhost ~]# netstat -ntap | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN     70787/java          

Go back to the multi-instance server and configure dynamic pages

#创建指定网页存放目录

[root@localhost ~]# mkdir -p /usr/local/tomcat/tomcat1/webapps/test
[root@localhost ~]# mkdir -p /usr/local/tomcat/tomcat2/webapps/test


Tomcat1配置:

[root@localhost ~]# vim /usr/local/tomcat/tomcat1/webapps/test/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("动态页面1,test1 page!!!");%>
</body>
</html>


[root@localhost ~]# vim /usr/local/tomcat/tomcat1/conf/server.xml  
#要把前面的host配置删除
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/tomcat1/webapps/test" path="" reloadable="true" />
</Host>
#重启tomcat1	
/usr/local/tomcat/tomcat1/bin/shutdown.sh 
/usr/local/tomcat/tomcat1/bin/startup.sh 

Tomcat2的配置

[root@localhost ~]# vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2  page</title>  
</head>
<body>
<% out.println("动态页面2,test2 page!!!");%>
</body>
</html>

[root@localhost ~]# vim /usr/local/tomcat/tomcat2/conf/server.xml
#要把前面的host配置删除
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/tomcat2/webapps/test" path="" reloadable="true" />
#重启tomcat2
/usr/local/tomcat/tomcat2/bin/shutdown.sh 
/usr/local/tomcat/tomcat2/bin/startup.sh

Configure nginx load balancing and dynamic and static separation on the prepared nginx server

[root@localhost ~]# vim /usr/local/nginx/html/index.html

<html>
<body>
<h1> this is Nginx static test !</h2>
<img src="preview.jpg"/>
</body>
</html>


[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	#gzip on;
	
	#配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大
	upstream zh1 {
		server 20.0.0.100:8080 weight=1;            #多实例tomcat1
		server 20.0.0.100:8081 weight=1;            #多实例tomcat2
        server 20.0.0.32:8080 weight=1;

	}
	
	server {
		listen 80;
		server_name localhost;
	
		charset utf-8;
	
		#access_log logs/host.access.log main;
		
		#配置Nginx处理动态页面请求,将 .jsp文件请求转发到Tomcat 服务器处理
		location ~ .*\.jsp$ {
			proxy_pass http://tomcat_server;
           #设置后端的Web服务器可以获取远程客户端的真实IP
           #设定后端的Web服务器接收到的请求访问的主机名(域名或IP、端口),默认HOST的值为proxy_pass指令设置的主机名。如果反向代理服务器不重写该请求头的话,那么后端真实服务器在处理时会认为所有的请求都来自反向代理服务器,如果后端有防攻击策略的话,机器就被封了。
			proxy_set_header HOST $host;
           #把$remote_addr赋值给X-Real-IP,来获取源IP
			proxy_set_header X-Real-IP $remote_addr;
           #在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		}
		
		  #配置Nginx处理静态图片请求
		location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
			root /usr/local/nginx/html;
			expires 10d;
		}
		
		location / {
			root html;
			index index.html index.htm;
		}
......
	}
......
}

location ~ .*\.jsp$ {
			proxy_pass http://zh1;
			proxy_set_header HOST $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		}

access test

static page access

 Static pages request access to dynamic pages

poll after refresh once

poll after refresh

 

 

 

Guess you like

Origin blog.csdn.net/ZZZ_CCC01/article/details/132225022