ubuntu

Ubuntu Tomcat集群配置教程

小樊
51
2025-09-20 13:24:58
栏目: 智能运维

Ubuntu Tomcat集群配置教程

1. 环境准备

在开始配置前,需完成以下基础准备工作:

2. 配置Tomcat集群

2.1 修改server.xml启用集群

进入/opt/tomcat/conf目录,编辑server.xml文件(备份原文件后再修改)。在<Engine>标签内添加集群配置,关键参数说明:

示例配置:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"> <!-- jvmRoute需唯一,如tomcat1、tomcat2 -->
  <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.TcpPingInterceptor"/>
      <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
    </Channel>
    <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/>
    <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 配置应用支持集群

在需要集群部署的应用的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_4_0.xsd"
         version="4.0">
  <distributable/>
</web-app>

2.3 确保会话对象可序列化

所有存入HttpSession的对象必须实现java.io.Serializable接口,否则无法同步会话数据。例如:

public class UserSession implements Serializable {
    private static final long serialVersionUID = 1L;
    private String username;
    private String role;
    // getters and setters
}

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

3.1 安装Nginx

在单独的服务器(或其中一个Tomcat节点)上安装Nginx:

sudo apt update
sudo apt install nginx -y

3.2 配置Nginx上游服务器组

编辑Nginx配置文件(/etc/nginx/nginx.conf/etc/nginx/conf.d/tomcat.conf),添加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 {
        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 启用会话保持(可选)

若应用需要保持用户会话(如购物车),可在upstream块中添加ip_hash指令,使同一用户的请求始终转发到同一Tomcat节点:

upstream tomcat_cluster {
    ip_hash; # 会话保持
    server 192.168.1.101:8080;
    server 192.168.1.102:8080;
}

3.4 测试并重载Nginx

验证Nginx配置语法:sudo nginx -t,若无错误则重载配置:sudo systemctl reload nginx

4. 启动集群并验证

4.1 启动Tomcat实例

在每个Tomcat节点上,进入/opt/tomcat/bin目录,启动Tomcat:

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

4.2 验证集群状态

5. 注意事项

0
看了该问题的人还看了