天天看點

項目代碼部署到Linux上并運作

舉例:java項目為cap-cbs-mgr,準備部署到開發環境的linux機器上。

1.本地項目的根目錄下需要放入三個檔案:package-upload.bat,PLINK.EXE,PSCP.EXE。

package-upload.bat的shell腳本内容如下:(本文中涉及賬号密碼就不友善顯示了)

echo off

set "PROJECT_NAME=xxx"
set "HOST=xx.xx.xx.xx"
set "USER=xxx"
set "PWD=xxxx"
set "TOMCAT_HOME=/apps/svr/tomcat-cbs-mgr"
set "SCRIPT=/home/apps/tomcat-cbs-mgr"
set "ZOOKEEPER=xx.xx.xx.xx.xx:xxxx,xx.xx.xx.xx.xx:xxxx,..."

REM ---------------------------------------------
REM * package
REM ---------------------------------------------
call gradlew --refresh-dependencies clean war

REM ---------------------------------------------
REM * Stop remote tomcat and delete all files and 
REM * folders in tomcat webapps directory
REM ---------------------------------------------
plink -pw %PWD% %USER%@%HOST% "sh %SCRIPT% stop ; rm -rf %TOMCAT_HOME%/webapps/cap-cbs*"

REM ---------------------------------------------
REM * Copy war file to remote tomcat webapps directory
REM ---------------------------------------------
pscp -pw %PWD% %PROJECT_NAME%-webapp\build\libs\*.war %USER%@%HOST%:%TOMCAT_HOME%/webapps

REM ---------------------------------------------
REM * Start remote tomcat
REM * Show remote tomcat logs, press Ctrl + C to exit
REM ---------------------------------------------
plink -pw %PWD% %USER%@%HOST% "export VIP_CFGCENTER_ZK_CONNECTION=%ZOOKEEPER% ; source /etc/profile;  sh %SCRIPT% start ; sh %SCRIPT%"
           

PLINK.EXE和PSCP.EXE這兩個打包和傳輸工具可以自行去網上下載下傳。

Linux伺服器上需要:安裝tomcat【路徑為/apps/svr/tomcat-cbs-mgr】,jdk【路徑為/apps/svr/jdk7】,tomcat-cbs-mgr腳本【路徑為/home/apps/tomcat-cbs-mgr】,環境變量的配置檔案profile【路徑為/etc/profile】。

tomcat-cbs-mgr的shell腳本内容如下:

#!/bin/bash

## language environment setting
export LANG=zh_CN.UTF-8

## Tomcat server base directory
TOMCAT_HOME=/apps/svr/tomcat-cbs-mgr

ENV=development

## Catalina base directory, the directory is same with $TOMCAT_HOME
CATALINA_HOME=$TOMCAT_HOME

## Tomcat server logs directory
TOMCAT_LOGS_DIR=$TOMCAT_HOME/logs

## Tomcat service process ID file
TOMCAT_PID_FILE=$TOMCAT_LOGS_DIR/server.pid

## Identify application that view process infomation using 'ps', 'top', etc. commands
APP_MAGIC_NUMBER=cap-cbs-mgr

## comment the line on online environment
JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=$ENV"

## JVM parameters
JAVA_OPTS="$JAVA_OPTS -Dapp.name=$APP_MAGIC_NUMBER"
JAVA_OPTS="$JAVA_OPTS -Dwebapps.logs.dir=$TOMCAT_LOGS_DIR"
JAVA_OPTS="$JAVA_OPTS -Xms512m -Xmx512m"

if [ -z $JAVA_HOME ]; then
  export JAVA_HOME=/apps/svr/jdk7
fi

if [ -z $VIP_CFGCENTER_ZK_CONNECTION ]; then
  export VIP_CFGCENTER_ZK_CONNECTION=xx.xx.xx.xx.xx:xxxx,xx.xx.xx.xx.xx:xxxx,...
fi


echo "======================================="
echo " JAVA_HOME   : $JAVA_HOME"
echo " TOMCAT_HOME : $TOMCAT_HOME"
echo " LOGS_DIR    : $TOMCAT_LOGS_DIR"
echo " PID_FILE    : $TOMCAT_PID_FILE"
echo " APP_MAGIC   : $APP_MAGIC_NUMBER"
echo " ENV         : $ENV"
echo "======================================="


if [ "$1" == "" ]; then
  tail -F $TOMCAT_LOGS_DIR/*.log $TOMCAT_LOGS_DIR/catalina.out
  exit 0
fi

RUNNING_PID=$(ps -efww | grep $APP_MAGIC_NUMBER | grep -v grep | awk '{print $2}')

if [ "$1" == "start" ]; then

  if [ ! -z "$RUNNING_PID" ]; then
    echo "tomcat started, PID = $RUNNING_PID"
    exit 1
  fi

  rm -rf $TOMCAT_LOGS_DIR/* $TOMAT_HOME/work/*

  source $TOMCAT_HOME/bin/catalina.sh start "[email protected]" &
  sleep 0.1s  
  PID=$(ps -efww | grep $APP_MAGIC_NUMBER | grep -v grep | awk '{print $2}')
  echo $PID > $TOMCAT_LOGS_DIR/server.pid
  echo "tomcat startup, PID = $PID, using $0 command tail start logging"
  exit 0
fi

if [ "$1" == "stop" ]; then

  if [ -z "$RUNNING_PID" ]; then
    echo "tomcat no start"
    if [ -f "$TOMCAT_PID_FILE" ]; then
      rm "$TOMCAT_PID_FILE"
    fi
    exit 2
  fi

  # source $TOMCAT_HOME/bin/catalina.sh stop "[email protected]" &
  kill -9 $RUNNING_PID
  echo "tomcat stopping..., using $0 command tail stop log"
  rm "$TOMCAT_PID_FILE"
  exit 0
fi


           

當然了,有需要的朋友不能全盤照搬,需要根據自己項目和伺服器的實際情況加以改動即可使用~~~

項目代碼部署到Linux上并運作