Nginx+Tomcat负载均衡群集(实战可跟做!)

快乐de馒头 2019-12-04

Nginx+Tomcat负载均衡群集

nginx处理静态

tomcat处理动态

合并为动静分离(前后端分离)

Tomcat重要目录

bin:存放启动和关闭Tomcat脚本
conf:存放Tomcat不同的配置文件
doc:存放Tomcat文档
lib/japser/common:存放Tomcat运行需要的库文件
logs:存放Tomcat执行时的LOG文件
src:存放Tomcat的源代码
webapps:Tomcat的主要Web发布目录

Demo前置知识点:

Nginx静态处理优势

1.Nginx处理静态页面的效率远高于Tomcat的处理能力
2.如果Tomcat的请求量为1000次,则Nginx的请求量为6000次
3.Tomcat每秒的吞吐量为0 6M,Nginx的每秒吞吐量为3.6M
4.Nginx处理静态资源的能力是Tomcat处理的6倍,优势可见一斑

动静分离原理:服务端接收来自客户端的请求中,既有静态资源也有动态资源

环境准备:

nginx:192.168.18.147---->CentOS 7-2

Tomcat 1:192.168.18.128---->CentOS 7-3

Tomcat 2:129.168.18.148---->CentOS 7-4

Client:192.168.18.129---->Win 7-1

Tomcat 1服务器CentOS 7-3的操作:

[ ~]# hostnamectl set-hostname tomcat1
[ ~]# su
[ ~]# systemctl stop firewalld.service 
[ ~]# setenforce 0
[ ~]# mkdir /aaa
[ ~]# mount.cifs //192.168.10.189/rpm /aaa
Password for //192.168.10.189/rpm:  
[ ~]# cd /aaa/tomcat/
[ tomcat]# ls
apache-tomcat-7.0.54.tar.gz  jdk-8u91-linux-x64.tar.gz
apache-tomcat-8.5.16.tar.gz  nginx-1.12.0.tar.gz
jdk-7u65-linux-x64.gz        nginx-1.6.0.tar.gz
[ tomcat]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/

#设置环境变量:
[ tomcat]# vim /etc/profile   
#按大写字母G到末行,按小写字母o在下行插入以下内容:
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
#修改完成后,按Esc退出插入模式,输入:wq保存退出
[ tomcat]# source /etc/profile
[ tomcat]# ls
apache-tomcat-7.0.54.tar.gz  jdk-8u91-linux-x64.tar.gz
apache-tomcat-8.5.16.tar.gz  nginx-1.12.0.tar.gz
jdk-7u65-linux-x64.gz        nginx-1.6.0.tar.gz
[ tomcat]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/
[ tomcat]# cd /usr/local/
[ local]# ls
apache-tomcat-8.5.16  etc    include      lib    libexec  share
bin                   games  jdk1.8.0_91  lib64  sbin     src
[ local]# mv apache-tomcat-8.5.16/ tomcat
[ local]# ls
bin  games    jdk1.8.0_91  lib64    sbin   src
etc  include  lib          libexec  share  tomcat
[ local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
[ local]# ln -s /usr/local/tomcat/bin/shutdown..sh /usr/local/bin/
[ local]# mkdir -pv /web/webapp1
mkdir: 已创建目录 "/web"
mkdir: 已创建目录 "/web/webapp1"
[ local]# cd /web/webapp1/
[ webapp1]# vim index.jsp
<%@ page language="java" import="java.until.*" pageEncoding="UTF-8"%>
<html>
 <head>
  <title>JSP test1 page</title>
</head>
 <body>
  <% out.println("Welcome ACCP Web");%>
 </body>
</html>
#添加完成后输入:wq保存退出
[ webapp1]# vim /usr/local/tomcat/conf/server.xml 
#输入/Host查找到以下内容
<Host name="localhost"  appBase="webapps"
unpackWARs="true" autoDeploy="true">
#在此行下插入,添加站点目录
<Context docBase="/web/webapp1" path="" reloadable="false">
</Context>
#添加完成后输入:wq保存退出
[ webapp1]# startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_91/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.ja
Tomcat started.

到宿主机中的浏览器输入:192.168.18.128:8080 可以看到:Welcome KGC Web

Nginx+Tomcat负载均衡群集(实战可跟做!)

Tomcat 2服务器CentOS 7-4的操作:

[ ~]# hostnamectl set-hostname tomcat2
[ ~]# su
[ ~]# systemctl stop firewalld.service 
[ ~]# setenforce 0
[ ~]# mkdir /aaa
[ ~]# mount.cifs //192.168.10.189/rpm /aaa
Password for //192.168.10.189/rpm:  
[ ~]# cd /aaa/tomcat/
[ tomcat]# ls
apache-tomcat-7.0.54.tar.gz  jdk-8u91-linux-x64.tar.gz
apache-tomcat-8.5.16.tar.gz  nginx-1.12.0.tar.gz
jdk-7u65-linux-x64.gz        nginx-1.6.0.tar.gz
[ tomcat]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/

#设置环境变量:
[ tomcat]# vim /etc/profile   
#按大写字母G到末行,按小写字母o在下行插入以下内容:
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
#修改完成后,按Esc退出插入模式,输入:wq保存退出
[ tomcat]# source /etc/profile
[ tomcat]# ls
apache-tomcat-7.0.54.tar.gz  jdk-8u91-linux-x64.tar.gz
apache-tomcat-8.5.16.tar.gz  nginx-1.12.0.tar.gz
jdk-7u65-linux-x64.gz        nginx-1.6.0.tar.gz
[ tomcat]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/
[ tomcat]# cd /usr/local/
[ local]# ls
apache-tomcat-8.5.16  etc    include      lib    libexec  share
bin                   games  jdk1.8.0_91  lib64  sbin     src
[ local]# mv apache-tomcat-8.5.16/ tomcat
[ local]# ls
bin  games    jdk1.8.0_91  lib64    sbin   src
etc  include  lib          libexec  share  tomcat
[ local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
[ local]# ln -s /usr/local/tomcat/bin/shutdown..sh /usr/local/bin/
[ local]# mkdir -pv /web/webapp1
mkdir: 已创建目录 "/web"
mkdir: 已创建目录 "/web/webapp1"
[ local]# cd /web/webapp1/
[ webapp1]# vim index.jsp
<%@ page language="java" import="java.until.*" pageEncoding="UTF-8"%>
<html>
 <head>
  <title>JSP test1 page</title>
</head>
 <body>
  <% out.println("Welcome KGC Web");%>
 </body>
</html>
#添加完成后输入:wq保存退出
[ webapp1]# vim /usr/local/tomcat/conf/server.xml 
#输入/Host查找到以下内容
<Host name="localhost"  appBase="webapps"
unpackWARs="true" autoDeploy="true">
#在此行下插入,添加站点目录
<Context docBase="/web/webapp1" path="" reloadable="false">
</Context>
#添加完成后输入:wq保存退出
[ webapp1]# startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_91/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.ja
Tomcat started.

到宿主机中的浏览器输入:192.168.18.148:8080 可以看到:Welcome ACCP Web

Nginx+Tomcat负载均衡群集(实战可跟做!)

Nginx服务器CentOS 7-2的操作:

[ ~]# hostnamectl set-hostname nginx
[ ~]# su
[ ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y
[ ~]# mkdir  /aaa
[ ~]# mount.cifs //192.168.10.189/rpm /aaa
Password for //192.168.10.189/rpm:  
[ ~]# cd /aaa/tomcat/
[ tomcat]# ls
apache-tomcat-7.0.54.tar.gz  jdk-8u91-linux-x64.tar.gz
apache-tomcat-8.5.16.tar.gz  nginx-1.12.0.tar.gz
jdk-7u65-linux-x64.gz        nginx-1.6.0.tar.gz
[ tomcat]# tar zxvf nginx-1.12.0.tar.gz -C /usr/local/
[ tomcat]# cd /usr/local/nginx-1.12.0/
[ nginx-1.12.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[ nginx-1.12.0]# useradd -M -s /sbin/nologin nginx
[ nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module
[ nginx-1.12.0]# make && make install
[ nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
#gzip  on;              #在下行插入以下内容
upstream tomcat_server {
          server 192.168.18.128:8080 weight=1;
          server 192.168.18.148:8080 weight=1;
}

location / {
    root   html;
    index  index.html index.htm;        #在下行插入
    proxy_pass http://tomcat_server;
}
#修改完成后按Esc退出插入模式,输入:wq保存退出
[ nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[ nginx-1.12.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[ nginx-1.12.0]# nginx
[ nginx-1.12.0]# netstat -ntap | grep 80
tcp        0      0 0.0.0.0:80         0.0.0.0:*        LISTEN        8917/nginx: master

验证:

1.在宿主机中的浏览器输入:192.168.18.147 可以看到:Welcome KGC Web

Nginx+Tomcat负载均衡群集(实战可跟做!)

####2.再次刷新可以可到:Welcome ACCP Web

Nginx+Tomcat负载均衡群集(实战可跟做!)

结果:此时达到了Nginx作为反向代理访问Tomcat服务器池!

相关推荐

lwplvx / 0评论 2020-11-22
岁月如歌 / 0评论 2020-07-21