操作系统:Centos7.9 
安装介质下载安装涉及到的介质和配置
说明:Tomcat9集群负载使用Nginxsession同步使用Redis,使用Tomcat自带FarmWarDeployer功能实现应用部署到一个服务器上,集群就会将部署分发到整个集群各个节点中,本文描述全部由亲自试验所得,部分源码借鉴各位无私奉献的大佬们,本文仅供参考


不多说直接开整

节点名称 节点IP
tomcatnode1 10.11.10.100
tomcatnode2 10.11.10.200

tomcatproxy_redis

10.11.10.99

一、安装介质 & 安装位置

apachetomcat-9.0.73.tar.gz  (tomcatnode1tomcatnode2
jdk-8u202-linuxx64.tar.gz  (tomcatnode1tomcatnode2
nginx-1.20.1.tar.gz  (tomcatproxy_redis
redis-6.2.5.tar.gz   (tomcatproxy_redis

tomcatredissessionmanagermaster-2.0.0.jar  (tomcat-node1tomcat-node2
commonspool2-2.3.jar  (tomcat-node1tomcat-node2
jedis-2.7.3.jar  (tomcat-node1tomcat-node2


二、安装jdk & 配置

tomcat-node1tomcat-node2节点都要配置

su – tomcat
tar -zxvf jdk-8u202-linux-x64.tar.gz
vi /home/tomcat/.bash_profile

export JAVA_HOME=/home/tomcat/jdk1.8.0_202
PATH=$JAVA_HOME/bin:$PATH:$HOME/.local/bin:$HOME/bin
export PATH

三、安装nginx & 配置

只需tomcat-proxy_redis节点配置

suroot
yumy install gcc gcc-c++ make automake autoconf pcre pcredevel zlib zlibdevel openssl openssldevel  gddevel

su – tomcat
tar -zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
 ./configureprefix=/home/tomcat/nginx
    —user=tomcat
    —group=tomcat 
    —withhttp_stub_status_module
    —withhttp_gzip_static_module
    —withhttp_flv_module
    —withhttp_ssl_module
    —withhttp_mp4_module
    —withstream

make && make install

vi /home/tomcat/nginx/conf/nginx.conf

http {
..
    include /home/tomcat/nginx/conf/conf.d/*.conf;
..
}

vi  /home/tomcat/nginx/conf/tomcat.conf

upstream tomcat {
   #ip_hash;
   #server 10.11.10.100:8080 weight=1 max_fails=3 fail_timeout=20s;
   #server 10.11.10.200:8081 weight=1 max_fails=3 fail_timeout=20s;
   #注意个节点端口不能重复
   server 10.11.10.100:8080;
   server 10.11.10.200:8081;
}
server {
    listen       8011;
    server_name  localhost;
    location / {
        proxy_redirect          off;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        client_max_body_size      20m;
        client_body_buffer_size 128k;
        proxy_connect_timeout   600;
        proxy_send_timeout      600;
        proxy_read_timeout      900;
        proxy_buffer_size       4k;
        proxy_buffers           4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
        proxy_pass http://tomcat;
    }
}

 四、安装redis & 配置

只需tomcat-proxy_redis节点配置

tar -zxvf redis-6.2.5.tar.gz
cd redis-6.2.5
make && make index PREFIX=/home/tomcat/redis
cp  /home/tomcat/redis-6.2.5/redis.conf  /home/tomcat/redis/redis.conf

mkdir /home/tomcat/redis/logs

vi   /home/tomcat/redis/redis.con

bind 0.0.0.0
daemonize yes     #默认redis不是在后台运行的,如果需要后台运行,把该项的值更改yes
requirepass redis_123456     #后续会用到
port 6379
pidfile /home/tomcat/redis/logs/6379.pid
logfile "/home/tomcat/redis/logs/6379.log"

vi  /home/tomcat/redis/run.sh    

#!/bin/sh
# redis 启动脚本
# /home/tomcat/redis/run.sh  start|stop|restart

REDISPORT=6379
EXEC=/home/tomcat/redis/bin/redis-server
CLIEXEC=/home/tomcat/redis/bin/redis-cli
PIDFILE=/home/tomcat/redis/redis_6379.pid
CONF="/home/tomcat/redis/redis.conf"
# 启动服务
start(){
    if [ -f $PIDFILE ]
    then
        echo "$PIDFILE exists, process is already running or crashed"
    else
        echo "Starting Redis server..."
        $EXEC $CONF
    fi
}
# 停止服务
stop(){
    if [ ! -f $PIDFILE ]
    then
        echo "$PIDFILE does not exist, process is not running"
    else
        PID=$(cat $PIDFILE)
        echo "Stopping ..."
        # 修改自己的redis密码
        $CLIEXEC -p $REDISPORT -a redis_123456 shutdown 2>/dev/null
        while [ -x /proc/${PID} ]
        do
            echo "Waiting for Redis to shutdown ..."
            sleep 1
        done
        echo "Redis stopped"
    fi
}
# 启动状态
status(){
    if [ -f $PIDFILE ]
    then
        PID=$(cat $PIDFILE)
        echo "$PIDFILE is runningpid=${PID}"
    else
        echo "$PIDFILE is not running"
    fi
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        stop
        sleep 1s
        start
        ;;
    *)
        echo "Please use start|stop|restart|status as first argument"
        ;;
esac

chmod 700  /home/tomcat/redis/run.sh


 五、安装tomcat & 配置

tomcat-node1tomcat-node2节点都需要配置

tar -zxvf apache-tomcat-9.0.73.tar.gz

vi /home/tomcat/apache-tomcat-9.0.73/bin/restart.sh

#!/bin/sh
# tocmat 重启脚本
# /home/tomcat/apache-tomcat-9.0.73/bin/restart.sh

bin=$(cd `dirname $`; pwd)
pid=$(ps aux | grep tomcat | grep -v grep | grep -v restart | grep ${bin} | awk '{print $2}')

if [ -n "${pid}" ]; then
    echo "Shutdown..."
    sh ${bin}/shutdown.sh
    sleep

    pid=$(ps aux | grep tomcat | grep -v grep | grep -v restart | grep ${bin} | awk '{print $2}')
    if [ -n "${pid}" ]; then
        kill - ${pid}
        sleep
    fi
fi

echo "Startup..."
sh ${bin}/startup.sh
if [ "$1" = "-v" ]; then
    tail -f -n  ${bin}/../logs/catalina.out
fi

chmoe 700  /home/tomcat/apache-tomcat-9.0.73/bin/restart.sh

vi /home/tomcat/apache-tomcat-9.0.73/conf/context.xml

<Context>
..
 <!--连接redis服务两个节点都连同一个redis-->
 <Valve className="com.naritech.nicole.gump.RedisSessionHandlerValve" />
   <Manager className="com.naritech.nicole.gump.RedisSessionManager"
     host="10.11.10.99"
     password="redis_123456"
     port="6379"
     database="2"
     maxInactiveInterval="60"
     />
..
</Context>

vi /home/tomcat/apache-tomcat-9.0.73/conf/server.xml 

<!--注意端口不能冲突-->
<Connector port="8080" protocol="HTTP/1.1"   
      connectionTimeout="20000"
      redirectPort="8443" />
<Host name="localhost"  appBase="webapps"
     unpackWARs="true" autoDeploy="true">
..
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
         <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"/>
         <!--改成自己节点ip-->
         <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
             address="10.11.10.100"   
             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>
          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" 
            tempDir="/home/tomcat/upload/temp"
            deployDir="/home/tomcat/apache-tomcat-9.0.73/webapps"
            watchDir="/home/tomcat/upload/listen"
            watchEnabled="true"/>
        <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
     </Cluster>
..
 </Host>

将 tomcat-redis-session-manager-master-2.0.0.jar、commonspool2-2.3.jar、jedis-2.7.3.jar上传至/home/tomcat/apache-tomcat-9.0.73/lib

 shutdown 无法正常关闭解决方案

1.脚本开头增加pid文件

vi /home/tomcat/apache-tomcat-9.0.73/bin/catalina.sh

 CATALINA_PID=/home/tomcat/apache-tomcat-9.0.73/tomcat.pid       ##该路径pid文件保存绝对路径

 2.脚本结尾修改shutdown配置

vi /home/tomcat/apache-tomcat-9.0.73/bin/shutdown.sh

exec "$PRGDIR"/"$EXECUTABLE" stop -force "$@"

六、测试session同步

tomcat-node1节点配置测试页面

vi  /home/tomcat/apache-tomcat-9.0.73/webapps/ROOT/index.jsp

<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="yellow">TomcatA </font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("abc","abc"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>

tomcat-node2节点配置测试页面

vi  /home/tomcat/apache-tomcat-9.0.73/webapps/ROOT/index.jsp

<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="red">TomcatB</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("abc","abc"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>

启动redis、nginx、2个节点tomcat服务访问 http://10.11.10.99:8011  不断刷新浏览器测试session同步


七、免密互信

tomcat-node1tomcat-node2节点都要做ssh免密互信

1、sshkeygen

[tomcat@tomcat-node1 ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tomcat/.ssh/id_rsa):
Created directory '/home/tomcat/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/tomcat/.ssh/id_rsa.
Your public key has been saved in /home/tomcat/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:o2Yq2XJnrkZGUiMFVJN5Lb70HYjMkD3K+rm+K5QZgeI tomcat@tomcat-node1
The key's randomart image is:
+---[RSA 2048]----+
| oo++= .         |
|o o B.= .        |
|o  = O + .       |
| Eo + * . .      |
|   B . oS. .     |
|  = o .....      |
| . * .+          |
|  = *+o          |
|   BBO.          |
+----[SHA256]-----+
[tomcat@tomcat-node2 ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tomcat/.ssh/id_rsa):
Created directory '/home/tomcat/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/tomcat/.ssh/id_rsa.
Your public key has been saved in /home/tomcat/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:kvO6C9fKsW8qoWLwvYT4iQ/ipTx8F9QhLP5OTKwzZFs tomcat@tomcat-node2
The key's randomart image is:
+---[RSA 2048]----+
|    .            |
|   . o .         |
|  . o o .        |
|   + E o         |
|  o O + S        |
|.. *.= =         |
|*..+B.+ o        |
|=B=+o* =.        |
|o** ooO*.        |
+----[SHA256]-----+

 2、ssh-copy-id   节点IP

[tomcat@tomcat-node1 ~]$  ssh-copy-id 10.11.10.100
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/tomcat/.ssh/id_rsa.pub"
The authenticity of host '10.11.10.100 (10.11.10.100)' can't be established.
ECDSA key fingerprint is SHA256:Rze7+FiebmPXyTncbJNikc8FHFn7axO6d1B3kkv8J18.
ECDSA key fingerprint is MD5:60:03:72:b6:55:77:ac:44:34:82:09:58:7e:98:2d:11.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
tomcat@10.11.10.100's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.11.10.100'"
and check to make sure that only the key(s) you wanted were added.

-------------------------------------------------------------------------------------

[tomcat@tomcat-node1 ~]$  ssh-copy-id 10.11.10.200
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/tomcat/.ssh/id_rsa.pub"
The authenticity of host '10.11.10.200 (10.11.10.200)' can't be established.
ECDSA key fingerprint is SHA256:Rze7+FiebmPXyTncbJNikc8FHFn7axO6d1B3kkv8J18.
ECDSA key fingerprint is MD5:60:03:72:b6:55:77:ac:44:34:82:09:58:7e:98:2d:11.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
tomcat@10.11.10.200's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.11.10.200'"
and check to make sure that only the key(s) you wanted were added.
[tomcat@tomcat-node2 ~]$ ssh-copy-id 10.11.10.100
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/tomcat/.ssh/id_rsa.pub"
The authenticity of host '10.11.10.100 (10.11.10.100)' can't be established.
ECDSA key fingerprint is SHA256:Rze7+FiebmPXyTncbJNikc8FHFn7axO6d1B3kkv8J18.
ECDSA key fingerprint is MD5:34:03:72:34:55:7e:ac:44:34:82:09:0b:7e:98:2d:17.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
tomcat@10.11.10.100's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.11.10.100'"
and check to make sure that only the key(s) you wanted were added.

---------------------------------------------------------------------------------------

[tomcat@tomcat-node2 ~]$ ssh-copy-id 10.11.10.200
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/tomcat/.ssh/id_rsa.pub"
The authenticity of host '10.11.10.200 (10.11.10.200)' can't be established.
ECDSA key fingerprint is SHA256:Rze7+FiebmPXyTncbJNikc8FHFn7axO6d1B3kkv8J18.
ECDSA key fingerprint is MD5:34:03:72:34:55:7e:ac:44:34:82:09:0b:7e:98:2d:17.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
tomcat@10.11.10.200's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.11.10.200'"
and check to make sure that only the key(s) you wanted were added.

八、测试部署war分发各个节点

tomcat-node1tomcat-node2节点都创建集群部署文件

mkdir -vp /home/tomcat/upload/{temp/,listen/} 

测试各节点程序包同步

原文地址:https://blog.csdn.net/sinat_21221041/article/details/129747140

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_35194.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注