安装环境
Tomcat编译安装环境
L:Linux(centos 7.6) http://mirrors.cqu.edu.cn/CentOS/7.6.1810/isos/x86_64/
N:Nginx(1.16.1)
T: Tomcat(8.5.53)
主机信息:
部署规划:
172.24.77.242(sr1.dj.com):运行Tomcat
-
关闭防火墙及selinux
vim /etc/selinux/config SELINUX=disabled systemctl stop firewallda systemctl disable firewlld
-
安装JDK
yum install jdk-8u241-linux-x64.rpm y
-
修改环境变量
vim /etc/profile.d/java.sh export JAVA_HOME=/usr/java/latest export PATH=$JAVA_HOME/bin:$PATH
-
重读环境变量
. /etc/profile.d/java.sh
-
检查版本及变量
java -version echo $JAVA_HOME
-
安装Tomcat
-
YUM安装
yum install tomcat tomcat-admin-webapps tomcat-webapps
-
编译安装
tar xf apache-tomcat-8.5.53.tar.gz -C /usr/local/ cd /usr/local/ ln -sv apache-tomcat-8.5.53 tomcat
-
-
配置tomcat环境变量
vim /etc/profile.d/tomcat.sh export CATALINA_HOME=/usr/local/tomcat export PATH=$CATALINA_HOME/bin:$PATH
-
重读环境变量
. /etc/profile.d/tomcat.sh catalina.sh version
-
测试配置文件
catalina.sh configtest
-
启动tomcat引擎catalina
catalina.sh start
-
查看监控状态
ss -tnl
-
测试访问
http://172.24.77.242
-
useradd -r java 建立系统账号
useradd -r java chown -R java.java ./* su - java -c '/usr/local/tomcat/bin/catalina.sh start' ps -aux | grep tomcat
-
手动添加一个测试应用程序
1) 创建webapp特有的目录结构
cd /usr/local/tomcat/webapps/ mkdir myapp/{lib,classes,WEB-INF,META-INF} -pv tree myapp/
-
创建首页
vim myapp/index.jsp <%@ page language="java" %> <%@ page import="java.util.*" %>
JSP Test Page <% out.println("Hello,world");%> -
重启catalina
catalina.sh stop catalina.sh start
-
测试访问
http://IP:8080/myapp
Cluster配置
方法一:Nginx安装
安装epel
yum install -y epel-release
编译安装nginx
yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed cd /usr/local/src/ wget https://nginx.org/download/nginx-1.16.1.tar.gz tar xvf nginx-1.16.1.tar.gz cd nginx-1.16.1
./configure --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module
make && make install
useradd nginx -s /sbin/nologin -u 2000 /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx -V
创建Nginx自启动脚本
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
验证Nginx自启动脚本
systemctl daemon-reload
systemctl start nginx
systemctl status nginx
systemctl enable nginx
配置nginx集群
cd /usr/local/nginx/conf/
cp nginx.conf{,bak}
vim nginx.conf
upstream tcsrvs {
server 172.24.77.241:8080;
server 172.24.77.242:8080;
}
指定反向代理
location ~* \.(jsp|do)$ {
proxy_pass http://tcsrvs;
}
启动nginx
systemctl reload nginx
修改后端两台tomcat配置server.xml
vim /usr/local/tomcat/conf/server.xml
重启tomcat服务
catalina.sh stop
catalina.sh start
测试访问群集IP
http://IP/myapp/index.jsp
方法二:httpd群集
yum install -y httpd
vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"
创建虚拟主机并配置反向代理
cd /etc/httpd/conf.d/
vim vhost.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
#ServerName可用域名
ServerName 172.24.77.243
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
ProxyPass / balancer://lbtomcats/
ProxyPa***everse / balancer://lbtomcats/
BalancerMember http://nodea.djk.com:8080 loadfactor=1 route=tomcat1-241
BalancerMember http://nodeb.djk.com:8080 loadfactor=2 route=tomcat2-242
ProxySet stickysession=ROUTEID
配置所有后端Tomcat
vim /usr/local/tomcat/conf/server.xml
修改host文件
vim /etc/hosts
172.24.77.241 nodea.djk.com
172.24.77.242 nodeb.djk.com
Session保持-Tomcat集群+共享会话
<%@ page import="java.util.*" %>
session-test
On <%=request.getServerName() %>
<%=request.getLocalAddr() + ":" + request.getLocalPort() %>
SessionID = <%=session.getId() %>
<%=new Date()%>
虚拟主机1配置
vim /usr/local/tomcat/conf/server.xml
配置集群,放入指定的host
http://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html
vim /usr/local/tomcat/conf/server.xml
shutdown.sh
catalina.sh configtest
虚拟主机2配置,放入指定的host
vim /usr/local/tomcat/conf/server.xml
vim /usr/local/tomcat/conf/server.xml
vim /usr/local/tomcat/conf/server.xml
shutdown.sh
catalina.sh configtest
确保tomcat的时间一致
配置项目中的web.xml文件,加入
vim /usr/local/tomcat/conf/web.xml
index.html
index.htm
index.jsp
cp /usr/local/tomcat/conf/web.xml /usr/local/tomcat/webapps/ROOT/WEB-INF/
重启tomcat服务
catalina.sh configtest
shutdown.sh
startup.sh
模拟Tomat故障,查看session是否一致
On 172.24.77.243
172.24.77.241:8080
SessionID = 8761F8B94376E728E6DEAF0636048249.tomcat1-241
Mon Jul 20 11:28:34 CST 2020
ssh 172.24.77.241
shutdown.sh
再次访问httpd-VIP
On 172.24.77.243
172.24.77.242:8080
SessionID = 8761F8B94376E728E6DEAF0636048249.tomcat2-242
Mon Jul 20 19:29:34 CST 2020
再次访问nginx-VIP
http://172.24.77.243/index.jsp