准备一tomcat,设置tomcat路径即可。
#!/bin/bash
# 定义Tomcat安装目录和端口号
TOMCAT_HOME="/opt/apache-tomcat-8.5.93"
PORT_BASE=8080
SHUTDOWN_PORT_BASE=8005
NUM_INSTANCES=3 # 启动的Tomcat实例数量
# 循环启动多个Tomcat实例
for ((i=1; i<=NUM_INSTANCES; i++))
do
INSTANCE_PORT=$((PORT_BASE + i))
INSTANCE_SHUTDOWN_PORT=$((SHUTDOWN_PORT_BASE + i))
INSTANCE_DIR="$TOMCAT_HOME/instance$i"
INDEX_JSP="$INSTANCE_DIR/webapps/ROOT/index.jsp"
# 创建Tomcat实例目录
cp -r "$TOMCAT_HOME" "$INSTANCE_DIR"
# 修改Tomcat实例的端口号
sed -i "s/8080/$INSTANCE_PORT/g" "$INSTANCE_DIR/conf/server.xml"
# 修改Tomcat实例的shutdown端口
sed -i "s/8005/$INSTANCE_SHUTDOWN_PORT/g" "$INSTANCE_DIR/conf/server.xml"
# 在index.jsp文件中添加区分信息
echo "This is instance $i" > "$INDEX_JSP"
# 启动Tomcat实例
"$INSTANCE_DIR/bin/startup.sh" &
# 输出启动信息
echo "Tomcat instance $i started on port $INSTANCE_PORT with custom index.jsp and shutdown port $INSTANCE_SHUTDOWN_PORT"
done
echo "Tomcat cluster started with $NUM_INSTANCES instances"