SpringBoot確實方便了我們很多,內置的tomcat,靈活的配置等等,但是,由於他使用jar包部署的話,也會達成一個很大的jar包,一旦上線,上傳就特別耗時了,而且還有一個問題就是,如果生產環境同時有多人更新,也會造成衝突(雖然可能性很少),但是如果採取增量式部署,也會減少這個可能性。
當然上述問題我們也可以使用war包方式,可是,我感覺那個外置的tomcat也很麻煩,還需要配置很多東西,SpringBoot都已經內置了tomcat了,何必還需要使用外置的呢
所以今天的目標是:SpringBoot增量式部署+使用內置tomcat
第一步:打包
我們使用的是maven來管理我們的jar包
不多說,直接上配置文件
<build>
<finalName>umf-wechat-core</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source><!-- 源代碼使用的JDK版本 -->
<target>8</target><!-- 需要生成的目標class文件的編譯版本 -->
<encoding>UTF-8</encoding><!-- 字符集編碼 -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests><!--跳過測試-->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<!-- 處理依賴jar包 -->
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- 依賴包輸出目錄,將來不打進jar包裏 -->
<outputDirectory>${project.build.directory}/${project.name}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<!--打成jar包後複製到的路徑-->
<outputDirectory>
${project.build.directory}/${project.name}/script
</outputDirectory>
<resources>
<resource>
<!--項目中的路徑-->
<directory>script</directory>
</resource>
</resources>
</configuration>
</execution>
<!--可配置多個提取複製路徑只需要 “<id>”名字不一樣即可-->
<execution>
<id>copy-bulid</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<outputDirectory>
${project.build.directory}/${project.name}/bin
</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/classes</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 排除不想打包-->
<excludes>
<!--注意從編譯結果目錄開始算目錄結構-->
<exclude>/sercurity/testUmpay.key.p8</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
上面包括了build的信息,因爲我們也只需要這些編譯時期的操作,通過這些操作,我們就可以在我們的target目錄下有一個項目名的文件夾,這個文件夾裏面的東西,就是我們所需要的所有的東西了
第二步:啓動腳本
啓動腳本分爲三個文件,三個文件統一在我們的script目錄下
class_path
para
core.sh
class_path:這個文件裏面存放了所有我們需要的jar包,由於我們不是使用 java -jar 啓動項目,所以我們需要指定我們的lib目錄
para:這個存放了我們所需要的所有常量信息
core.sh:這個是我們的啓動,停止,重啓,檢查狀態等腳本集合
class_path:
. para
APPCLASSPATH=
APPCLASSPATH=$APPCLASSPATH:${SERVICE_BIN}
APPCLASSPATH=$APPCLASSPATH:${SERVICE_LIB}/spring-boot-2.1.10.RELEASE.jar
APPCLASSPATH=$APPCLASSPATH:${SERVICE_LIB}/spring-boot-autoconfigure-2.1.10.RELEASE.jar
APPCLASSPATH=$APPCLASSPATH:${SERVICE_LIB}/spring-boot-starter-2.1.10.RELEASE.jar
#export APPCLASSPATH
中間省略n多jar包
para:
#!/bin/bash
SERVICE_ID=core
SERVICE_NAME=core
SERVICE_DIR=/usr/mpsp/${SERVICE_NAME}
SERVICE_BIN=${SERVICE_DIR}/bin
SERVICE_LIB=${SERVICE_DIR}/lib
SERVICE_LOG=${SERVICE_DIR}/log
SERVICE_CLASS=com.xxx.xxx
SERVICE_SPRING_PROFILES_ACTIVE=dev
JAVA_HOME=/usr/local/java/jdk1.8.0_241
PATH=$JAVA_HOME/bin:$PATH
其中需要改動的有:SERVICE_ID 和 SERVICE_NAME 修改爲你的項目名;SERVICE_CLASS 修改爲你的啓動類; JAVA_HOME 這個應該不用說了
core.sh
#! /bin/bash
export LANG=en_US.UTF-8
clear
. para
. class_path
#使用說明,用來提示輸入參數
usage() {
echo "Usage: sh 執行腳本.sh [start|stop|restart|status]"
exit 1
}
#檢查程序是否在運行
is_exist(){
pid=`ps -ef|grep $SERVICE_ID|grep -v grep|awk '{print $2}' `
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#啓動方法
start(){
is_exist
if [ $? -eq "0" ]; then
echo "${SERVICE_ID} is already running. pid=${pid} ."
else
cd ${SERVICE_BIN}
sleep 1
nohup java -Xms258m -Xmx512m -Dflag=${SERVICE_ID} -Djava.security.policy="policy.txt" -Djava.rmi.server.codebase=file://${SERVICE_BIN}/ -cp ${APPCLASSPATH} ${SERVICE_CLASS} --spring.profiles.active=${SERVICE_SPRING_PROFILES_ACTIVE} >${SERVICE_LOG}/console.out 2>&1 &
sleep 1
fi
status
tail -300f ${SERVICE_LOG}/console.out
}
#停止方法
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${SERVICE_ID} is not running"
fi
status
}
#輸出運行狀態
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${SERVICE_ID} is running. Pid is ${pid}"
else
echo "${SERVICE_ID} is NOT running."
fi
}
#重啓
restart(){
stop
start
}
#根據輸入參數,選擇執行對應方法,不輸入則執行使用說明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
該文件爲啓動腳本,內容可由各自需求任意更改,名稱隨意更改
至此 結束!!!! so easy
途中遇見的坑:
1.加載不了主類 Error: Could not find or load main class xxx:
原因:class_path 問題,可能由於編譯器問題,導致沒有拼接成功,所以導致沒有加載好lib,出現這個問題!!!
2.啓動失敗,nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sequenceService':
原因:還是class_path 由於不細心,導致少放了一個jar包,好像是commom-io或者netty的,導致啓動失敗,對比過後發現問題,修改過後,啓動成功。