Tomcat multiple instances and load balancing

Tomcat multiple instances and load balancing

  • 1. Tomcat multiple instances
    • 1.1. Install JDK
    • 1.2. Install tomcat
    • 1.3. Configure tomcat environment variables
    • 1.4. Modify the main configuration file in tomcat
    • 1.5. Modify startup script and shutdown script
    • 1.6. Start tomcat and view
  • 2. Nginx + Tomcat load balancing, dynamic and static separation
    • 2.1. Deploy Nginx load balancing
    • 2.2. Deploy the first tomcat
    • 2.3. Deploy the second tomcat
    • 2.4. nginx configuration

1. Tomcat multiple instances

1.1, Install JDK

Before deploying tomct, jdk must be installed, thinking that jdk is a necessary environment for tomcat to run.

1. #Turn off the firewall
 
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
 
2. #Transfer the software packages required to install Tomcat to the /opt directory
apache-tomcat-9.0.16.tar.gz
jdk-8u201-linux-x64.rpm
 
3. #Switch to /opt and install JDK
cd /opt
rpm -ivh jdk-8u201-linux-x64.rpm
?
4. #View java version
java-version

Turn off firewall

Transfer the software packages required to install tomcat to the /opt directory

Install jdk

Check java version

1.2. Install tomcat

1. #Switch to /opt and decompress the tomcat package
cd /opt
tar -zxf apache-tomcat-9.0.16.tar.gz
?
2. #Create a new folder /usr/local/tomcat
mkdir /usr/local/tomcat
 
3. #Copy the decompressed package to /usr/local/ and rename it
cp -a apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1
cp -a apache-tomcat-9.0.16 /usr/local/tomcat/tomcat2

Switch to /opt and unzip the tomcat package

Create a new folder /usr/local/tomcat


Copy the unpacked package to /usr/local and rename it

1.3. Configure tomcat environment variables

vim /etc profile

export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH


1.4. Modify the main configuration file in tomcat

vim /usr/local/tomcat/tomcat2/conf/server.xml
?
22 <Server port="8006" shutdown="SHUTDOWN">
69 <Connector port="8081" protocol="HTTP/1.1"
116 <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
?



1.5. Modify the startup script and shutdown script

1. #Modify /usr/local/tomcat/tomcat1/bin/startup.sh of tomcat1
vim /usr/local/tomcat/tomcat1/bin/startup.sh
?
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1
?
2. #Modify /usr/local/tomcat/tomcat1/bin/shutdown.sh of tomcat1
vim /usr/local/tomcat/tomcat1/bin/shutdown.sh
?
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1
?
?
3. #Modify /usr/local/tomcat/tomcat2bin/startup.sh of tomcat2
vim /usr/local/tomcat/tomcat2/bin/startup.sh
?
export CATALINA_HOME1=/usr/local/tomcat/tomcat2
export CATALINA_BASE1=/usr/local/tomcat/tomcat2
export TOMCAT_HOME1=/usr/local/tomcat/tomcat2
?
4. #Modify /usr/local/tomcat/tomcat2/bin/shutdown.sh of tomcat2
vim /usr/local/tomcat/tomcat2/bin/shutdown.sh
?
export CATALINA_HOME1=/usr/local/tomcat/tomcat2
export CATALINA_BASE1=/usr/local/tomcat/tomcat2
export TOMCAT_HOME1=/usr/local/tomcat/tomcat2
?

Modify /usr/local/tomcat/tomcat1/bin/startup.sh of tomcat1

Modify /usr/local/tomcat/tomcat1/bin/shutdown.sh of tomcat1

Modify tomcat2’s /usr/local/tomcat/tomcat2bin/startup.sh

Modify tomcat2’s /usr/local/tomcat/tomcat2/bin/shutdown.sh:

1.6. Start tomcat and view

1. #Start tomcat1
cd /usr/local/tomcat/
./tomcat1/bin/startup.sh
?
2. #Start tomcat2
./tomcat2/bin/startup.sh
?
3. #Check whether the startup is successful
ss -ntap|grep java
http://192.168.59.118:8080/

Start tomcat1, tomcat2


Check if startup is successful


2. Nginx + Tomcat load balancing, dynamic and static separation


  • In standalone mode, Tomcat runs alone and directly accepts user requests. It is not recommended.
    Reverse proxy, running on a single machine, provides an Nginx as a reverse proxy. It can provide static responses from nginx and dynamic jsp proxy to Tomcat.

  • LNMT: Linux + Nginx + MySQL + Tomcat
    LAMT: Linux + Apache (Httpd) + MySQL + Tomcat
    An Nginx is installed in front to perform reverse proxy and load balancing scheduling for multiple Tomcat instances. Purely dynamic pages deployed on Tomcat are more suitable.
    LNMT: Linux + Nginx + MySQL + Tomcat

  • multi-level agency
    LNNMT: Linux + Nginx + Nginx + MySQL + Tomcat
    The problem with dynamic servers is often that the concurrency capability is too weak, and multiple dynamic servers are often required to provide services together. How to allocate concurrency pressure requires scheduling, using a certain scheduling strategy to distribute requests to different servers. This is Load Balance.

When single-machine Tomcat evolved into multi-machine and multi-level deployment, a problem became apparent, which is Session. The origin of this problem is that the future development of the HTTP protocol was not considered at the beginning of its design.

2.1. Deploy Nginx load balancing

Environment introduction:
nginx:192.168.11.16
tomcat1:192.168.11.17
tomcat2:192.168.11.18

1. #Turn off the firewall
systemctl stop firewalld
setenforce 0
?
2. #Install dependency packages
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
?
3. #Create new users and groups for easy management
useradd -M -s /sbin/nologin nginx
?
4. #Switch to the opt directory and transfer the downloaded compressed package to decompress it.
cd /opt
tar -zxf nginx-1.12.0.tar.gz
?
4. #Switch to the decompressed directory and compile
cd nginx-1.12.0
?
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
?
5. #install
make & amp; & amp; make install -j4
?
6. #Make a soft connection to let the system recognize the nginx operation command
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
?
7. #Add nginx command to service
cd /lib/systemd/system
vim nginx.service
#!/bin.bash
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
?
8. #Reload unit. Start service
systemctl daemon-reload
systemctl start nginx
?
9. #Check whether the startup is successful
ss -ntap|grep nginx

http://192.168.59.108/

Turn off firewall

Install dependency packages

Create new users and groups for easy management

Switch to the opt directory and transfer the downloaded compressed package.

Switch to the decompressed directory and compile

Install

Make a soft connection to let the system recognize the nginx operation command

Add nginx to the service




Reload the unit and start the service

Check whether it started successfully

2.2. Deploy the first tomcat

192.168.11.17

1. #Turn off the firewall
systemctl stop firewalld
setenforce 0
?
2. #Switch to /opt and transfer the software packages required to install Tomcat to the /opt directory.
apache-tomcat-9.0.16.tar.gz
jdk-8u201-linux-x64.rpm
?
3. #Install JDK
rpm -ivh jdk-8u201-linux-x64.rpm
?
4. #Modify the variable configuration file
vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
?
5. #Refresh configuration file
source /etc/profile
?
6. #Switch to /opt and decompress the tomcat package
cd /opt
tar -zxf apache-tomcat-9.0.16.tar.gz
?
7. #Copy the decompressed package to /usr/local/ and rename it
cp -r apache-tomcat-9.0.16 /usr/local/tomcat
?
8. #Add user settings owner group
useradd -s /sbin/nologin tomcat
chown tomcat:tomcat /usr/local/tomcat -R
?
?
9. #Create new service file
vim /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
?
[Service]
Type=forking
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat
?
[Install]
WantedBy=multi-user.target
?
10. #Reload the service and turn it on to check whether it starts successfully
?
systemctl daemon-reload
systemctl start tomcat
ss -ntap |grep 8080
?
?
?
#############New dynamic page site###########3
?
11. #Switch to webapp and create a new test directory
cd /usr/local/tomcat/webapps/
mkdir test
?
12. #Create dynamic page file
vim test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <head>
     <title>JSP test1 page </title>
  </head>
  <body>
     <% out.println("Dynamic page 1, http://www.test1.com");%>
  </body>
</html>
?
13. #Modify the main configuration file
vim /usr/local/tomcat/conf/server.xml
Delete the original site module
Add to
<Host name="localhost" appBase="webapps"
            unpackWARs="true" autoDeploy="true" xmlValidation="false"
            xmlNamespaceAware="false">
                <Context docBase="/usr/local/tomcat/webapps/test"
                path="" reloadable="true" />
      </Host>
?
?
14. #Restart the service and test on the web page
systemctl restart tomcat.service
http://192.168.59.105:8080/

Turn off firewall

Switch to /opt and transfer the software packages required to install Tomcat to the /opt directory
Install JDK

Modify the variable configuration file


Refresh configuration file

Switch to /opt and unzip the tomcat package


Copy the decompressed package to /usr/local/ and rename it

Add user settings owner group



Reload the service and turn it on to see if it starts successfully


Switch to webapp and create a new test directory

Create dynamic page files


Modify main configuration file


Restart the service and test on the web page

2.3. Deploy the second tomcat

1. #Turn off the firewall
systemctl stop firewalld
setenforce 0
?
2. #Switch to /opt and transfer the software packages required to install Tomcat to the /opt directory.
apache-tomcat-9.0.16.tar.gz
jdk-8u201-linux-x64.rpm
?
3. #Install JDK
rpm -ivh jdk-8u201-linux-x64.rpm
?
4. #Modify environment variable configuration file
vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
?
5. #Refresh configuration file
source /etc/profile
?
6. #Switch to /opt and decompress the tomcat package
cd /opt
tar -zxf apache-tomcat-9.0.16.tar.gz
?
7. #Copy the decompressed package to /usr/local/ and rename it
cp -r apache-tomcat-9.0.16 /usr/local/tomcat
?
8. #Add user settings owner group
useradd -s /sbin/nologin tomcat
chown tomcat:tomcat /usr/local/tomcat -R
?
?
9. #Create new service file
vim /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
?
[Service]
Type=forking
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat
?
[Install]
WantedBy=multi-user.target
?
10. #Reload the service and turn it on to check whether it starts successfully
?
systemctl daemon-reload
systemctl start tomcat
ss -ntap |grep 8080
?
?
?
#############New dynamic page site###########3
?
11. #Switch to webapp and create a new test directory
cd /usr/local/tomcat/webapps/
mkdir test
?
12. #Create dynamic page file
vim test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <head>
     <title>JSP test2 page </title>
  </head>
  <body>
     <% out.println("Dynamic page 2, http://www.test2.com");%>
  </body>
</html>
?
13. #Modify the main configuration file
vim /usr/local/tomcat/conf/server.xml
Delete the original site module
Add to
<Host name="localhost" appBase="webapps"
            unpackWARs="true" autoDeploy="true" xmlValidation="false"
            xmlNamespaceAware="false">
                <Context docBase="/usr/local/tomcat/webapps/test"
                path="" reloadable="true" />
      </Host>
?
?
14. #Restart the service and test on the web page
systemctl restart tomcat.service
http://192.168.59.105:8080/

Turn off firewall

Switch to /opt and transfer the software packages required to install Tomcat to the /opt directory.

Install JDK

Modify environment variable configuration file


Refresh configuration file

Switch to /opt and unzip the tomcat package

Copy the decompressed package to /usr/local/ and rename it

Add user settings owner group

Create new service file


Reload the service and turn it on to see if it starts successfully


Switch to webapp and create a new test directory

Create dynamic page files


Modify main configuration file



Restart the service and test on the web page

2.4, nginx configuration

Prepare static pages and images

1. #Switch to the /usr/local/nginx/html/ directory
cd /usr/local/nginx/html/
?
2. #Create the test folder and create a static web page in it
mkdir test
cd test
vim test.html
this is static test web!!
?
3. #Drag a picture to test and rename it to 1.jpg
mv 1.jfif 1.jpg
?
4. #Configure the main configuration file
vim /usr/local/nginx/conf/nginx.conf
?
#Configure the load balancing server list. The weight parameter indicates the weight. The higher the weight, the greater the probability of being assigned.
#gzip on;
    upstream tomcat_server {<!-- -->
                    server 192.168.59.105:8080 weight=1;
                    server 192.168.59.118:8080 weight=1;
                 
                    }
                    
                    
#motion-stillseparation
 location ~ .*\.jsp$ {<!-- -->
          proxy_pass http://tomcat_server;
          proxy_set_header HOST $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }
?
#Static picture regularity
         location ~* .*\.(jpg|html|png|gif)$ {<!-- -->
         root /usr/local/nginx/html/test;
     }
?
?
         location / {<!-- -->
            roothtml;
            index index.html index.htm;
     }
?
?
?
5. #Restart nginx and test
http://192.168.59.108/1.jpg
http://192.168.59.108/test.html

Switch to the cd /usr/local/nginx/html/ directory


Create a test folder and create a static web page in it


Drag a picture to test

Configure the main configuration file



Restart nginx and test