一、Tomcat简介
- 最初是由sun的软件架构师詹姆斯·邓肯·戴维森开发
- 安装Tomcat后,安装路径下面的目录和文件,是使用或者配置Tomcat的重要文件
二、负载均衡、动静分离原理
-
standalone模式,Tomcat单独运行,直接接受用户的请求,不推荐。 反向代理,单机运行,提供了一个Nginx作为反向代理,可以做到静态由nginx提供响应,动态jsp 代理给Tomcat -
LNMT:Linux + Nginx + MySQL + Tomcat LAMT:Linux + Apache(Httpd)+ MySQL + Tomcat 前置一台Nginx,给多台Tomcat实例做反向代理和负载均衡调度,Tomcat上部署的纯动态页面更 适合 LNMT:Linux + Nginx + MySQL + Tomcat -
多级代理 LNNMT:Linux + Nginx + Nginx + MySQL + Tomcat
动态服务器的问题,往往就是并发能力太弱,往往需要多台动态服务器一起提供服务。如何把并发的压
力分摊,这就需要调度,采用一定的调度策略,将请求分发给不同的服务器,这就是Load Balance负载
均衡。
当单机Tomcat,演化出多机多级部署的时候,一个问题便凸显出来,这就是Session。而这个问题的由
来,都是由于HTTP协议在设计之初没有想到未来的发展。
三、部署一台ngnix负载均衡器
环境简介:
ngnix:192.168.111.52
tomcat1:192.168.111.50
tomcat2:192.168.111.51
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
nginx-1.12.0.tar(1).gz
[root@localhost opt]
[root@localhost opt]
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
useradd -M -s /sbin/nologin nginx
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
--with-file-aio
--with-http_gzip_static_module
--with-http_flv_module
--with-http_ssl_module
--without
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-file-aio --with-http_gzip_static_module --with-http_flv_module
make && make install
[root@localhost nginx-1.12.0]
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
vim /etc/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
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost nginx]
[root@localhost nginx]
[root@localhost nginx]
LISTEN 0 128 *:80 *:* users:(("nginx",pid=7011,fd=6),("nginx",pid=7010,fd=6))
在浏览器上验证
http://192.168.111.52/
四、部署两台tomcat
方法一:
[root@localhost opt]
[root@localhost opt]
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
[root@localhost opt]
[root@localhost opt]
[root@localhost local]
bin games jdk1.8.0_91 lib64 sbin src
etc include lib libexec share
[root@localhost local]
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JAVA_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
[root@localhost local]
[root@localhost opt]
[root@localhost tomcat]
[root@localhost tomcat]
[root@localhost tomcat]
[root@localhost webapps]
docs examples host-manager manager ROOT
[root@localhost webapps]
[root@localhost webapps]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<tile>JSP test1 page </title>
</head>
<boby>
<% out.println("动态页面1,http://www.test1.com");%>
</boby>
</html>
[root@localhost tomcat]
[root@localhost ~]
[root@localhost jdk1.8.0_201-amd64]
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
[root@localhost jdk1.8.0_201-amd64]
动静分离配置
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page </title>
</head>
<body>
<% out.println("动态页面1,http://www.test1.com");%>
</body>
</html>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true" xmlValidation="false"
xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test"
path="" reloadable="true" />
</Host>
[root@localhost tomcat]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<tile>JSP test2 page </title>
</head>
<boby>
<% out.println("动态页面2,http://www.test2.com");%>
</boby>
</html>
<Host name="www.kgc.com" appBase="/usr/local/tomcat/webapps"
unpackWARs="true" autoDeploy="true" xmlValidation="false"
xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test"
path="" reloadable="true" />
</Host>
[root@localhost tomcat]
方法二:
#关闭防火墙及selinux
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# cd /opt/
#上传安装包
apache-tomcat-9.0.16.tar.gz jdk-8u201-linux-x64.rpm
#安装jdk-8u201-linux-x64.rpm
[root@localhost opt]# rpm -ivh jdk-8u201-linux-x64.rpm
[root@localhost opt]# vim /etc/profile
#添加坏境变量 加到文件末尾
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
查看java 版本
[root@localhost opt]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
刷新/etc/profile配置文件
[root@localhost opt]# source /etc/profile
[root@localhost opt]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
解压
[root@localhost opt]# tar zxf apache-tomcat-9.0.16.tar.gz
[root@localhost opt]# mv apache-tomcat-9.0.16 /usr/local/tomcat
[root@localhost opt]# cd /usr/local/tomcat/
动静分离配置
###Tomcat1
[root@localhost tomcat]# cd webapps/
[root@localhost webapps]# mkdir test
[root@localhost webapps]# vim test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page </title>
</head>
<body>
<% out.println("动态页面1,http://www.test1.com");%>
</body>
</html>
[root@localhost webapps]# cd ..
[root@localhost tomcat]# vim conf/server.xml
###将文件末尾原来的<host
......
</host>删掉
然后插入下面这段:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true" xmlValidation="false"
xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test"
path="" reloadable="true" />
</Host>
[root@localhost tomcat]# /usr/local/tomcat/bin/startup.sh
[root@localhost tomcat]# ss -ntap|grep 8080
LISTEN 0 100 :::8080 :::* users:(("java",pid=8205,fd=54))
在浏览器上验证
http://192.168.111.50:8080/
###Tomcat2
#关闭防火墙及selinux
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# cd /opt
#上传安装包
apache-tomcat-9.0.16.tar.gz jdk-8u201-linux-x64.rpm
安装jdk-8u201-linux-x64.rpm
[root@localhost opt]# rpm -ivh jdk-8u201-linux-x64.rpm
[root@localhost opt]# vim /etc/profile
##在文件末尾 添加坏境变量
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
##刷新
[root@localhost opt]# source /etc/profile
##查看java版本
[root@localhost opt]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
##解压
[root@localhost opt]# tar zxf apache-tomcat-9.0.16.tar.gz
[root@localhost opt]# mv apache-tomcat-9.0.16 /usr/local/tomcat
[root@localhost opt]# cd /usr/local/tomcat/
[root@localhost tomcat]# cd webapps/
[root@localhost webapps]# mkdir test
[root@localhost webapps]# vim test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page </title>
</head>
<body>
<% out.println("动态页面2,http://www.test2.com");%>
</body>
</html>
[root@localhost webapps]# vim ../conf/server.xml
###将文件末尾原来的<host
......
</host>删掉
然后插入下面这段:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true" xmlValidation="false"
xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test"
path="" reloadable="true" />
</Host>
##开启startup.sh
[root@localhost webapps]# /usr/local/tomcat/bin/startup.sh
[root@localhost webapps]# ss -ntap|grep 8080
LISTEN 0 100 :::8080 :::* users:(("java",pid=8515,fd=54))
在浏览器上验证
http://192.168.111.51:8080/
五、最后再次进行nginx 配置
准备静态页面和图片
[root@localhost nginx]
[root@localhost html]
[root@localhost html]
[root@localhost test]
this is test web !!!
拖入一张图片
[root@localhost test]
[root@localhost nginx]
upstream tomcat_server {
server 192.168.91.101:8080 weight=1;
server 192.168.91.103:8080 weight=1;
}
server {
listen 80;
server_name www.kgc.com
location ~ .*\.jsp$ {
proxy_pass http://tomcat_server;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* .*\.(gif|jpg|jpeg|png|bmp|html)$ {
root /usr/local/nginx/html/test;
}
location / {
root html;
index index.html index.htm; }
}
[root@localhost nginx]
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 nginx]
http://192.168.111.52/1.jpg //后缀1.jpg是托进去的照片名字
http://192.168.111.52/test.html
http://192.168.111.52/index.jsp
|