·最初是由Sun的软件架构师詹姆斯.邓肯。戴维森开发
·安装Tomcat后,安装路径下面的目录和文件,是使用或者配置Tomcat的重要文件
bin: 存放启动和关闭Tomcat脚本
conf :存放Tomcat不同的配置文件
doc :存放Tomcat文档
lib :存放Tomcat运行需要的库文件
logs :存放Tomcat执行时的LOG文件
src :存放Tomcat的源代码
webapps: Tomcat的主要Web发布目录
work :存放jsp编译后产生的class文件
1、Nginx实现负载均衡是通过反向代理实现
2、Nginx配置反向代理的主要参数
(1)upstream 服务池名{}
配置后端服务器池,以提供响应数据
(2)proxy_pass http://服务池名
配置将访问请求转发给后端服务器池的服务器处理
动静分离原理
服务端接收来自客户端的请求中,既有静态资源也有动态资源,静态资源由Nginx提供服务,动态资源Nginx转发至后端
Nginx静态处理优势
1、Nginx处理静态页面的效率远高于Tomcat的处理能力
2、若Tomcat的请求量为1000次,则Nginx的请求量为6000次
3、Tomcat每秒的吞吐量为0.6M,Nginx的每秒吞吐量为3.6M
4、Nginx处理静态资源的能力是Tomcat处理的6倍
部署nginx+tomcat负载均衡、动静分离
nginx1:192.168.188.10
nginx2:192.168.188.20
tomcat1:192.168.188.30
tomcat2:192.168.188.40
nfs:192.168.188.50
客户机:192.168.188.60
注:一定要把防火墙和核心防护关掉
nfs:
[root@nfs ~]#mkdir /web1
[root@nfs ~]#mkdir /web2
[root@nfs ~]#vi /web1/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>tomcat 1</head>
<body>
<% out.println("this is tomcat1!!!");%>
<div>动态页面</div><br/><img src="a.jpg" />
</body>
</html>
[root@nfs ~]#vi /web2/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>tomcat 2</head>
<body>
<% out.println("this is tomcat2!!!");%>
<div>动态页面</div><br/><img src="b.jpg" />
</body>
</html>
[root@nfs ~]#
[root@nfs web1]#ls //把a.jpg放入web1的目录中
a.jpg index.jsp
[root@nfs ~]#cd /web2
[root@nfs web2]#ls //把b.jpg放入web2的目录中
b.jpg index.jsp
[root@nfs ~]#vi /etc/exports
/web1 192.168.188.30(ro)
/web2 192.168.188.40(ro)
[root@nfs ~]#systemctl start nfs
[root@nfs ~]#systemctl restart rpcbind
[root@nfs ~]#showmount -e
Export list for nfs:
/web2 192.168.188.40
/web1 192.168.188.30
tomcat1:
把jdk-8u91-linux-x64.tar.gz放入root目录下
[root@tomcat1 ~]#tar zxvf jdk-8u91-linux-x64.tar.gz
[root@tomcat1 ~]#mv jdk1.8.0_91/ /usr/local/java
[root@tomcat1 ~]#vi /etc/profile
##末尾添加
export JAVA_HOME=/usr/local/java ##定义变量,设置JAVA根目录设为全局变量
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre ##环境变量,在PATH环境变量中添加JAVA根目录下的bin/子目录,jre/子目录
[root@tomcat1 ~]#source /etc/profile
[root@tomcat1 ~]#echo $PATH //输出环境信息
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin:/usr/local/java/jre
[root@tomcat1 ~]#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)
[root@tomcat1 ~]#vi abc.java
[root@tomcat1 ~]#javac abc.java //编写脚本
[root@tomcat1 ~]#java abc //执行显示
hello world!!!
安装tomcat1
[root@tomcat1 ~]#tar zxvf apache-tomcat-8.5.16.tar.gz
[root@tomcat1 ~]#mv apache-tomcat-8.5.16/ /usr/local/tomcat8
创建站点目录
[root@tomcat1 ~]#mkdir /webapp1
[root@tomcat1 ~]#cd /webapp1/
[root@tomcat1 webapp1]#ls
a.jpg b.jpg
[root@tomcat1 ~]#mount 192.168.188.50:/web1 /webapp1
[root@tomcat1 ~]#df -Th
[root@tomcat1 ~]#vi /usr/local/tomcat8/conf/server.xml
<Context docBase="/webapp1" path="" reloadable="false">
</Context>
[root@tomcat1 ~]#ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[root@tomcat1 ~]#ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@tomcat1 ~]#tomcatup //启动tomcat
Using CATALINA_BASE: /usr/local/tomcat8
Using CATALINA_HOME: /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat1 ~]#netstat -anpt | grep 8080 //查看8080是否启动
tcp6 0 0 :::8080 :::* LISTEN 42877/java
[root@tomcat1 ~]#
客户机
打开浏览器输入http://192.168.188.30:8080/
tomcat2
[root@tomcat2 ~]#tar zxvf jdk-8u91-linux-x64.tar.gz
[root@tomcat2 ~]#mv jdk1.8.0_91/ /usr/local/java
[root@tomcat2 ~]#vi /etc/profile
末尾添加
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre
[root@tomcat2 ~]#source /etc/profile
[root@tomcat2 ~]#echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin:/usr/local/java/jre
[root@tomcat2 ~]#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)
[root@tomcat2 ~]#vi abc.java
public class abc {
public static void main (String[] args) {
System.out.println("hello world!!!");
}
}
[root@tomcat2 ~]#javac abc.java
[root@tomcat2 ~]#java abc
hello world!!!
[root@tomcat2 ~]#tar zxvf apache-tomcat-8.5.16.tar.gz
[root@tomcat2 ~]#mv apache-tomcat-8.5.16/ /usr/local/tomcat8
[root@tomcat2 ~]#mkdir /webapp2
[root@tomcat2 ~]#cd /webapp2/
[root@tomcat2 webapp2]#ls //添加图片
a.jpg b.jpg
[root@tomcat2 webapp2]#cd
[root@tomcat2 ~]#mount 192.168.188.50:/web2 /webapp2
[root@tomcat2 ~]#df -Th
[root@tomcat2 ~]#vi /usr/local/tomcat8/conf/server.xml
<Context docBase="/webapp2" path="" reloadable="false">
</Context>
[root@server4 webapp2]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[root@server4 webapp2]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@server4 webapp2]# tomcatup
Using CATALINA_BASE: /usr/local/tomcat8
Using CATALINA_HOME: /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
[root@server4 webapp2]# netstat -anpt | grep 8080
[root@nginx1 ~]#yum -y install gcc gcc-c++ make pcre-devel zlib-devel
pcre-devel:正则表达式库
zlib-devel:函数库
[root@nginx1 ~]#useradd -M -s /sbin/nologin nginx //创建不可登录,不带宿主的用户
[root@nginx1 ~]#tar zxvf nginx-1.12.2.tar.gz
[root@nginx1 ~]#cd nginx-1.12.2/
[root@nginx1 nginx-1.12.2]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx //安装路径,属主,属组设置
[root@nginx1 nginx-1.12.2]#make && make install //编译安装
[root@nginx1 nginx-1.12.2]#ln -s /usr/local/nginx/conf/nginx.conf /etc/ //创建设置链接
[root@nginx1 nginx-1.12.2]#ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ //创建命令链接(nginx命令使用前提)
[root@nginx1 nginx-1.12.2]#cd
[root@nginx1 ~]#vi /usr/local/nginx/conf/nginx.conf
upstream tomcat_server {
server 192.168.188.30:8080 weight=1;
server 192.168.188.40:8080 weight=1;
}
location ~ \.jsp$ {
proxy_pass http://tomcat_server;
}
[root@nginx1 ~]#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@nginx1 ~]#nginx
[root@nginx1 ~]#netstat -anpt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 58978/nginx: master
[root@nginx1 ~]#cd /usr/local/nginx/html/ //在这里面添加图片
[root@nginx1 html]#ls
50x.html index.html
[root@nginx1 html]#ls
50x.html a.jpg b.jpg index.html
[root@nginx1 html]#
nginx2(调度器)
[root@nginx2 ~]#yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@nginx2 ~]#useradd -M -s /sbin/nologin nginx //创建不可登录,不带宿主的用户
[root@nginx2 ~]#tar zxvf nginx-1.12.2.tar.gz
[root@nginx2 ~]#cd nginx-1.12.2/
[root@nginx2 nginx-1.12.2]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx //安装路径,属主,属组设置
[root@nginx2 nginx-1.12.2]#make && make install //编译安装
[root@nginx2 nginx-1.12.2]#ln -s /usr/local/nginx/conf/nginx.conf /etc/ //创建设置链接
[root@nginx2 nginx-1.12.2]#ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ //创建命令链接(nginx命令使用前提)
[root@nginx2 nginx-1.12.2]#cd
[root@nginx2 ~]#vi /usr/local/nginx/conf/nginx.conf
upstream tomcat_server {
server 192.168.188.30:8080 weight=1;
server 192.168.188.40:8080 weight=1;
}
location ~ \.jsp$ {
proxy_pass http://tomcat_server;
}
[root@nginx2 ~]#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@nginx2 ~]#nginx
[root@nginx2 ~]#netstat -anpt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 43519/nginx: master
[root@nginx2 ~]#cd /usr/local/nginx/html/ //在里面添加图片
[root@nginx2 html]#ls
50x.html index.html
[root@nginx2 html]#ls
50x.html a.jpg b.jpg index.html
客户机
http://192.168.188.10/
http://192.168.188.10/index.jsp
http://192.168.188.20/
http://192.168.188.20/index.jsp
nginx1
安装keepalive
[root@nginx1 html]#yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
[root@nginx1 html]#tar zxvf keepalived-2.0.13.tar.gz
[root@nginx1 html]#cd keepalived-2.0.13/
[root@nginx1 keepalived-2.0.13]#./configure --prefix=/
[root@nginx1 keepalived-2.0.13]#make && make install
[root@nginx1 keepalived-2.0.13]#cp keepalived/etc/init.d/keepalived /etc/init.d
[root@nginx1 keepalived-2.0.13]#vi /etc/init.d/keepalived
[root@nginx1 ~]#chmod 755 /etc/init.d
[root@nginx1 ~]#chkconfig --add keepalived
[root@nginx1 ~]#chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@nginx1 ~]#
[root@nginx1 ~]#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id nginx1 ##本服务器的名称
}
vrrp_script chk_http_port {
## vrrp脚本
interval 2 ## 检测间隔时间 单位秒
weight 2
}
! Configuration File for keepalived
global_defs {
router_id nginx1 ##本服务器的名称
}
vrrp_script chk_http_port {
## vrrp脚本
interval 2 ## 检测间隔时间 单位秒
weight 2
}
vrrp_instance vi_1 {
##实例名称
state MASTER ###指定keepalived的角色,MASTER为主,BACKUP为备
interface ens33 ##当前进行vrrp通讯的网络接口卡
virtual_router_id 51 ##虚拟路由编号,主从要一致
priority 110 ##优先级,数值越大,获取地址优先级越大
adver_int 1 ##检查间隔,默认为1s (vrrp组播周期秒数)
authentication {
##认证方式
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ##调用检测脚本
}
virtual_ipaddress {
192.168.188.200 ##定义虚拟ip(VIP)
}
}
[root@nginx1 ~]#vi /usr/local/src/nginx.sh
#!/bin/bash
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ]
then /usr/local/nginx/sbin/nginx
if [ `ps -C nginx --no-header | wc -l` -eq 0 ]
then exit 1
else exit 0
fi
else
exit 0
fi
[root@nginx1 ~]#chmod +x /usr/local/src/nginx.sh
[root@nginx1 ~]#systemctl restart keepalived.service
[root@nginx1 ~]#ip addr
[root@nginx1 ~]#systemctl status keepalived.service
nginx2
安装keepalivr
[root@nginx2 html]#yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
[root@nginx2 html]#tar zxvf keepalived-2.0.13.tar.gz
[root@nginx2 html]#cd keepalived-2.0.13/
[root@nginx2 keepalived-2.0.13]#./configure --prefix=/
[root@nginx2 keepalived-2.0.13]#make && make install
[root@nginx2 keepalived-2.0.13]#cp keepalived/etc/init.d/keepalived /etc/init.d
[root@nginx2 keepalived-2.0.13]#vi /etc/init.d/keepalived
修改
# chkconfig: 35 21 79
[root@nginx2 keepalived-2.0.13]#chmod 755 /etc/init.d
[root@nginx2 keepalived-2.0.13]#chkconfig --add keepalived
[root@nginx2 keepalived-2.0.13]#chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@nginx2 keepalived-2.0.13]#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id nginx2 ##本服务器的名称
}
! Configuration File for keepalived
global_defs {
router_id nginx2 ##本服务器的名称
}
vrrp_script chk_http_port {
## vrrp脚本
interval 2 ## 检测间隔时间 单位秒
weight 2
}
! Configuration File for keepalived
global_defs {
router_id nginx1 ##本服务器的名称
}
vrrp_script chk_http_port {
## vrrp脚本
interval 2 ## 检测间隔时间 单位秒
weight 2
}
vrrp_instance vi_1 {
##实例名称
state BACKUP ###指定keepalived的角色,MASTER为主,BACKUP为备
interface ens33 ##当前进行vrrp通讯的网络接口卡
virtual_router_id 51 ##虚拟路由编号,主从要一致
priority 105 ##优先级,数值越大,获取地址优先级越大
adver_int 1 ##检查间隔,默认为1s (vrrp组播周期秒数)
authentication {
##认证方式
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ##调用检测脚本
}
virtual_ipaddress {
192.168.188.200 ##定义虚拟ip(VIP)
}
}
[root@nginx2 keepalived-2.0.13]#vi /usr/local/src/nginx.sh
#!/bin/bash
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ]
then /usr/local/nginx/sbin/nginx
if [ `ps -C nginx --no-header | wc -l` -eq 0 ]
then exit 1
else exit 0
fi
else
exit 0
fi
[root@nginx2 keepalived-2.0.13]#chmod +x /usr/local/src/nginx.sh
[root@nginx2 keepalived-2.0.13]#systemctl restart keepalived.service
[root@nginx2 keepalived-2.0.13]#ip addr
[root@nginx2 keepalived-2.0.13]#systemctl status keepalived.service
客户机
http://192.168.188.200/
http://192.168.188.200/index.jsp
断掉nginx1调度器
[root@nginx1 ~]#systemctl stop keepalived.service
[root@nginx1 ~]#ip addr //查看下hi否有虚拟地址
[root@nginx2 ~]#ip addr //虚拟地址到nginx2调度器上了