centos

centos下tomcat集群搭建方法

小樊
40
2025-10-11 03:15:26
栏目: 智能运维

CentOS下Tomcat集群搭建步骤

1. 环境准备

2. 配置Tomcat集群

2.1 修改server.xml(核心集群配置)

编辑/opt/tomcat/conf/server.xml,在<Engine>标签内添加<Cluster>配置,启用集群通信、会话管理及故障检测:

<Engine name="Catalina" defaultHost="localhost">
    <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.MessageDispatch15Interceptor"/>
        </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>
</Engine>

说明

2.2 配置context.xml(启用集群会话)

编辑/opt/tomcat/conf/context.xml,在<Context>标签内添加集群配置,确保会话数据同步:

<Context>
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
</Context>

说明:此配置使应用支持分布式会话,用户会话数据会在集群节点间同步。

2.3 部署应用(标记为可分布)

将应用打包为WAR文件,部署到每个Tomcat实例的webapps目录。需在应用的WEB-INF/web.xml中添加<distributable/>标签,声明应用支持集群部署:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
         version="3.1">
    <distributable/>
</web-app>

说明<distributable/>标签告知Tomcat该应用需启用集群特性(如会话复制)。

3. 配置负载均衡(Nginx为例)

Tomcat集群需通过负载均衡器分发请求,以下以Nginx为例:

3.1 安装Nginx

执行命令:

sudo yum install epel-release
sudo yum install nginx

安装完成后启动Nginx:sudo systemctl start nginx,并设置开机自启:sudo systemctl enable nginx

3.2 配置Nginx集群

编辑/etc/nginx/nginx.conf,在http块中添加upstream配置,定义Tomcat集群节点:

http {
    upstream tomcat_cluster {
        server 192.168.1.101:8080;  # Tomcat节点1 IP
        server 192.168.1.102:8080;  # Tomcat节点2 IP
        server 192.168.1.103:8080;  # Tomcat节点3 IP
    }
    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;
        }
    }
}

说明

3.3 启动Nginx

执行命令:sudo systemctl start nginx,并设置开机自启:sudo systemctl enable nginx

4. 启动集群并验证

4.1 启动Tomcat实例

在每个节点上进入Tomcat的bin目录,执行启动命令:

cd /opt/tomcat/bin
./startup.sh

启动后通过ps -ef | grep tomcat验证进程是否存在,通过tail -f logs/catalina.out查看启动日志。

4.2 验证集群功能

5. 可选优化

0
看了该问题的人还看了