必须要掌握的开源Web应用服务器—Tomcat多实例和负载均衡

一、Tomcat多实例

1.1 Tomcat多实例的步骤(理论)

1.安装好 jdk

2.安装 tomcat

cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mkdir /usr/local/tomcat
mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1
cp -a /usr/local/tomcat/tomcat1 /usr/local/tomcat/tomcat2


3.配置 tomcat 环境变量

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


source /etc/profile.d/tomcat.sh


4.修改 tomcat2 中的 server.xml 文件,要求各 tomcat 实例配置不能有重复的端口号

vim /usr/local/tomcat/tomcat2/conf/server.xml


<Server port="8006" shutdown="SHUTDOWN"> #22行,修改Server prot,默认为8005 -> 修改为8006
<Connector port="8081" protocol="HTTP/1.1" #69行,修改Connector port,HTTP/1.1 默认为8080 -> 修改为8081
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> #116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010


5.修改各 tomcat 实例中的 startup.sh 和 shutdown.sh 文件,添加 tomcat 环境变量

vim /usr/local/tomcat/tomcat1/bin/startup.sh


# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
##添加以下内容
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1


vim /usr/local/tomcat/tomcat1/bin/shutdown.sh


# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1


vim /usr/local/tomcat/tomcat2/bin/startup.sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2


vim /usr/local/tomcat/tomcat2/bin/shutdown.sh


# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2


6.启动各 tomcat 中的 /bin/startup.sh

/usr/local/tomcat/tomcat1/bin/startup.sh
/usr/local/tomcat/tomcat2/bin/startup.sh

netstat -natp | grep java


7.浏览器访问测试

http://192.168.98.90:8080
http://192.168.98.90:8081

1.2 Tomcat多实例的具体实验步骤(实操)

第一步:放入安装关闭防火墙和selinux

第二步:安装JDK并设置环境变量

1)安装JKD

2)编写java脚本设置JDK环境变量

3)运行脚本文件,然后查看jdk版本

第三步:安装tomcat

1)解压缩tomcat

2)创建tomcat多实例的目录,并移动复制文件

第四步:配置tomcat环境变量

1)编写脚本

2)运行脚本文件

第五步:修改 tomcat2 中的 server.xml 文件,要求各 tomcat 实例配置不能有重复的端口号

1)修改主配置文件,修改Server prot,默认为8005 -> 修改为8006

2)修改Connector port,HTTP/1.1 默认为8080 -> 修改为8081

3)修改Connector port AJP/1.3,默认为8009 -> 修改为8010

第六步:修改各 tomcat 实例中的 startup.sh 和 shutdown.sh 文件,添加 tomcat 环境变量

1)修改tomcat1的startup.sh

2)修改tomcat1的shutdown.sh

3)修改tomcat2的startup.sh

4)修改tomcat2的shutdown.sh

第七步:启动各 tomcat 中的 /bin/startup.sh

第八步:浏览器中访问测试

 

 

二、Nginx+Tomcat负载均衡、动静分离

2.1 Nginx+Tomcat负载均衡、动静分离的步骤(理论)

Nginx 服务器:192.168.98.90:80
Tomcat服务器1:192.168.98.100:80
Tomcat服务器2:192.168.98.101:8080 192.168.98.101:8081

1.部署Nginx 负载均衡器

systemctl stop firewalld
setenforce 0

yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make

useradd -M -s /sbin/nologin nginx

cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/

cd nginx-1.12.0/


./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module

make && make install


ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

vim /lib/systemd/system/nginx.service


[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target


chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service


2.部署2台Tomcat 应用服务器

systemctl stop firewalld
setenforce 0

tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/

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:$PATH


source /etc/profile

tar zxvf apache-tomcat-8.5.16.tar.gz

mv /opt/apache-tomcat-8.5.16/ /usr/local/tomcat

/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh

netstat -ntap | grep 8080


3.动静分离配置

(1)Tomcat1 server 配置

mkdir /usr/local/tomcat/webapps/test
vim /usr/local/tomcat/webapps/test/index.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title> #指定为 test1 页面
</head>
<body>
<% out.println("动态页面 1,http://www.test1.com");%>
</body>
</html>


vim /usr/local/tomcat/conf/server.xml


#由于主机名 name 配置都为 localhost,需要删除前面的 HOST 配置

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true">
</Context>
</Host>


/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh


(2)Tomcat2 server 配置

mkdir /usr/local/tomcat/tomcat1/webapps/test /usr/local/tomcat/tomcat2/webapps/test

vim /usr/local/tomcat/tomcat1/webapps/test/index.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title> #指定为 test2 页面
</head>
<body>
<% out.println("动态页面 2,http://www.test2.com");%>
</body>
</html>


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>


/usr/local/tomcat/tomcat1/bin/shutdown.sh
/usr/local/tomcat/tomcat1/bin/startup.sh


vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test3 page</title> #指定为 test3 页面
</head>
<body>
<% out.println("动态页面 3,http://www.test3.com");%>
</body>
</html>


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" />
</Host>


/usr/local/tomcat/tomcat2/bin/shutdown.sh
/usr/local/tomcat/tomcat2/bin/startup.sh


(3)Nginx server 配置

#准备静态页面和静态图片

echo '<html><body><h1>这是静态页面</h1></body></html>' > /usr/local/nginx/html/index.html
mkdir /usr/local/nginx/html/img
cp /root/game.jpg /usr/local/nginx/html/img

vim /usr/local/nginx/conf/nginx.conf


......
http {
......
#gzip on;

#配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大
upstream tomcat_server {
server 192.168.80.100:8080 weight=1;
server 192.168.80.101:8080 weight=1;
server 192.168.80.101:8081 weight=1;
}

server {
listen 80;
server_name www.kgc.com;

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/img;
expires 10d;
}

location / {
root html;
index index.html index.htm;
}
......
}
......
}


4.测试效果

测试静态页面效果
浏览器访问 http://192.168.98.90/
浏览器访问 http://192.168.98.90/game.jpg

测试负载均衡效果,不断刷新浏览器测试
浏览器访问 http://192.168.98.90/index.jsp

 

posted @ 2021-08-16 17:35  lyaeq  阅读(213)  评论(0编辑  收藏  举报