Debian上Tomcat如何进行集群部署
Prerequisites
Before configuring the Tomcat cluster on Debian, ensure the following prerequisites are met:
- All Debian servers in the cluster have the same Tomcat version installed (e.g., Tomcat 9).
- Java Development Kit (JDK) 8 or later is installed (
sudo apt install default-jdk
). - A dedicated system user (e.g.,
tomcat
) is created for running Tomcat instances to enhance security (sudo groupadd tomcat & & sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
). - Network connectivity between all cluster nodes (for multicast communication, which is the default clustering method).
Step 1: Install Tomcat on Each Node
- Update the package list and install Tomcat:
sudo apt update & & sudo apt install tomcat9 tomcat9-admin
- Verify the installation by accessing
http://< node-ip> :8080
in a browser. You should see the Tomcat welcome page.
Step 2: Configure Multiple Tomcat Instances
To run multiple Tomcat instances on a single node (or across multiple nodes), duplicate the Tomcat directory and customize ports for each instance:
- Create instance directories (e.g.,
tomcat1
,tomcat2
):sudo mkdir -p /opt/tomcat1/{ conf,webapps,temp,work} & & sudo cp -R /usr/share/tomcat9/* /opt/tomcat1/ sudo mkdir -p /opt/tomcat2/{ conf,webapps,temp,work} & & sudo cp -R /usr/share/tomcat9/* /opt/tomcat2/
- Modify ports in each instance’s
conf/server.xml
to avoid conflicts:- Shutdown Port: Change
< Connector port="8005"
to unique values (e.g.,8005
fortomcat1
,8006
fortomcat2
). - HTTP Connector: Change
< Connector port="8080"
to unique values (e.g.,8081
fortomcat2
). - AJP Connector: Change
< Connector port="8009"
to unique values (e.g.,8010
fortomcat2
).
- Shutdown Port: Change
- Set ownership of instance directories to the
tomcat
user:sudo chown -R tomcat:tomcat /opt/tomcat1 /opt/tomcat2
Step 3: Enable Clustering in server.xml
Edit the conf/server.xml
file for each Tomcat instance to enable clustering. The key configurations include:
- Cluster Class: Use
org.apache.catalina.ha.tcp.SimpleTcpCluster
for basic TCP clustering. - Membership: Configure multicast communication (all nodes must use the same multicast address and port).
- JvmRoute: Add a unique
jvmRoute
attribute to the< Engine>
element (e.g.,jvmRoute="node1"
fortomcat1
,jvmRoute="node2"
fortomcat2
).
Example configuration for one node:
<
Engine name="Catalina" defaultHost="localhost" jvmRoute="node1">
<
Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster">
<
!-- Membership: Multicast for cluster discovery -->
<
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: TCP listener for this node -->
<
Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
autoBind="100"
selectorTimeout="5000"/>
<
!-- Sender: Replication transmitter -->
<
Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<
Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
<
/Sender>
<
!-- Interceptors: Failure detection and message dispatch -->
<
Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<
Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/>
<
/Channel>
<
!-- Replication Valve: Filters non-session resources -->
<
Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;
.*\.js;
.*\.jpg;
.*\.png;
.*\.css;
.*\.txt"/>
<
!-- Session Listener: Tracks session changes -->
<
ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
<
/Cluster>
<
/Engine>
Repeat this step for all nodes, ensuring the jvmRoute
and Receiver
port are unique per instance.
Step 4: Configure Session Replication
For session consistency across the cluster, add the <
distributable/>
element to the web.xml
file of each web application:
<
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>
This tells Tomcat to replicate sessions across the cluster.
Step 5: Set Up a Load Balancer (Nginx)
Use Nginx as a reverse proxy to distribute requests across the Tomcat cluster. Install Nginx and configure it as follows:
- Install Nginx:
sudo apt install nginx
- Edit the Nginx configuration file (e.g.,
/etc/nginx/sites-available/default
):upstream tomcat_cluster { server 192.168.1.101:8080; # Replace with IP/port of tomcat1 server 192.168.1.102:8081; # Replace with IP/port of tomcat2 } server { listen 80; server_name your_domain.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; } }
- Restart Nginx to apply changes:
sudo systemctl restart nginx
Step 6: Start and Validate the Cluster
- Start each Tomcat instance as the
tomcat
user:sudo systemctl start tomcat9@tomcat1 # For instance 1 sudo systemctl start tomcat9@tomcat2 # For instance 2
- Check cluster status by accessing the Tomcat Manager (e.g.,
http://< node-ip> :8080/manager/html
) and navigating to the “Cluster” section. You should see all nodes listed as “running.” - Test session replication by deploying a sample application (e.g., a simple JSP with a session variable) to the cluster. Access the app through Nginx and verify that the session persists across different Tomcat instances.
Optional: Use Redis for Centralized Session Storage
For more robust session management (e.g., across multiple data centers), configure Tomcat to use Redis as a session store:
- Install Redis:
sudo apt install redis-server
- Add the Redis session manager dependency to your web application’s
WEB-INF/lib
directory (e.g.,tomcat-redis-session-manager-x.x.x.jar
andcommons-pool2-x.x.x.jar
). - Configure the
context.xml
file of your web application:< Context> < Manager className="org.apache.catalina.session.PersistentManager" storeClassName="org.apache.catalina.session.RedisStore" connectionURL="redis://localhost:6379/0" driverName="org.apache.tomcat.session.RedisStore"/> < /Context>
This setup ensures sessions are stored in Redis, eliminating the need for multicast communication.
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Debian上Tomcat如何进行集群部署
本文地址: https://pptw.com/jishu/729886.html