linux

Linux下Tomcat集群如何搭建

小樊
39
2025-11-02 23:12:35
栏目: 智能运维

Linux下Tomcat集群搭建详细步骤

1. 环境准备

1.1 安装Java环境

Tomcat依赖JDK 1.8及以上版本,所有集群节点需安装相同版本的JDK。以CentOS为例,执行以下命令:

sudo yum install -y java-1.8.0-openjdk-devel
# 验证安装
java -version

1.2 下载并安装Tomcat

选择相同版本的Tomcat(如9.0.56),在每台节点上执行:

# 下载Tomcat(以9.0.56为例)
wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.tar.gz
# 解压到/opt目录
sudo tar -xvf apache-tomcat-9.0.56.tar.gz -C /opt
# 创建软链接(方便管理)
sudo ln -s /opt/apache-tomcat-9.0.56 /opt/tomcat

1.3 配置环境变量

编辑/etc/profile文件,添加Tomcat环境变量:

export CATALINA_HOME=/opt/tomcat
export PATH=$CATALINA_HOME/bin:$PATH

使配置生效:

source /etc/profile

2. 配置Tomcat集群

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

每台节点的/opt/tomcat/conf/server.xml文件中,<Engine>元素内添加集群配置

<Engine name="Catalina" defaultHost="localhost" jvmRoute="node1"> <!-- jvmRoute需唯一(如node1、node2) -->
    <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;"/>
        <!-- 部署器(同步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>
</Engine>

2.2 启用会话复制

/opt/tomcat/conf/context.xml文件中,为<Context>元素添加distributable="true"属性,启用会话分布式存储:

<Context distributable="true">
    <!-- 其他配置 -->
</Context>

2.3 配置web应用

在web应用的WEB-INF/web.xml文件中,添加<distributable/>标签,声明该应用支持集群会话复制:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="3.1">
    <distributable/>
    <!-- 其他配置 -->
</web-app>

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

3.1 安装Nginx

以CentOS为例,执行以下命令:

sudo yum install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx

3.2 配置负载均衡

编辑/etc/nginx/nginx.conf文件,在http块中添加upstream配置:

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

重载Nginx配置:

sudo nginx -t  # 测试配置语法
sudo systemctl reload nginx

4. 启动集群并验证

4.1 启动Tomcat实例

在每台节点上执行:

/opt/tomcat/bin/startup.sh
# 验证是否启动成功
tail -f /opt/tomcat/logs/catalina.out

4.2 验证集群状态

注意事项

  1. 端口冲突:确保每台节点的Tomcat端口(Server Port、Connector Port、AJP Port)不重复。
  2. 防火墙设置:开放Tomcat端口(8080)和组播端口(45564),允许节点间通信:
    sudo firewall-cmd --add-port=8080/tcp --permanent
    sudo firewall-cmd --add-port=45564/udp --permanent
    sudo firewall-cmd --reload
    
  3. 会话粘滞:若应用需要固定用户到某节点(如上传文件),可在Nginx中配置ip_hash
    upstream tomcat_cluster {
        ip_hash;  # 会话粘滞
        server 192.168.1.101:8080;
        server 192.168.1.102:8080;
    }
    
  4. 共享存储:若应用依赖文件系统(如上传目录),建议使用NFS或分布式存储(如MinIO)实现共享,避免文件不同步问题。

0
看了该问题的人还看了