安装环境

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

  1. 关闭防火墙及selinux

    vim /etc/selinux/config
    SELINUX=disabled
    systemctl stop firewallda
    systemctl disable firewlld
    
  2. 安装JDK

    yum install jdk-8u241-linux-x64.rpm
    y
    
  3. 修改环境变量

 ```
 vim /etc/profile.d/java.sh
 export JAVA_HOME=/usr/java/latest
 export PATH=$JAVA_HOME/bin:$PATH
 ```
  1. 重读环境变量
 ```
 . /etc/profile.d/java.sh
 ```
  1. 检查版本及变量
 ```
 java -version
 echo $JAVA_HOME
 ```
  1. 安装Tomcat

    1. YUM安装

      yum install tomcat tomcat-admin-webapps tomcat-webapps
      
    2. 编译安装

    tar xf apache-tomcat-8.5.53.tar.gz -C /usr/local/
    cd /usr/local/
    ln -sv apache-tomcat-8.5.53 tomcat
    
  2. 配置tomcat环境变量

 ```
 vim /etc/profile.d/tomcat.sh
 export CATALINA_HOME=/usr/local/tomcat
 export PATH=$CATALINA_HOME/bin:$PATH
 ```
  1. 重读环境变量
 ```
 . /etc/profile.d/tomcat.sh
 catalina.sh version
 ```
  1. 测试配置文件

    catalina.sh configtest
    
  2. 启动tomcat引擎catalina

 ```
 catalina.sh start
 ```
  1. 查看监控状态
 ```
 ss -tnl
 ```
  1. 测试访问

    http://172.24.77.242
    
  2. useradd -r java 建立系统账号

    useradd -r java
    chown -R java.java ./*
    su - java -c '/usr/local/tomcat/bin/catalina.sh start'
    ps -aux | grep tomcat
    
  3. 手动添加一个测试应用程序

    1. 创建webapp特有的目录结构

cd /usr/local/tomcat/webapps/ mkdir myapp/{lib,classes,WEB-INF,META-INF} -pv tree myapp/ ```

  1. 创建首页

vim myapp/index.jsp <%@ page language="java" %> <%@ page import="java.util.*" %> <html> <head> <title>JSP Test Page</title> </head> <body> <% out.println("Hello,world");%> </body> </html> ```

  1. 重启catalina

    catalina.sh stop
    catalina.sh start
    
  2. 测试访问

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
<Engine name="Catalina" defaultHost="hostname1">

重启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
<VirtualHost *:80>
  #ServerName可用域名
  ServerName 172.24.77.243
  ProxyRequests     Off
  ProxyVia          On
  ProxyPreserveHost On
  ProxyPass        / balancer://lbtomcats/
  ProxyPassReverse / balancer://lbtomcats/
</VirtualHost>
<Proxy 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
</Proxy>

配置所有后端Tomcat

vim /usr/local/tomcat/conf/server.xml
<Engine name="Catalina" defaultHost="nodea.djk.com" jvmRoute="tomcat1-241">
<Engine name="Catalina" defaultHost="nodeb.djk.com" jvmRoute="tomcat2-242">

修改host文件

vim /etc/hosts
172.24.77.241 nodea.djk.com
172.24.77.242 nodeb.djk.com

Session保持-Tomcat集群+共享会话

<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>session-test</title>
</head>
<body>
<div>On <%=request.getServerName() %></div>
<div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
<div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
<%=new Date()%>
</body>
</html>

虚拟主机1配置

vim /usr/local/tomcat/conf/server.xml
<Engine name="Catalina" defaultHost="nodea.djk.com">
<Host name="nodea.djk.com"  appBase="webapps"
cd /usr/local/tomcat/bin/
catalina.sh configtest

配置集群,放入指定的host

http://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html

vim /usr/local/tomcat/conf/server.xml
        <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="8">

          <Manager className="org.apache.catalina.ha.session.DeltaManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"/>

          <Channel className="org.apache.catalina.tribes.group.GroupChannel">
            <Membership className="org.apache.catalina.tribes.membership.McastService"
                        address="228.0.0.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="172.24.77.241"
                      port="4000"
                      autoBind="100"
                      selectorTimeout="5000"
                      maxThreads="6"/>

            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            </Sender>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/>
          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=""/>
          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                    tempDir="/tmp/war-temp/"
                    deployDir="/tmp/war-deploy/"
                    watchDir="/tmp/war-listen/"
                    watchEnabled="false"/>

          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>
shutdown.sh
catalina.sh configtest

虚拟主机2配置,放入指定的host

vim /usr/local/tomcat/conf/server.xml
vim /usr/local/tomcat/conf/server.xml
<Engine name="Catalina" defaultHost="nodeb.djk.com">
<Host name="nodeb.djk.com"  appBase="webapps"
cd /usr/local/tomcat/bin/
catalina.sh configtest
vim /usr/local/tomcat/conf/server.xml
        <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="8">

          <Manager className="org.apache.catalina.ha.session.DeltaManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"/>

          <Channel className="org.apache.catalina.tribes.group.GroupChannel">
            <Membership className="org.apache.catalina.tribes.membership.McastService"
                        address="228.0.0.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="172.24.77.242"
                      port="4000"
                      autoBind="100"
                      selectorTimeout="5000"
                      maxThreads="6"/>

            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            </Sender>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/>
          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=""/>
          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                    tempDir="/tmp/war-temp/"
                    deployDir="/tmp/war-deploy/"
                    watchDir="/tmp/war-listen/"
                    watchEnabled="false"/>

          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>
shutdown.sh
catalina.sh configtest

确保tomcat的时间一致


配置项目中的web.xml文件,加入<distributable/>

vim /usr/local/tomcat/conf/web.xml
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
        <distributable/>
</web-app>
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