一、环境准备
sudo yum install java-1.8.0-openjdk-devel,安装完成后通过java -version验证是否成功。/opt)。例如:wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.tar.gz,tar xvf apache-tomcat-9.0.56.tar.gz -C /opt,建议重命名为/opt/tomcat便于管理。/etc/profile文件,添加Tomcat环境变量:export CATALINA_HOME=/opt/tomcat、export PATH=$CATALINA_HOME/bin:$PATH,保存后执行source /etc/profile使配置生效。二、配置Tomcat集群
$CATALINA_HOME/conf目录,编辑server.xml文件,在<Engine>元素内添加<Cluster>配置,启用集群通信和会话复制。关键配置如下:<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster">
<!-- 会话管理器:DeltaManager支持主节点故障时自动切换 -->
<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="auto"
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=""/>
<!-- 部署器:支持集群内war包自动部署 -->
<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>
$CATALINA_HOME/conf/context.xml文件,在<Context>标签内添加distributable="true"属性,允许应用支持分布式会话:<Context distributable="true">
<!-- 其他配置 -->
</Context>
WEB-INF目录,编辑web.xml文件,在<web-app>标签内添加<distributable/>标签,声明该应用需要参与集群:<web-app>
<distributable/>
<!-- 其他配置 -->
</web-app>
三、配置负载均衡(Nginx为例)
sudo yum install epel-release(启用EPEL仓库),sudo yum install nginx,安装完成后启动Nginx并设置开机自启:sudo systemctl start nginx、sudo systemctl enable nginx。/etc/nginx/nginx.conf文件,在http块内添加upstream集群定义,指定Tomcat节点的IP和端口(如8080),然后配置server块将请求转发到集群:http {
upstream tomcat_cluster {
server 192.168.1.101:8080; # Tomcat节点1
server 192.168.1.102:8080; # Tomcat节点2
server 192.168.1.103:8080; # Tomcat节点3
}
server {
listen 80;
server_name yourdomain.com; # 替换为你的域名或IP
location / {
proxy_pass http://tomcat_cluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
保存后执行sudo nginx -t测试配置语法,无误后重启Nginx:sudo systemctl restart nginx。四、启动集群并验证
$CATALINA_HOME/bin目录,执行./startup.sh启动Tomcat。可通过ps -ef | grep tomcat确认进程是否运行,或访问http://节点IP:8080查看Tomcat默认页面。http://yourdomain.com),多次刷新页面,观察请求是否被分发到不同Tomcat节点(可通过Tomcat的/status页面查看当前访问节点)。