Nginx实现后端多台Tomcat的负载均衡调度

Posted njsummer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx实现后端多台Tomcat的负载均衡调度相关的知识,希望对你有一定的参考价值。


4.1 环境及主机

Nginx实现后端多台Tomcat的负载均衡调度_tomcat


IP

主机名

角色

软件

172.16.0.8

nginx08.shone.cn

反向代理调度

nginx

172.16.0.18

T18.shone.cn

Tomcat 主机18

JDK、Tomcat

172.16.0.28

T28.shone.cn

Tomcat 主机28

JDK、Tomcat

# 因为在反向代理调度服务器上对后端得Tomcat主机是通过域名方式定义的,所以需要做好DNS域名解析,直接在hosts文件内定义
vim /etc/hosts
172.16.0.8 nginx08.shone.cn nginx08
172.16.0.18 T18.shone.cn T18
172.16.0.28 T28.shone.cn T28

4.2 后端两台 Tomcat 主机配置

[root@CentOS84-IP172-18 ]#hostnamectl set-hostname T18.shone.cn
[root@CentOS84-IP172-18 ]#exit
[root@T18 ]#cat xjzth.sh
#!/bin/bash
timedatectl set-timezone Asia/Shanghai
sed -i s/pool 2.centos.pool.ntp.org iburst/server ntp1.aliyun.com iburst/g /etc/chrony.conf
systemctl enable --now chronyd.service
systemctl restart chronyd.service
date
[root@T18 ]#
[root@T18 ]#bash xjzth.sh

[root@T18 ]#rz
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring apache-tomcat-10.0.20.tar.gz...
100% 11636 KB 1057 KB/sec 00:00:11 0 Errors
Transferring install_tomcat2022.sh...
100% 3 KB 3 KB/sec 00:00:01 0 Errors
Transferring jdk-11.0.14_linux-x64_bin.tar.gz...
100% 164726 KB 397 KB/sec 00:06:54 0 Errors

[root@T18 ]#ll
-rw-r--r-- 1 root root 11915655 Apr 8 20:53 apache-tomcat-10.0.20.tar.gz
-rw-r--r-- 1 root root 3111 Apr 9 05:44 install_tomcat2022.sh
-rw-r--r-- 1 root root 168679847 Apr 8 22:27 jdk-11.0.14_linux-x64_bin.tar.gz

[root@T18 ]#cat install_tomcat2022.sh
#!/bin/bash
#
#******************************************************************<strong>
#Author: wangxiaochun
#QQ: 29308620
#Date: 2021-03-15
#FileName: install_tomcat.sh
#URL: http://www.wangxiaochun.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#</strong>******************************************************************

DIR=`pwd`
JDK_FILE="jdk-11.0.14_linux-x64_bin.tar.gz"
TOMCAT_FILE="apache-tomcat-10.0.20.tar.gz"
JDK_DIR="/usr/local"
TOMCAT_DIR="/usr/local"

color ()
RES_COL=60
MOVE_TO_COL="echo -en \\\\033[$RES_COLG"
SETCOLOR_SUCCESS="echo -en \\\\033[1;32m"
SETCOLOR_FAILURE="echo -en \\\\033[1;31m"
SETCOLOR_WARNING="echo -en \\\\033[1;33m"
SETCOLOR_NORMAL="echo -en \\E[0m"
echo -n "$2" && $MOVE_TO_COL
echo -n "["
if [ $1 = "success" -o $1 = "0" ] ;then
$SETCOLOR_SUCCESS
echo -n $" OK "
elif [ $1 = "failure" -o $1 = "1" ] ;then
$SETCOLOR_FAILURE
echo -n $"FAILED"
else
$SETCOLOR_WARNING
echo -n $"WARNING"
fi
$SETCOLOR_NORMAL
echo -n "]"
echo




install_jdk()
if ! [ -f "$DIR/$JDK_FILE" ];then
color 1 "$JDK_FILE 文件不存在"
exit;
elif [ -d $JDK_DIR/jdk ];then
color 1 "JDK 已经安装"
exit
else
[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk* jdk

cat > /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=$JDK_DIR/jdk
export JRE_HOME=\\$JAVA_HOME/jre
export CLASSPATH=\\$JAVA_HOME/lib/:\\$JRE_HOME/lib/
export PATH=\\$PATH:\\$JAVA_HOME/bin
EOF
. /etc/profile.d/jdk.sh
java -version && color 0 "JDK 安装完成" || color 1 "JDK 安装失败" ; exit;



install_tomcat()
if ! [ -f "$DIR/$TOMCAT_FILE" ];then
color 1 "$TOMCAT_FILE 文件不存在"
exit;
elif [ -d $TOMCAT_DIR/tomcat ];then
color 1 "TOMCAT 已经安装"
exit
else
[ -d "$TOMCAT_DIR" ] || mkdir -pv $TOMCAT_DIR
fi
tar xf $DIR/$TOMCAT_FILE -C $TOMCAT_DIR
cd $TOMCAT_DIR && ln -s apache-tomcat-*/ tomcat
echo "PATH=$TOMCAT_DIR/tomcat/bin:"$PATH > /etc/profile.d/tomcat.sh
id tomcat &> /dev/null || useradd -r -s /sbin/nologin tomcat

cat > $TOMCAT_DIR/tomcat/conf/tomcat.conf <<EOF
JAVA_HOME=$JDK_DIR/jdk
EOF

chown -R tomcat.tomcat $TOMCAT_DIR/tomcat/

cat > /lib/systemd/system/tomcat.service <<EOF
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target

[Service]
Type=forking
EnvironmentFile=$TOMCAT_DIR/tomcat/conf/tomcat.conf
ExecStart=$TOMCAT_DIR/tomcat/bin/startup.sh
ExecStop=$TOMCAT_DIR/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now tomcat.service &> /dev/null
systemctl is-active tomcat.service &> /dev/null && color 0 "TOMCAT 安装完成" || color 1 "TOMCAT 安装失败" ; exit;



install_jdk
install_tomcat
[root@T18 ]#



[root@T18 ]#bash install_tomcat2022.sh
.................................
jdk-11.0.14/release
java version "11.0.14" 2022-01-18 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.14+8-LTS-263)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.14+8-LTS-263, mixed mode)
JDK 安装完成 [ OK ]
TOMCAT 安装完成 [ OK ]
[root@T18 ]#




[root@T18 ]#ll /usr/local/tomcat/conf/server.xml 
-rw------- 1 tomcat tomcat 16.负载均衡的配置场景和调度算法

16.负载均衡的配置场景和调度算法

Nginx七层负载均衡

Nginx七层负载均衡 #yyds干货盘点#

Nginx+Tomcat负载均衡及动静分离

Nginx+Tomcat的负载均衡与动静分离集群