May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Tomcat load balancing using Nginx reverse proxies.

This essay focuses on Tomcat clusters and Tomcat load balancing using Nginx reverse proxies.

First, we need to literacy some knowledge points (to literacy, embarrassment):
Cluster (Cluster)
Simply put, N servers form a loosely coupled multiprocessor system (external is a server), internal communication through the network. Let N servers cooperate with each other and jointly carry the request pressure of a website. In the words of the last author is “the same business, deployed on multiple servers,” This is the cluster. The more important task in the cluster is task scheduling.
Load Balance
simply means that the request is distributed to each server in the cluster according to a certain load policy, and the entire server group processes the website request so as to jointly complete the task.
2, the installation environment is as follows:
Tencent cloud host, the installation is CentOS 7.3 64bits
Nginx 1.7.4
JDK8 and Tomcat8

Configure Nginx web reverse proxy to implement two Tomcat load balancing:

–Install and configure Tomcat tar -zxvf apache-tomcat-8.5.28.tar.gz
cp -rf apache-tomcat-8.5.28 /usr/local/
tomcat1 mv apache-tomcat-8.5.28 /usr/local/tomcat2

– Modify the tomcat1 port number
$ cd /usr/local/tomcat1/conf/
$ cp server.xml server.xml.bak
$ cp web.xml web.xml.bak
$ vi server.xml

# # Modify the Tomcat2 port number
$ cd / usr / local / tomcat / conf /
$ cp server.xml server.xml.bak
$ cp web.xml web.xml.bak
$ vi server.xml

– Add Tomcat1 to boot automatically
– Copy the /usr/local/tomcat1/bin/catalina.sh file to the /etc/init.d directory and rename it to tomcat1
# cp /usr/local/tomcat1/bin/catalina. Sh /etc/init.d/tomcat1 –Modify the
/etc/init.d/tomcat1 file and add it in the file:
# vi /etc/init.d/tomcat1 –Enter the
following in the first line (otherwise Error: The tomcat service does not support chkconfig):

#!/bin/sh
# chkconfig: 2345 10 90
# description:Tomcat1 service

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
……………………. ……………………………………..
# $ Id: catalina. Sh 1498485 2013-07-01 14:37:43Z markt $
# ———————————– ——————————————

CATALINA_HOME=/usr/local/tomcat1
JAVA_HOME=/usr/local/jdk8

# OS specific support. $var _must_ be set to either true or false.

– Add tomcat service:
# chkconfig –add tomcat1
– Set tomcat to boot from:
# chkconfig tomcat1 on – Tomcat2 will be set to boot from
– Copy /usr/local/tomcat2/bin/catalina.sh file To /etc/init.d directory and renamed tomcat2
# cp /usr/local/tomcat2/bin/catalina.sh /etc/init.d/tomcat2
– Modify / etc / init.d / tomcat2 file in the file :
# Vi /etc/init.d/tomcat2
– Enter the following in the first line (otherwise error: tomcat service does not support chkconfig):
#! / Bin / sh
# chkconfig: 2345 10 90
# description: Tomcat2 Service

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
……………………. ……………………………………..
# —– ————————————————– ———————-

CATALINA_HOME=/usr/local/tomcat2
JAVA_HOME=/usr/local/jdk8

# OS specific support. $var must be set to either true or false.

– Add tomcat service:
# chkconfig –add tomcat2
— Set tomcat to start from boot:
# chkconfig tomcat2 on

Here, tomcat has been installed, respectively, enable them, print out the referenced environment corresponds to two tomcat is right:

[root @ VM_177_101_centos src] # service tomcat1 start
Using CATALINA_BASE: / usr / local / tomcat1
Using CATALINA_HOME: / usr / local / tomcat1
Using CATALINA_TMPDIR: / usr / local / tomcat1 / temp
Using JRE_HOME: / usr / local / jdk8
Using CLASSPATH : /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar
Tomcat started.

[root @ VM_177_101_centos src] # service tomcat2 start
Using CATALINA_BASE: / usr / local / tomcat2
Using CATALINA_HOME: / usr / local / tomcat2
Using CATALINA_TMPDIR: / usr / local / tomcat2 / temp
Using JRE_HOME: / usr / local / jdk8
Using CLASSPATH: /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.

Last configuration configuration Nginx:

– Switch to the directory
cd /usr/local/nginx/conf
– Modify the configuration file
vi nginx.conf

– some common configuration –
worker_processes: the number of work processes can be configured more than one
–worker_connections: the maximum number of connections
per process –server: each server is equivalent to a proxy server
–lister: monitor port, the default 80
– server_name: The current service domain name, there can be more than one, separated by a space (we are local and therefore localhost) –
Location: said path matching, then configured / that all requests are matched here
–index: when If you do not specify a home page, the specified file will be selected by default, and can be separated by more than one space.
–proxy_pass: request to a custom server list
–upstream name {}: server cluster name

– Now want to access tomcat through nginx, you need to modify the server part of the configuration

server
{
listen 80 default;
charset utf-8;
server_name localhost;
access_log logs / host.access.log;

Location / {
proxy_pass http://localhost:8080;
proxy_redirect default;
}
}
– The proxy has been completed here, so that all requests need to go through the proxy server to access the official server.

Next, load balancing is implemented. During the installation process, the port configured by tomcat1 is 8080, and the port configured by tomcat2 is 8081. Then we need to define the upstream server in the configuration file (upstream server)

# The cluster
upstream testcomcat {
#weight is the greater the weight, the greater the probability of allocation
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=2;
}

Server
{
listen 80 default;
charset utf-8;
access_log logs/host.access.log;

location / {
proxy_pass http: // testcomcat;
proxy_redirect default;
}
}

– In order to see is not the same, I will tomcat root the following index.jsp page slightly changed, respectively, joined TEST1, TEST2, to facilitate the distinction, restart nginx, the browser address bar enter the IP, visits, refresh several times Page, you will find that the switch between the two servers, as shown below:
service nginx reload

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>