jenkins 构建部署(soa)
1、全局工具配置
2、soa端job配置
/opt/script/deploy-soa-transfer-21.sh
#!/bin/sh echo $1 projectname=$1 projectdir="/opt/soa/${projectname}" tar_name="${projectname}.tar.gz" rm -rf ${projectdir}/*.tar.gz mkdir -p ${projectdir} /usr/bin/rsync -avzP www@***.***.***.***::rsync_pro_womaiwms/${projectname}/*.tar.gz ${projectdir}/${projectname}.tar.gz # ***是jenkins服务器ip
以上是jenkins构建soa项目tar包,下面是部署启动项目,另外新建一个job
/opt/script/deploy-soa-only.sh
#!/bin/sh echo $1 projectname=$1 projectdir="/opt/soa/${projectname}" tar_name="${projectname}.tar.gz" root_file=root$(date +'%Y%m%d_%H%M%S') default_file="default" latest_file="latest" if [ ! -f "${projectdir}/${tar_name}" ]; then echo "error! ================== ${projectdir}/${tar_name} is not exist" exit -1; fi mkdir -p ${projectdir}/${root_file} tar -zxvf ${projectdir}/${tar_name} -C ${projectdir}/${root_file} echo "unzip success, will restart soa server!" #sh ${projectdir}/${default_file}/bin/stop.sh sh /opt/script/soa/soa-one.sh ${projectname} stop unlink ${projectdir}/$latest_file rm -rf ${projectdir}/$latest_file mv ${projectdir}/$default_file ${projectdir}/$latest_file ln -s ${projectdir}/${root_file} ${projectdir}/${default_file} echo "****start startup soa****" #sh ${projectdir}/${default_file}/bin/start.sh sh /opt/script/soa/soa-one.sh ${projectname} start
/opt/script/soa/soa-one.sh
#!/bin/bash if [ "$2" != "start" ] && [ "$2" != "stop" ]; then echo "ERROR: Please input 2 argument: start or stop" exit -1; fi projectname=$1 sh /opt/soa/${projectname}/default/bin/server.sh $2
server.sh脚本
#!/bin/bash cd `dirname $0` if [ "$1" = "start" ]; then sh ./start.sh else if [ "$1" = "stop" ]; then sh ./stop.sh else if [ "$1" = "debug" ]; then sh ./start.sh debug else if [ "$1" = "restart" ]; then sh ./restart.sh else if [ "$1" = "dump" ]; then sh ./dump.sh else echo "ERROR: Please input argument: start or stop or debug or restart or dump" exit 1 fi fi fi fi fi
dump.sh
#!/bin/bash cd `dirname $0` BIN_DIR=`pwd` cd .. DEPLOY_DIR=`pwd` CONF_DIR=$DEPLOY_DIR/conf . "$BIN_DIR"/env.sh PIDS=`ps --no-heading -C java -f --width 1000 | grep "$CONF_DIR" |awk '{print $2}'` if [ -z "$PIDS" ]; then echo "ERROR: The $SERVER_NAME does not started!" exit 1 fi DUMP_DIR=$LOGS_DIR/dump if [ ! -d $DUMP_DIR ]; then mkdir $DUMP_DIR fi DUMP_DATE=`date +%Y%m%d%H%M%S` DATE_DIR=$DUMP_DIR/$DUMP_DATE if [ ! -d $DATE_DIR ]; then mkdir $DATE_DIR fi echo -e "Dumping the $SERVER_NAME ...\c" for PID in $PIDS ; do jstack $PID > $DATE_DIR/jstack-$PID.dump 2>&1 echo -e ".\c" jinfo $PID > $DATE_DIR/jinfo-$PID.dump 2>&1 echo -e ".\c" jstat -gcutil $PID > $DATE_DIR/jstat-gcutil-$PID.dump 2>&1 echo -e ".\c" jstat -gccapacity $PID > $DATE_DIR/jstat-gccapacity-$PID.dump 2>&1 echo -e ".\c" jmap $PID > $DATE_DIR/jmap-$PID.dump 2>&1 echo -e ".\c" jmap -heap $PID > $DATE_DIR/jmap-heap-$PID.dump 2>&1 echo -e ".\c" jmap -histo $PID > $DATE_DIR/jmap-histo-$PID.dump 2>&1 echo -e ".\c" if [ -r /usr/sbin/lsof ]; then /usr/sbin/lsof -p $PID > $DATE_DIR/lsof-$PID.dump echo -e ".\c" fi done if [ -r /bin/netstat ]; then /bin/netstat -an > $DATE_DIR/netstat.dump 2>&1 echo -e ".\c" fi if [ -r /usr/bin/iostat ]; then /usr/bin/iostat > $DATE_DIR/iostat.dump 2>&1 echo -e ".\c" fi if [ -r /usr/bin/mpstat ]; then /usr/bin/mpstat > $DATE_DIR/mpstat.dump 2>&1 echo -e ".\c" fi if [ -r /usr/bin/vmstat ]; then /usr/bin/vmstat > $DATE_DIR/vmstat.dump 2>&1 echo -e ".\c" fi if [ -r /usr/bin/free ]; then /usr/bin/free -t > $DATE_DIR/free.dump 2>&1 echo -e ".\c" fi if [ -r /usr/bin/sar ]; then /usr/bin/sar > $DATE_DIR/sar.dump 2>&1 echo -e ".\c" fi if [ -r /usr/bin/uptime ]; then /usr/bin/uptime > $DATE_DIR/uptime.dump 2>&1 echo -e ".\c" fi echo "OK!" echo "DUMP: $DATE_DIR"
env.sh
CLASS_NAME="com.***.soa.service.launcher.Provider" CONF_NAME="conf/applicationContext-dubbo.xml" if [ -z "$SERVER_NAME" ]; then SERVER_NAME=`sed '/dubbo:application/!d;s/.*="//;s/".*//' $CONF_NAME | tr -d '\r'` fi if [ -z "$SERVER_NAME" ]; then SERVER_NAME=`hostname` fi if [ -z "SERVER_PORT" ]; then SERVER_PORT=`sed '/dubbo:protocol.*port/!d;s/.*port="//;s/".*//' $CONF_NAME | tr -d '\r'` fi #LOGS_DIR="" LOGS_DIR="/opt/logs/soa/${SERVER_NAME}/" if [ ! -n "$LOGS_DIR" ]; then LOGS_DIR=$DEPLOY_DIR/logs fi if [ ! -d $LOGS_DIR ]; then mkdir $LOGS_DIR fi echo $SERVER_NAME $SERVER_PORT $CLASS_NAME $CONF_NAME $LOGS_DIR export LD_LIBRARY_PATH=/usr/local/bdb/lib
restart.sh
#!/bin/bash cd `dirname $0` sh ./stop.sh sh ./start.sh
start.sh
#!/bin/bash cd `dirname $0` BIN_DIR=`pwd` cd .. DEPLOY_DIR=`pwd` CONF_DIR=$DEPLOY_DIR/conf . "$BIN_DIR"/env.sh PIDS=`ps --no-heading -C java -f --width 1000 | grep "$CONF_DIR" |awk '{print $2}'` if [ -n "$PIDS" ]; then echo "ERROR: The $SERVER_NAME already started!" echo "PID: $PIDS" exit 1 fi if [ -n "$SERVER_PORT" ] && [ $SERVER_PORT -gt 0 ]; then echo 'netstat -tln | grep "$SERVER_PORT" | wc -l' SERVER_PORT_COUNT=`netstat -tln | grep $SERVER_PORT | wc -l` if [ $SERVER_PORT_COUNT -gt 0 ]; then echo "ERROR: The $SERVER_NAME port $SERVER_PORT already used!" exit 1 fi fi echo $SERVER_NAME $SERVER_PORT $CLASS_NAME $LOGS_DIR $CONF_NAME STDOUT_FILE=$LOGS_DIR/stdout.log STDERR_FILE=$LOGS_DIR/stderr.log echo $LOGS_DIR $STDOUT_FILE $STDERR_FILE LIB_DIR=$DEPLOY_DIR/lib LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"` JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true " JAVA_DEBUG_OPTS="" if [ "$1" = "debug" ]; then JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n " fi JAVA_JMX_OPTS="" if [ "$1" = "jmx" ]; then JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false " fi JAVA_MEM_OPTS="" BITS=`file $JAVA_HOME/bin/java | grep 64-bit` if [ -n "$BITS" ]; then let memTotal=`cat /proc/meminfo |grep MemTotal|awk '{printf "%d", $2/1024 }'` if [ $memTotal -gt 2500 ];then JAVA_MEM_OPTS=" -server -Xmx2g -Xms2g -Xmn512m -XX:PermSize=256m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./base_java_pid<pid>.hprof" else JAVA_MEM_OPTS=" -server -Xmx1g -Xms1g -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./base_java_pid<pid>.hprof" fi else JAVA_MEM_OPTS=" -server -Xmx2g -Xms2g -Xmn512m -XX:PermSize=256m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./base_java_pid<pid>.hprof" fi echo -e "Starting the $SERVER_NAME ...\c" nohup /usr/java/default/bin/java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS -classpath $CONF_DIR:$LIB_JARS $CLASS_NAME 1>>${STDOUT_FILE} 2>>${STDERR_FILE} & COUNT=0 NUM=0 while [ $COUNT -lt 1 ]; do echo -e ".\c" sleep 1 if [ -n "$SERVER_PORT" ] && [ "$SERVER_PORT" -gt 0 ]; then echo 'echo status | nc 127.0.0.1 $SERVER_PORT -i 1 | grep -c OK' COUNT=`echo status | nc 127.0.0.1 $SERVER_PORT -i 1 | grep -c OK` else COUNT=`ps --no-heading -C java -f --width 1000 | grep "$DEPLOY_DIR" | awk '{print $2}' | wc -l` fi if [ $COUNT -gt 0 ]; then break fi let "NUM++" #echo -n "$NUM " if [ $NUM -gt 10 ]; then echo "ERROR!" echo `cat $STDERR_FILE` exit -1 fi done PIDS=`ps --no-heading -C java -f --width 1000 | grep "$DEPLOY_DIR" | awk '{print $2}'` echo "PID: $PIDS" echo "STDOUT: $STDOUT_FILE" echo "STDERR: $STDERR_FILE" echo "OK!"
stop.sh
#!/bin/bash cd `dirname $0` BIN_DIR=`pwd` cd .. DEPLOY_DIR=`pwd` CONF_DIR=$DEPLOY_DIR/conf . "$BIN_DIR"/env.sh PIDS=`ps --no-heading -C java -f --width 1000 | grep "$CONF_DIR" |awk '{print $2}'` if [ -z "$PIDS" ]; then echo "ERROR: The $SERVER_NAME does not started!" exit 1 fi if [ "$1" != "skip" ]; then sh $BIN_DIR/dump.sh fi echo -e "Stopping the $SERVER_NAME ...\c" for PID in $PIDS ; do kill -9 $PID > /dev/null 2>&1 done COUNT=0 while [ $COUNT -lt 1 ]; do echo -e ".\c" sleep 1 COUNT=1 for PID in $PIDS ; do PID_EXIST=`ps --no-heading -p $PID` if [ -n "$PID_EXIST" ]; then COUNT=0 break fi done done echo "OK!" echo "PID: $PIDS"
distribution.xml
<?xml version="1.0" encoding="utf-8"?> <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> <id>release</id> <formats> <format>zip</format> <format>tar.gz</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>target/classes</directory> <outputDirectory>conf</outputDirectory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.txt</include> </includes> </fileSet> <fileSet> <lineEnding>unix</lineEnding> <directory>src/main/bin</directory> <outputDirectory>bin</outputDirectory> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> <scope>runtime</scope> </dependencySet> </dependencySets> </assembly>