java spring boot項目部署-上

來源:互聯網
上載者:User

標籤:files   1.5   ready   return   coding   nis   one   dev   mod   

1.編寫sh指令碼,便於伺服器上管理工程:

#!/bin/bashsource /etc/profilePROG_NAME=$0ACTION=$1usage() {    echo "Usage: $PROG_NAME {start|stop|restart|status|tailf}"    exit 1;}# colorsred=‘\e[0;31m‘green=‘\e[0;32m‘yellow=‘\e[0;33m‘reset=‘\e[0m‘echoRed() { echo -e "${red}$1${reset}"; }echoGreen() { echo -e "${green}$1${reset}"; }echoYellow() { echo -e "${yellow}$1${reset}"; }APP_HOME=$(cd $(dirname $0)/..; pwd)app=${project.build.finalName}.${project.packaging}cd $APP_HOMEmkdir -p logspidfile=logs/app.pidlogfile=logs/start.`date +%F`.logJAVA_OPTS="${java_opts}"bakdir=/data/ops/packages/app_bak/${project.build.finalName}bakfile=$bakdir/${project.build.finalName}`date +%F`.${project.packaging}function check_pid() {    if [ -f $pidfile ];then        pid=`cat $pidfile`        if [ -n $pid ]; then            running=`ps -p $pid|grep -v "PID TTY" |wc -l`            return $running        fi    fi    return 0}function start() {    check_pid    running=$?    if [ $running -gt 0 ];then        echoGreen "$app now is running already, pid=`cat $pidfile`"        return 1    fi        nohup java -jar $JAVA_OPTS $app >> ${logfile} 2>&1 & pid=$!      echoGreen "$app starting "    for e in $(seq 10); do        echo -n " $e"        sleep 1    done    echo $pid > $pidfile    check_pid    running=$?    if [ $running -gt 0 ];then        echoGreen " ,pid=`cat $pidfile`"        return 1    else        echoRed ",started fail!!!"    fi}function stop() {    pid=`cat $pidfile`    kill -9 $pid    echoRed "$app stoped..."}function restart() {    stop    sleep 1    start}function backup(){  if [ ! -x $bakdir ];then    mkdir -p $bakdir  fi  if [ ! -f $bakfile ];then        cp $app $bakfile        echo $bakfile backup finish  else        echo $bakfile is already backup  fi}function rollback(){  if [ ! -f $bakfile ];then        echo $bakfile backup not found  else        rm -f $app        cp $bakfile $app        echo $app rollback finish  fi}function tailf() {        tail -f $APP_HOME/$logfile}function status() {    check_pid    running=$?    if [ $running -gt 0 ];then        echoGreen "$app now is running, pid=`cat $pidfile`"    else        echoYellow "$app is stoped"    fi}function main {   RETVAL=0   case "$1" in      start)         start         ;;      stop)         stop         ;;      restart)         restart         ;;      tailf)         tailf         ;;      status)         status         ;;      backup)         backup         ;;      rollback)         rollback         ;;      *)         usage         ;;      esac   exit $RETVAL}main $1

檔案中包含多個站位符,可以藉助spring filter打包時進行填充,如將sh起名為app.sh放置於maven格式項目的src/main/bin目錄下則在pom檔案中可添加如下配置,如: 

    <profiles>        <!--開發預設環境-->        <profile>            <id>dev</id>            <activation>                <activeByDefault>true</activeByDefault>            </activation>            <properties>                <profileActive>dev</profileActive>                <java_opts>-server -Xms512m -Xmx512m -XX:NewSize=128m -XX:MaxNewSize=128m -Xss256k</java_opts>                <bakcupdir>/data/ops/packages/app_bak</bakcupdir>            </properties>        </profile>        <!--生產環境-->        <profile>            <id>product</id>            <properties>                <profileActive>product</profileActive>                <java_opts>-server -Xms2048m -Xmx2048m -XX:NewSize=256m -XX:MaxNewSize=256m -Xss256k</java_opts>                <bakcupdir>/data/ops/packages/app_bak</bakcupdir>            </properties>        </profile>    </profiles>    <build>        <finalName>liam-service</finalName>        <resources>            <resource>                <directory>src/main/resources</directory>                <filtering>true</filtering>            </resource>            <resource>                <directory>src/main/resources/keys/*</directory>                <includes>                    <include>webank_keystore.jks</include>                    <include>webank_truststore.jks</include>                </includes>                <filtering>false</filtering>            </resource>            <!--也就是此處配置上maven打包需要進行配置的檔案-->            <resource>                <directory>src/main/bin</directory>                <targetPath>${project.build.directory}/bin</targetPath>                <filtering>true</filtering>            </resource>        </resources>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>                <version>1.5.6.RELEASE</version>                <configuration>                    <!--fork:  如果沒有該項配置,肯呢個devtools不會起作用,即應用不會restart-->                    <fork>true</fork>                </configuration>                <executions>                    <execution>                        <goals>                            <!-- 用於打包jar -->                            <goal>repackage</goal>                        </goals>                    </execution>                </executions>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.5.1</version>                <configuration>                    <source>${jdk.version}</source>                    <target>${jdk.version}</target>                    <encoding>UTF-8</encoding>                </configuration>            </plugin>        </plugins>        <pluginManagement>            <plugins>                <plugin>                    <artifactId>maven-resources-plugin</artifactId>                    <configuration>                        <encoding>utf-8</encoding>                        <useDefaultDelimiters>true</useDefaultDelimiters>                        <nonFilteredFileExtensions>                            <!--防止maven該表認證內的內容-->                            <nonFilteredFileExtension>p12</nonFilteredFileExtension>                            <nonFilteredFileExtension>jks</nonFilteredFileExtension>                        </nonFilteredFileExtensions>                    </configuration>                </plugin>            </plugins>        </pluginManagement>    </build>

如此將包打好後扔於伺服器上,就很方便了:

ssh "chmod +x /data/ops/app/liam-service/bin/app.sh"ssh "/data/ops/app/liam-service/bin/app.sh backup"ssh "/data/ops/app/liam-service/bin/app.sh stop"ssh "/data/ops/app/liam-service/bin/app.sh start"

當然藉助jenkens的話會很方便~

 

java spring boot項目部署-上

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.