tomcat多实例部署包含Jenkins对接-日记

随着项目发展, 需要将一个应用部署到多个环境上, 因为关系tomcat版本统一, 所以需要同一个tomcat下将一个应用部署到不同的端口, 这些应用连接不同数据库, 这里使用了tomcat多实例的方式. 利用tomcat环境变量CATALINA_BASE, 一共两个环境:

  • 开发环境 连接开发库
  • 测试环境 连接测试库

首先准备两个环境的实例目录
开发环境: multi-instance/dev
测试环境: multi-instance/test

两个实例目录都包含以下目录:
– bin 存放脚本 一般是启动脚本
– conf 实例配置文件, 复制 ${CATALINA_HOME}/conf 修改server.xml文件端口 注意SHUTDOWN 端口多个实例不能重复
– logs 实例日志
– temp
– webapps 实例运行应用
– work jsp等编译后的class文件

然后在 jenkins构建项目完成后, 将运行文件复制到对应实例的 webapps目录下, 在这里之前删除 之前部署应用的文件

启动脚本大致如下:

JAVA_HOME=/home/crt/jdk/jdk1.7.0_79
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

export CATALINA_BASE="$dev_home"
export CATALINA_HOME="/home/crt/tomcat/apache-tomcat-7.0.72"

echo "开始重启开发环境[CATALINA_BASE=$CATALINA_BASE]::::::::::::::::::::::::::::::::::::::::::"

${CATALINA_HOME}/bin/startup.sh

开发环境启动完整脚本:

#!/bin/sh

#var
dev_home=/home/crt/tomcat/multi-instance/dev
dir_src_fromJenkins=/home/crt/jenkins/jenkins_home/jobs/myapps/workspace

cd $dev_home

pids=`ps -ef | grep 'tomcat/multi-instance/dev' | grep -v grep | awk '{print $2}'`

for pid in ${pids[@]}
do
  if [ -n $pid ]; then
  echo "Kill the $pid "
  kill -9 $pid
  fi
done

echo '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[['

sleep 2

#删除缓存
rm -rf $dev/webapps/* $dev_home/work/Catalina/localhost/*

#crp war
war_1=$dir_src_fromJenkins/app1/target/app1.war

#解压war
##unzip $file -d $dirname/
unzip $war_1 -d -o $dev_home/webapps/app1

#修改配置文件
cd $dev_home/webapps/app1/WEB-INF/classes/
f="app.properties"
newurl="platform.url:http://172.17.60.133:6060/app1"

## 替换应用访问地址
sed -i "/platform.url/d" $f

sed -i "/^#sed_mount/a$newurl" $f
##使用开发数据库
sed -i "s/data/data2/g" $f

JAVA_HOME=/home/crt/jdk/jdk1.7.0_79
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

export CATALINA_BASE="$dev_home"
export CATALINA_HOME="/home/crt/tomcat/apache-tomcat-7.0.72"

echo "开始重启开发环境[CATALINA_BASE=$CATALINA_BASE]::::::::::::::::::::::::::::::::::::::::::"

${CATALINA_HOME}/bin/startup.sh

sleep 5

测试环境部署[包含jenkins对接]脚本:

#!/bin/bash

tpids=`ps -ef | grep 'tomcat/multi-instance/test' | grep -v grep | awk '{print $2}'`

for pid in ${tpids[@]}
do
  if [ -n $pid ]; then
  echo "kill the $pid "
  kill -9 $pid
  fi
done
echo '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[['

sleep 2
## test webapps_home
test_home=/home/crt/tomcat/multi-instance/test
dir_src_fromJenkins=/home/crt/jenkins/jenkins_home/jobs/myapps/workspace

cd $test_home

# app1
war_1=$dir_src_fromJenkins/app1/target/app1.war
# app2
war_2=$dir_src_fromJenkins/app2/target/app2.war
wars=(
$war_1
$war_2
)
#缓存
webapps=$test_home/webapps
del_dir=$test_home/work/Catalina/localhost

#del
echo 'Tomcat cache cleanup in {'$webapps '}{' $del_dir'}:::::::::::::::::::::::::::::::::::::::::'
rm -rf $webapps/* $del_dir/*

#cp war
echo '开始将war复制到测试环境[instance-test], into loop, path: '$webapps

for _e in ${wars[@]}
do
  echo "[e- $_e]"
  cp $_e $webapps
done

echo 'copied done,restart........'
echo '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[['

## source /etc/profile

### ./startup.sh &

JAVA_HOME=/home/crt/jdk/jdk1.7.0_79
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

export CATALINA_BASE="$test_home"
export CATALINA_HOME="/home/crt/tomcat/apache-tomcat-7.0.72"

echo $CATALINA_BASE $CATALINA_HOME

${CATALINA_HOME}/bin/startup.sh

##nohup sh startup.sh &

##同步开发环境
echo '开始同步开发环境'

sleep 5

##启动开发环境
source ../dev/bin/start-dev.sh

sleep 6

echo 'restarted........'

#jenkins 默认会永久保存每次构建信息, 如此会将磁盘空间耗尽, 建议按需要修改每个job配置 [#配置 #丢弃旧的构建 ]

    原文作者:_palm
    原文地址: https://www.jianshu.com/p/5c4801001828
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞