Tomcat在Linux上的集群部署方案
在开始部署前,需完成以下基础准备工作:
java -version命令检查是否已安装,未安装时使用sudo yum install java-11-openjdk-devel(CentOS)命令安装。/opt/tomcat),并通过export CATALINA_HOME="/opt/tomcat"(添加至/etc/profile)设置环境变量。ntpdate命令同步时间(如sudo ntpdate pool.ntp.org),避免因时间差异导致集群通信问题。每个Tomcat实例需修改核心配置文件以启用集群功能:
server.xml:在<Engine>元素内添加<Cluster>配置,定义集群通信参数(如组播地址、端口、接收器/发送器设置)。示例如下:<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster">
<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=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.css;.*\.txt"/>
<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>
关键参数说明:address为组播地址(集群节点需在同一网段),port为组播端口,frequency为心跳频率(毫秒)。web.xml:在需集群的应用的WEB-INF/web.xml中添加<distributable/>标签,告知Tomcat该应用需支持分布式部署(会话复制)。负载均衡器负责将客户端请求分发至集群中的Tomcat节点,提升系统吞吐量和可用性:
sudo yum install epel-release && sudo yum install nginx),启动服务并设置开机自启(sudo systemctl start nginx && sudo systemctl enable nginx)。/etc/nginx/nginx.conf),在http块中添加upstream定义,列出所有Tomcat节点的IP和端口:upstream tomcat_cluster {
server 192.168.1.101:8080;
server 192.168.1.102:8080;
server 192.168.1.103:8080;
}
server块中配置location,将请求转发至上游集群,并传递客户端真实IP(避免后端获取到Nginx的IP):server {
listen 80;
server_name yourdomain.com;
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;
}
}
为实现用户会话在集群中的共享,需选择以下一种会话同步方式:
server.xml的<Cluster>中完成(DeltaManager用于全量复制,BackupManager用于主备复制)。stickysession参数,将同一用户的请求始终路由至同一Tomcat节点。适用于对会话一致性要求不高但需减少跨节点通信的场景,Nginx配置示例如下:location / {
proxy_pass http://tomcat_cluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cookie_path / "/; HttpOnly; Secure";
sticky cookie JSESSIONID expires=1h domain=.yourdomain.com path=/;
}
tomcat-redis-session-manager),并配置context.xml:<Context>
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.1.100"
port="6379"
database="0"
maxInactiveInterval="1800"/>
</Context>
bin目录,执行./startup.sh启动Tomcat,通过ps -ef | grep tomcat确认进程运行状态。http://yourdomain.com),多次刷新页面,通过Tomcat管理页面(http://节点IP:8080/manager/status)查看当前活动的会话数,确认请求被分发至不同节点。server.xml中的channelSendOptions(如设置为8,启用异步复制)、maxThreads(接收器线程数,根据节点性能调整)等参数,减少网络延迟。jmx接口查看实时性能数据。