Tomcat多实例、负载均衡、动静分离

Tomcat多实例部署

安装jdk
[root@localhost ~]#systemctl stop firewalld.service
[root@localhost ~]#setenforce 0
[root@localhost ~]#cd /opt
[root@localhost opt]#ls
apache-tomcat-8.5.16.tar.gz  jdk-8u91-linux-x64.tar.gz  rh
[root@localhost opt]#tar xf jdk-8u91-linux-x64.tar.gz
[root@localhost opt]#ls
apache-tomcat-8.5.16.tar.gz  jdk1.8.0_91  jdk-8u91-linux-x64.tar.gz  rh
[root@localhost opt]#mv jdk
jdk1.8.0_91/               jdk-8u91-linux-x64.tar.gz
[root@localhost opt]#mv jdk1.8.0_91/ /usr/local/
[root@localhost opt]#ls /usr/local/
bin  etc  games  include  jdk1.8.0_91  lib  lib64  libexec  sbin  share  src
[root@localhost opt]#cd /etc/profile.d/ #环境变量子配置文件
[root@localhost profile.d]#ls
256term.csh                   colorgrep.csh  flatpak.sh  less.sh        vte.sh
256term.sh                    colorgrep.sh   lang.csh    PackageKit.sh  which2.csh
abrt-console-notification.sh  colorls.csh    lang.sh     vim.csh        which2.sh
bash_completion.sh            colorls.sh     less.csh    vim.sh
[root@localhost profile.d]#vim java.sh
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

[root@localhost profile.d]#source java.sh

部署Tomcat
[root@localhost profile.d]#cd /opt
[root@localhost opt]#ls
apache-tomcat-8.5.16.tar.gz  jdk-8u91-linux-x64.tar.gz  rh
[root@localhost opt]#tar xf apache-tomcat-8.5.16.tar.gz
[root@localhost opt]#ls
apache-tomcat-8.5.16  apache-tomcat-8.5.16.tar.gz  jdk-8u91-linux-x64.tar.gz  rh
[root@localhost opt]#cp -a apache-tomcat-8.5.16 /usr/local/tomcat1
[root@localhost opt]#cp -a apache-tomcat-8.5.16 /usr/local/tomcat2
[root@localhost opt]#ls /usr/local/
bin  games    jdk1.8.0_91  lib64    sbin   src      tomcat2
etc  include  lib          libexec  share  tomcat1
[root@localhost opt]#vim /usr/local/tomcat2/conf/server.xml
[root@localhost opt]#cd /usr/local/tomcat2
[root@localhost tomcat2]#ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@localhost tomcat2]#cd bin
[root@localhost bin]#ls
bootstrap.jar                 configtest.bat    setclasspath.sh  tomcat-native.tar.gz
catalina.bat                  configtest.sh     shutdown.bat     tool-wrapper.bat
catalina.sh                   daemon.sh         shutdown.sh      tool-wrapper.sh
catalina-tasks.xml            digest.bat        startup.bat      version.bat
commons-daemon.jar            digest.sh         startup.sh       version.sh
commons-daemon-native.tar.gz  setclasspath.bat  tomcat-juli.jar

[root@localhost bin]#vim startup.sh
export CATALINA_BASE=/usr/local/tomcat2 #Tomcat存放的目录位置
export CATALINA_HOME=/usr/local/tomcat2
export TOMCAT_HOME=/usr/local/tomcat2

[root@localhost bin]#vim shutdown.sh
export CATALINA_BASE=/usr/local/tomcat2
export CATALINA_HOME=/usr/local/tomcat2
export TOMCAT_HOME=/usr/local/tomcat2

[root@localhost bin]#cd /usr/local/tomcat1
[root@localhost tomcat1]#ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@localhost tomcat1]#cd bin
[root@localhost bin]#ls
bootstrap.jar                 configtest.bat    setclasspath.sh  tomcat-native.tar.gz
catalina.bat                  configtest.sh     shutdown.bat     tool-wrapper.bat
catalina.sh                   daemon.sh         shutdown.sh      tool-wrapper.sh
catalina-tasks.xml            digest.bat        startup.bat      version.bat
commons-daemon.jar            digest.sh         startup.sh       version.sh
commons-daemon-native.tar.gz  setclasspath.bat  tomcat-juli.jar
[root@localhost bin]#vim startup.sh
export CATALINA_BASE=/usr/local/tomcat1
export CATALINA_HOME=/usr/local/tomcat1
export TOMCAT_HOME=/usr/local/tomcat1
[root@localhost bin]#vim shutdown.sh
export CATALINA_BASE=/usr/local/tomcat1
export CATALINA_HOME=/usr/local/tomcat1
export TOMCAT_HOME=/usr/local/tomcat1

[root@localhost bin]#/usr/local/tomcat1/bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat1
Using CATALINA_HOME:   /usr/local/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat1/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_91/jre
Using CLASSPATH:       /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar
Tomcat started.
[root@localhost bin]#/usr/local/tomcat2/bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat2
Using CATALINA_HOME:   /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_91/jre
Using CLASSPATH:       /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.

[root@localhost bin]#netstat -ltnp |grep java
tcp6       0      0 :::8080                 :::*                    LISTEN      40828/java 
tcp6       0      0 :::8081                 :::*                    LISTEN      40885/java 
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      40828/java 
tcp6       0      0 127.0.0.1:8006          :::*                    LISTEN      40885/java 
tcp6       0      0 :::8009                 :::*                    LISTEN      40828/java 
tcp6       0      0 :::8010                 :::*                    LISTEN      40885/java 

网页输入http://192.168.65.102:8080
	   http://192.168.65.102:8081都可以出现画面

NGINX+Tomcat负载均衡、动静分离

Tomcat多实例、负载均衡、动静分离_第1张图片

部署Tomcat页面

Tomcat1
[root@localhost ~]#cd /usr/local/tomcat1/webapps/
[root@localhost webapps]#ls
docs  examples  host-manager  manager  ROOT
[root@localhost webapps]#mkdir mm
[root@localhost webapps]#cd mm
[root@localhost mm]#vim test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


JSP tomcat1 page


<% out.println("动态页面 1,Hello Tomcat1");%>




Tomcat2
[root@localhost mm]#cd /usr/local/tomcat2/webapps/
[root@localhost webapps]#mkdir mm
[root@localhost webapps]#cd mm
[root@localhost webapps]#vim test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


JSP tomcat2 page


<% out.println("动态页面 2,Hello Tomcat2");%>



[root@www webapps]#cd /usr/local/tomcat/webapps/
[root@www webapps]#ls
docs  examples  host-manager  manager  mm  ROOT  test1  test2
[root@www webapps]#cd mm
[root@www mm]#vim test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


JSP tomcat3 page


<% out.println("动态页面 2,Hello Tomcat3");%>


Tomcat多实例、负载均衡、动静分离_第2张图片

配置NGINX服务

yum安装NGINX
[root@localhost opt]# cd /etc/yum.repos.d
[root@localhost yum.repos.d]# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo

[root@localhost yum.repos.d]# mkdir repo.bak
[root@localhost yum.repos.d]# mv *.repo repo.bak
[root@localhost yum.repos.d]# ls
repo.bak
[root@localhost yum.repos.d]# vim nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[root@localhost yum.repos.d]# yum -y install nginx
  验证中      : 1:nginx-1.24.0-1.el7.ngx.x86_64                                        1/1
已安装:
  nginx.x86_64 1:1.24.0-1.el7.ngx
完毕!

部署NGINX页面
[root@localhost ~]# cd /usr/share/nginx/html/
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# mkdir mm
[root@localhost html]# cd mm
[root@localhost html]# echo '

this is nginx static web page!

' >test.html [root@localhost mm]# ls test.html

Tomcat多实例、负载均衡、动静分离_第3张图片

实现动静分离

[root@localhost html]# cd /etc/nginx/
[root@localhost nginx]# vim nginx.conf
   在 #gzip  on;下一行加入
    upstream tomcat {
      server 192.168.65.101:8080 weight=1
      server 192.168.65.102:8080 weight=1
      server 192.168.65.102:8082 weight=1
    }

[root@localhost nginx]# cd conf.d/
[root@localhost conf.d]# ls
default.conf
[root@localhost conf.d]# vim default.conf
location ~* .*\.jsp$ {
       proxy_pass http://tomcat;
       proxy_set_header HOST $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

[root@localhost nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost conf.d]# systemctl restart nginx

Tomcat多实例、负载均衡、动静分离_第4张图片

负载均衡

Tomcat多实例、负载均衡、动静分离_第5张图片

将192.168.65.104主机的nginx.repo拷贝到192.168.65.103
[root@localhost ~]# cd /etc/yum.repos.d
[root@localhost yum.repos.d]# ls
nginx.repo  repo.bak
[root@localhost yum.repos.d]# scp nginx.repo 192.168.65.103:`pwd`
The authenticity of host '192.168.65.103 (192.168.65.103)' can't be established.
ECDSA key fingerprint is SHA256:63CrYDGmi+o9Wnfxyu+ceZXb8PaFjKl3Ob3EEWD50ag.
ECDSA key fingerprint is MD5:6e:96:3c:ab:13:8b:b9:d2:cd:34:80:46:3c:52:8e:bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.65.103' (ECDSA) to the list of known hosts.
[email protected]'s password:
nginx.repo                                               100%  178   134.9KB/s   00:00

192.168.65.103
[root@localhost ~]#cd /etc/yum.repos.d/
[root@localhost yum.repos.d]#ls
CentOS-Base.repo       CentOS-fasttrack.repo  CentOS-Vault.repo  
CentOS-CR.repo         CentOS-Media.repo      epel.repo
CentOS-Debuginfo.repo  CentOS-Sources.repo    epel-testing.repo
[root@localhost yum.repos.d]#mkdir repo.bak
[root@localhost yum.repos.d]#mv *.repo repo.bak/
[root@localhost yum.repos.d]#ls
repo.bak
[root@localhost yum.repos.d]#ls
nginx.repo  repo.bak
[root@localhost yum.repos.d]# yum -y install nginx
  验证中      : 1:nginx-1.24.0-1.el7.ngx.x86_64                                        1/1
已安装:
  nginx.x86_64 1:1.24.0-1.el7.ngx
完毕!

104
[root@localhost yum.repos.d]# cd /etc/nginx/
[root@localhost nginx]# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params
[root@localhost nginx]# scp -r conf.d/ nginx.conf 192.168.65.103:`pwd`
[email protected]'s password:
default.conf                                             100% 1292   324.4KB/s   00:00
nginx.conf                                               100%  804   630.6KB/s   00:00
[root@localhost nginx]# cd /usr/share/nginx/html/
[root@localhost html]# ls
50x.html  index.html  mm
[root@localhost html]# vim mm/test.html

this is nginx static web1 page!

[root@localhost html]# scp -r mm/ 192.168.65.103:`pwd` [email protected]'s password: test.html 100% 40 12.1KB/s 00:00 103 [root@localhost yum.repos.d]# cd /usr/share/nginx/html/ [root@localhost html]# ls 50x.html index.html mm [root@localhost html]# vim mm/test.html

this is nginx static web2 page!

[root@localhost html]# cd /etc/nginx/ [root@localhost nginx]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@localhost nginx]# systemctl start nginx 输入以下网址出现下图 http://192.168.65.103/mm/test.html http://192.168.65.103/mm/test.jsp

Tomcat多实例、负载均衡、动静分离_第6张图片

Tomcat多实例、负载均衡、动静分离_第7张图片

192.168.65.105
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
nginx-1.18.0 .tar.gz  rh
[root@localhost opt]# useradd -M -s /sbin/nologin nginx
[root@localhost opt]# tar xf nginx-1.18.0\ .tar.gz
[root@localhost opt]# ls
nginx-1.18.0  nginx-1.18.0 .tar.gz  rh
[root@localhost opt]# cd nginx-1.18.0/
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module \
> --with-stream
[root@localhost nginx-1.18.0]# make -j2 && make install
[root@localhost conf]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf  html  logs  sbin
[root@localhost nginx]# cd conf/
[root@localhost conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@localhost conf]# vim nginx.conf
在http上一行添加
stream {
    upstream nginx_server{
       server 192.168.65.103:80;
       server 192.168.65.104:80;
     }
     server {
        listen 80;
        proxy_pass nginx_server;
    }
}
下边的80监听端口改为8333
[root@localhost conf]# cd ..
[root@localhost nginx]# cd sbin/
[root@localhost sbin]# ./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
[root@localhost sbin]# /usr/local/nginx/sbin/nginx  #启动NGINX
[root@localhost sbin]# netstat -lnpt | grep nginx
tcp        0      0 0.0.0.0:8333            0.0.0.0:*               LISTEN      5480/nginx: master
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5480/nginx: master
[root@localhost sbin]# cd ..
[root@localhost nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@localhost nginx]# cd conf
[root@localhost conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@localhost conf]# vim nginx.conf
keepalive_timeout  0;  #修改长连接时间为0,103和104也同样修改
#keepalive_timeout  65;
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload #重启服务
105未设置页面,但是可以访问其他的静态和动态页面

你可能感兴趣的:(tomcat,负载均衡)