Centos7 deploy Nginx load balancing Tomcat server and session sharing architecture

Keywords: Tomcat Session Redis network

brief introduction

load balancing
Load balancing is based on the existing network structure, which provides a cheap, effective and transparent way to expand the bandwidth of network equipment and servers, increase throughput, strengthen the capacity of network data processing, and improve the flexibility and availability of the network.
The meaning of Load Balance is to allocate to multiple operation units for execution, such as Web server, FTP server, enterprise key application server and other key task servers, so as to jointly complete work tasks
session sharing
Session is the session tracking technology of communication between client and server. The server and client keep the basic information of the whole communication session
Because load balancing will allocate tasks to different machines, when users refresh the page, they will jump from machine A to machine B. at this time, all operation data of users may be lost, so it is necessary to realize data tracking through the session sharing mechanism of the cluster

preparation in advance

Prepare Centos7 server, synchronize system time, configure IP address and hostname, turn off firewall and selinux
Install openjdk

[root@localhost ~]# yum install java -y

#View installation
[root@localhost ~]# java -version
openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)

Deploy Nginx
Download the yum source from the official website and install Nginx

[root@localhost ~]# yum install nginx -y

Deploy redis

#yum install redis
[root@localhost ~]# yum install redis -y

#to configure
#Set to run as background Daemons
daemonize yes
#Set login password (optional)
masterauth "your_password"

#Start service
[root@localhost ~]# systemctl start redis

Deploy Tomcat cluster

Deploy tomcat1

Download Tomcat7 compressed package from Tomcat official website and extract it

[root@localhost ~]# tar -zxvf apache-tomcat-7.0.104.tar.gz -C /usr/local/tomcat-7

Configure service startup file

[root@localhost ~]# vi /usr/lib/systemd/system/tomcat7.service 
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/usr/local/tomcat-7/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat-7
Environment=CATALINA_BASE=/usr/local/tomcat-7
ExecStart=/usr/local/tomcat-7/bin/startup.sh
ExecStop=/usr/local/tomcat-7/bin/shutdown.sh
User=root
Group=root
[Install]
WantedBy=multi-user.target

Configure redis cache

[root@localhost ~]# vi /usr/local/tomcat-7/conf/context.xml 
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
    <!--to configure Redis cache-->
    <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
    <Manager className="com.radiadesign.catalina.session.RedisSessionManager"
    host="127.0.0.1"
    port="6379"
    #redis needs to fill in password when it has set password to log in
    password="your_password"
    database="0"
    maxInactiveInterval="60" />
</Context>

Write read redis cache file

[root@localhost ~]# vi /usr/local/tomcat-7/webapps/ROOT/session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>shared session</title>
</head>
<body>
<br>session id=<%=session.getId()%>
<br>tomcat 1
</body>
</html>

Start service

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start tomcat7

Deploy tomcat2

#Copy tomcat1 folder
[root@localhost ~]# cp -r /usr/local/tomcat-7/ /usr/local/tomcat-7-8888/

Modify profile

[root@localhost ~]# vi /usr/local/tomcat-7-8888/conf/server.xml
#Modify three port numbers
<Server port="8015" shutdown="SHUTDOWN">
<Connector port="8888" protocol="HTTP/1.1"
<Connector protocol="AJP/1.3"
address="::1"
port="8019"
redirectPort="8443" />

Configure service startup file

[root@localhost ~]# vi /usr/lib/systemd/system/tomcat7-8888.service
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/usr/local/tomcat-7-8888/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat-7-8888
Environment=CATALINA_BASE=/usr/local/tomcat-7-8888
ExecStart=/usr/local/tomcat-7-8888/bin/startup.sh
ExecStop=/usr/local/tomcat-7-8888/bin/shutdown.sh
User=root
Group=root
[Install]
WantedBy=multi-user.target

Modify the file reading redis cache

[root@localhost ~]# vi /usr/local/tomcat-7-8888/webapps/ROOT/session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>shared session</title>
</head>
<body>
<br>session id=<%=session.getId()%>
<br>tomcat 2
</body>
</html>

Modify home page display to differentiate

[root@localhost ~]# vi /usr/local/tomcat-7-8888/webapps/ROOT/index.jsp
 <span id="nav-home"><a href="${tomcatUrl}">Home-7-8888</a></span>

Start service

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start tomcat7-8888

Deploy tomcat3

#Copy tomcat1 folder
[root@localhost ~]# cp -r /usr/local/tomcat-7/ /usr/local/tomcat-7-9999/

Modify profile

[root@localhost ~]# vi /usr/local/tomcat-7-9999/conf/server.xml
#Modify three port numbers
<Server port="8025" shutdown="SHUTDOWN">
<Connector port="9999" protocol="HTTP/1.1"
<Connector protocol="AJP/1.3"
address="::1"
port="8029"
redirectPort="8443" />

Configure service startup file

[root@localhost ~]# vi /usr/lib/systemd/system/tomcat7-9999.service
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/usr/local/tomcat-7-9999/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat-7-9999
Environment=CATALINA_BASE=/usr/local/tomcat-7-9999
ExecStart=/usr/local/tomcat-7-9999/bin/startup.sh
ExecStop=/usr/local/tomcat-7-9999/bin/shutdown.sh
User=root
Group=root
[Install]
WantedBy=multi-user.target

Modify the file reading redis cache

[root@localhost ~]# vi /usr/local/tomcat-7-9999/webapps/ROOT/session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>shared session</title>
</head>
<body>
<br>session id=<%=session.getId()%>
<br>tomcat 3
</body>
</html>

Modify home page display to differentiate

[root@localhost ~]# vi /usr/local/tomcat-7-9999/webapps/ROOT/index.jsp
 <span id="nav-home"><a href="${tomcatUrl}">Home-7-9999</a></span>

Start service

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start tomcat7-9999

Configure Nginx load balancing

[root@localhost ~]# vi /etc/nginx/conf.d/default.conf
#Configure tomcat cluster
upstream serverpool{
#The tomcat cluster is called by polling by default
	server localhost:8080;
    server localhost:8888;
    server localhost:9999;
    }
#Configure services
server{
	listen 80;
	server_name localhost;
	location / {
	proxy_pass http://serverpool/;
	}
}

Start service

[root@localhost ~]# systemctl start nginx

Test verification

Verify load balancing
Browser access http://ip

Click refresh

Click refresh again

Test session sharing
Browser access http://ip/session.jsp

Click refresh

Click refresh again

View redis database records

[root@localhost ~]# redis-cli
127.0.0.1:6379> auth "your_password"
OK
127.0.0.1:6379> ping 
PONG
127.0.0.1:6379> keys *
1) "C13930F70F4B33537E99140CFEDAC637"

Posted by TKirahvi on Tue, 09 Jun 2020 21:50:10 -0700