SpringBoot項目打包+shell腳本部署實踐,太有用了

本篇和大家分享的是springboot打包並結合shell腳本命令部署,重點在分享一個shell程序啟動工具,希望能便利工作;

  • profiles指定不同環境的配置
  • maven-assembly-plugin打發布壓縮包
  • 分享shenniu_publish.sh程序啟動工具
  • linux上使用shenniu_publish.sh啟動程序

profiles指定不同環境的配置

通常一套程序分為了很多個部署環境:開發,測試,uat,線上 等,我們要想對這些環境區分配置文件,可以通過兩種方式:

  • 通過application.yml中編碼指定 profile.active=uat 方式指定
  • 通過mvn中profiles來區分不同環境對應的配置文件夾,人工可以手動在idea勾選生成不同環境的包(推薦)

這裡我們要講的是第二種,首先在mvn中配置如下內容:

<code><profiles>
        <profile>
            node
            <properties>
                
                <activeprofile>node/<activeprofile>
                <package-name>${scripts_packageName}/<package-name>

                <boot-main>${scripts_bootMain}/<boot-main>
            /<properties>
            <activation>
                <activebydefault>true/<activebydefault>
            /<activation>
        /<profile>
        <profile>
            node1
            <properties>
                <activeprofile>node1/<activeprofile>
                <package-name>${scripts_packageName}/<package-name>
                <boot-main>${scripts_bootMain}/<boot-main>
            /<properties>
        /<profile>
        <profile>
            node2
            <properties>
                <activeprofile>node2/<activeprofile>
                <package-name>${scripts_packageName}/<package-name>
                <boot-main>${scripts_bootMain}/<boot-main>
            /<properties>
        /<profile>
    /<profiles>/<code>

節點粗解:

id:用來指定不同環境配置文件所在的目錄,如下我這裡:

SpringBoot項目打包+shell腳本部署實踐,太有用了

properties:該節點中的節點是可作為參數傳遞給其他配置文件,如我這裡的package-name節點值就可以在另外的assembly.xml或者shell腳本文件中通過${package-name}獲取到,如下:

SpringBoot項目打包+shell腳本部署實踐,太有用了

activeByDefault:指定默認環境配置文件夾

maven-assembly-plugin打發布壓縮包

對於springboot程序打包,可以分為jar和war,這裡是jar包;有場景是咋們配置文件或者第三方等依賴包不想放到工程jar中,並且把這些文件壓縮成一個zip包,方便上傳到linux;此時通過maven-assembly-plugin和maven-jar-plugin就可以做到,mvn的配置如:

<code><plugin>
                <groupid>org.apache.maven.plugins/<groupid>
                <artifactid>maven-jar-plugin/<artifactid>
                <version>2.6/<version>
                <configuration>
                    <archive>
                        <addmavendescriptor>false/<addmavendescriptor>
                        <manifest>
                            <addclasspath>true/<addclasspath>
                            <classpathprefix>lib//<classpathprefix>
                            <mainclass>${scripts_bootMain}/<mainclass>
                        /<manifest>
                    /<archive>
                    
                    <excludes>
                        <exclude>**/*.yml/<exclude>
                        <exclude>**/*.properties/<exclude>
                        <exclude>**/*.xml/<exclude>
                        <exclude>**/*.sh/<exclude>
                    /<excludes>
                /<configuration>
                <executions>
                    <execution>
                        make-a-jar
                        <phase>compile/<phase>
                        <goals>
                            <goal>jar/<goal>

                        /<goals>
                    /<execution>
                /<executions>
            /<plugin>

            <plugin>
                <groupid>org.apache.maven.plugins/<groupid>
                <artifactid>maven-assembly-plugin/<artifactid>
                <version>2.4/<version>
                
                <configuration>
                    
                    <descriptors>
                        <descriptor>${project.basedir}/src/main/assembly/assembly.xml/<descriptor>
                    /<descriptors>
                /<configuration>
                <executions>
                    <execution>
                        make-assembly
                        <phase>package/<phase>
                        <goals>
                            <goal>single/<goal>
                        /<goals>
                    /<execution>
                /<executions>
            /<plugin>/<code>

值得注意的地方如下幾點:

  • mainClass節點:用來指定啟動main函數入口類路徑,如這裡的:com.sm.EurekaServerApplication
  • excludes節點:排除主jar包中配置等一些列後綴文件,因為我們要包這些配置文件放到主包外面
  • descriptor節點:用來指定assembly插件對應的assembly.xml配置文件

有了上面mvn配置,我們還需要assembly.xml的配置,這裡提取了結合shell腳本發佈程序的配置:

<code><assembly>          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd
http://maven.apache.org/ASSEMBLY/2.0.0 ">
    ${activeProfile}
    
    <formats>
        <format>zip/<format>
    /<formats>
    
    <includebasedirectory>false/<includebasedirectory>
    <dependencysets>
        <dependencyset>
            
            <useprojectartifact>false/<useprojectartifact>
            <outputdirectory>${package-name}-${activeProfile}/lib/<outputdirectory>
            <unpack>false/<unpack>
        /<dependencyset>
    /<dependencysets>

    <filesets>
        
        <fileset>
            <directory>${project.basedir}/src/main/profiles/${activeProfile}/<directory>
            <outputdirectory>${package-name}-${activeProfile}/conf/<outputdirectory>
            <includes>
                <include>**/*/<include>
                
                

                
            /<includes>
        /<fileset>

        
        <fileset>
            <directory>${project.basedir}/src/main/scripts/<directory>
            <outputdirectory>
            <includes>
                <include>**/*/<include>
            /<includes>
            
            <filemode>777/<filemode>
            
            <directorymode>777/<directorymode>
            
            <filtered>true/<filtered>
        /<fileset>

        
        <fileset>
            <directory>${project.build.directory}/<directory>
            <outputdirectory>${package-name}-${activeProfile}//<outputdirectory>
            <includes>
                <include>*.jar/<include>
            /<includes>
        /<fileset>
    /<filesets>
/<assembly>/<code>

重點節點介紹:

  • formats節點:把配置文件和jar包等壓縮成什麼文件格式,這裡可以有:zip,tar等
  • fileMode節點:指定scripts目錄下腳本文件(這裡是:shenniu_publish.sh)在linux上文件權限為777
  • filtered節點:腳本中參數變量為pom的profiles中properties的值(該配置,是把mvn中屬性值映射生成到sh文件中,如:${package-name})

完成上面配置後,此時我們可以通過idea上勾選切換不同環境來打zip包,如圖:

SpringBoot項目打包+shell腳本部署實踐,太有用了

分享shenniu_publish.sh程序啟動工具

上面步驟完成了zip格式的發佈包,我們再分享下啟動程序的shell腳本,該腳本具有的功能如:

  • 解壓zip+啟動jar包
  • 啟動jar包
  • 停止對應jar運行
  • 重啟jar程序

目前該shell中封裝了兩種啟動jar命令的方式:

  • java -cp
  • java -jar

如圖命令格式:

SpringBoot項目打包+shell腳本部署實踐,太有用了

來看全部的shell代碼:

<code>#!/usr/bin/env bash
#可變參數變量
languageType="javac" #支持 java,javac,netcore 發佈
#參數值由pom文件傳遞
baseZipName="${package-name}-${activeProfile}" #壓縮包名稱  publish-test.zip的publish
packageName="${package-name}" #命令啟動包名 xx.jar的xx
mainclass="${boot-main}" #java -cp啟動時,指定main入口類;命令:java -cp conf;lib\\*.jar;${packageName}.jar ${mainclass}

#例子
# baseZipName="publish-test" #壓縮包名稱  publish-test.zip的publish
# packageName="publish" #命令啟動包名 publish.jar的xx

#固定變量
basePath=$(cd `dirname $0`/; pwd)
baseZipPath="${basePath}/${baseZipName}.zip"  #壓縮包路徑
baseDirPath="${basePath}" #解壓部署磁盤路徑
pid= #進程pid

#解壓
function shenniu_unzip()
{
    echo "解壓---------------------------------------------"
    echo "壓縮包路徑:${baseZipPath}"
    if [ ! `find ${baseZipPath}` ]
    then
        echo "不存在壓縮包:${baseZipPath}"
    else
        echo "解壓磁盤路徑:${baseDirPath}/${baseZipName}"
        echo "開始解壓..."

        #解壓命令
        unzip -od ${baseDirPath}/${baseZipName} ${baseZipPath}

        #設置執行權限

        chmod +x ${baseDirPath}/${baseZipName}/${packageName}

        echo "解壓完成。"  
    fi
}

#檢測pid
function getPid()
{
    echo "檢測狀態---------------------------------------------"
    pid=`ps -ef | grep -n ${packageName} | grep -v grep | awk '{print $2}'`
    if [ ${pid} ] 
    then
        echo "運行pid:${pid}"
    else
        echo "未運行"
    fi
}

#啟動程序
function start()
{
    #啟動前,先停止之前的
    stop
    if [ ${pid} ]
    then
        echo "停止程序失敗,無法啟動"
    else
        echo "啟動程序---------------------------------------------"

        #選擇語言類型
        read -p "輸入程序類型(java,javac,netcore),下一步按回車鍵(默認:${languageType}):" read_languageType
        if [ ${read_languageType} ]
        then
            languageType=${read_languageType}
        fi
        echo "選擇程序類型:${languageType}"

        #進入運行包目錄
        cd ${baseDirPath}/${baseZipName}

        #分類啟動

        if [ "${languageType}" == "javac" ] 
        then
            if [ ${mainclass} ] 
            then
                nohup java -cp conf:lib\\*.jar:${packageName}.jar ${mainclass} >${baseDirPath}/${packageName}.out 2>&1 &
               #nohup java -cp conf:lib\\*.jar:${packageName}.jar ${mainclass} >/dev/null 2>&1 &
            fi
        elif [ "${languageType}" == "java" ] 
        then
            nohup java -jar ${baseDirPath}/${baseZipName}/${packageName}.jar >/dev/null 2>&1 &
            # java -jar ${baseDirPath}/${baseZipName}/${packageName}.jar
        elif [ "${languageType}" == "netcore" ] 
        then
            #nohup dotnet run ${baseDirPath}/${baseZipName}/${packageName} >/dev/null 2>&1 &
            nohup ${baseDirPath}/${baseZipName}/${packageName} >/dev/null 2>&1 &
        fi

        #查詢是否有啟動進程
        getPid
        if [ ${pid} ]
        then
            echo "已啟動"
            #nohup日誌
            tail -n 50 -f ${baseDirPath}/${packageName}.out
        else
            echo "啟動失敗"
        fi
    fi
}

#停止程序
function stop()
{
    getPid
    if [ ${pid} ] 
    then
        echo "停止程序---------------------------------------------"
        kill -9 ${pid}

        getPid
        if [ ${pid} ] 
        then
            #stop
            echo "停止失敗"
        else
            echo "停止成功"
        fi

    fi
}

#啟動時帶參數,根據參數執行
if [ ${#} -ge 1 ] 
then
    case ${1} in
        "start") 
            start
        ;;
        "restart") 
            start
        ;;
        "stop") 
            stop
        ;;
        "unzip") 
            #執行解壓
            shenniu_unzip
            #執行啟動
            start
        ;;
        *) 
            echo "${1}無任何操作"
        ;;
    esac
else
    echo "
    command如下命令:
    unzip:解壓並啟動
    start:啟動
    stop:停止進程
    restart:重啟

    示例命令如:./shenniu_publish start
    "
fi/<code>

正如上面小節說的,shell中的參數 package-name,activeProfile,boot-main 都是由mvn中profiles的properties中提供,是可變的參數,腳本代碼本身不需要人工去修改,只需要變的是mvn的參數即可;其實在我們生成zip包的時候,shell中的參數就被替換了,可以看zip中shell文件內容如:

SpringBoot項目打包+shell腳本部署實踐,太有用了

linux上使用shenniu_publish.sh啟動程序

把生成的zip上傳到linux上,通過命令解壓:

<code>unzip -od eureka-server-0.0.1-node eureka-server-0.0.1-node.zip/<code>

其實shell腳本中包含有解壓命令,但是我在打包時放在了zip中,所以只能通過手動解壓了,當然可以調整;此時進入加壓目錄如此:

SpringBoot項目打包+shell腳本部署實踐,太有用了

注:這裡第一次執行./shenniu_publish.sh腳本時候,提示了錯誤信息;是由於我是在windows上編輯的這個腳本,其空格等和linux上不一樣,所以運行會有問題,要解決可以使用vim命令在linux把該文件轉成linux格式,如下命令:

<code>vim shenniu_publish.sh
set ff=unix
:wq/<code>

執行完後,再來運行腳本./shenniu_publish.sh,此時有如下提示:

SpringBoot項目打包+shell腳本部署實踐,太有用了

此刻我們文件是解壓狀態,因此只需要start命令啟動程序即可:

SpringBoot項目打包+shell腳本部署實踐,太有用了

到這裡shenniu_publish.sh腳本使用就完成了,只要腳本沒有提示錯誤,基本都能啟動jar服務;其他restart和stop命令也如此執行就行:

SpringBoot項目打包+shell腳本部署實踐,太有用了

原文鏈接:https://mp.weixin.qq.com/s/sPtAg9-eXbVTfcMbc3FsmA


分享到:


相關文章: