在使用assembly來打包springboot微服務項目前,我想說一說,目前springboot項目的幾種常見的部署方式。vue
本文主要針對第二種部署方式提供一種更加友好的打包方案,是部署管理更加輕鬆,第一種方式可能將來我會在本身博客中寫。java
最近我看到一個項目團隊,他們在採用springboot開發完項目構建交互給運維團隊就是一個spring boot 的fatjar。並且這種原始打出的包在傳統型項目開發公司,對於運維人員來講無疑是很致命的,項目交付後整個配置文件都被隱藏到打成的jar中,針對不一樣的環境修改配置文件就變成了一件很困難的事情。所以,咱們在公司引入任何新技術時,必定要考慮怎麼去作服務化和工程化,若是僅僅引用技術框架,不少時候可能只須要加入幾個依賴,看下api寫幾行代碼就能跑起來。linux
針對上面的這種問題,要去作服務化和工程化,大體要解決兩點問題:git
這裏先來看下使用assembly將springboot服務化打包後的效果圖。github
下面是打包springboot的詳細步驟。spring
<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <configuration> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
從上面代碼看出了我把assembly的配置放在main目錄下,這個是習慣,能夠不放這裏也能夠,下面就是一個assembly在項目中的大體結構圖:docker
assembly的配置不一樣的應用和下面配置也差很少,無非就是打包服務腳本、jar、配置文件等。從下面的代碼中config配置就會發現, assembly將配置文件打到了config下。shell
<assembly> <id>1.0</id> <formats> <format>tar.gz</format> </formats> <fileSets> <fileSet> <directory>src/main/assembly/bin</directory> <outputDirectory>bin</outputDirectory> <fileMode>0755</fileMode> </fileSet> <fileSet> <directory>src/main/assembly/config</directory> <outputDirectory>config</outputDirectory> <fileMode>0644</fileMode> </fileSet> <fileSet> <directory>target</directory> <outputDirectory>lib</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <fileSet> <directory>src/main/resources</directory> <outputDirectory>logs</outputDirectory> <fileMode>0755</fileMode> <excludes> <exclude>**/*</exclude> </excludes> </fileSet> </fileSets> </assembly>
如今寫linux環境的腳本。 windows
第一個:start.sh啓動腳本api
#!/bin/bash SERVER_NAME='spring-vue' # jar名稱 JAR_NAME='springboot-vue.jar' cd `dirname $0` BIN_DIR=`pwd` cd .. DEPLOY_DIR=`pwd` CONF_DIR=$DEPLOY_DIR/config # SERVER_PORT=`sed '/server.port/!d;s/.*=//' config/application.properties | tr -d '\r'` # 獲取應用的端口號 SERVER_PORT=`sed -nr '/port: [0-9]+/ s/.*port: +([0-9]+).*/\1/p' config/application.yml` PIDS=`ps -f | grep java | grep "$CONF_DIR" |awk '{print $2}'` if [ "$1" = "status" ]; then if [ -n "$PIDS" ]; then echo "The $SERVER_NAME is running...!" echo "PID: $PIDS" exit 0 else echo "The $SERVER_NAME is stopped" exit 0 fi fi if [ -n "$PIDS" ]; then echo "ERROR: The $SERVER_NAME already started!" echo "PID: $PIDS" exit 1 fi if [ -n "$SERVER_PORT" ]; then 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 LOGS_DIR=$DEPLOY_DIR/logs if [ ! -d $LOGS_DIR ]; then mkdir $LOGS_DIR fi STDOUT_FILE=$LOGS_DIR/stdout.log 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=`java -version 2>&1 | grep -i 64-bit` if [ -n "$BITS" ]; then JAVA_MEM_OPTS=" -server -Xmx512m -Xms512m -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 " else JAVA_MEM_OPTS=" -server -Xms512m -Xmx512m -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC " fi CONFIG_FILES=" -Dlogging.path=$LOGS_DIR -Dlogging.config=$CONF_DIR/log4j2.xml -Dspring.config.location=$CONF_DIR/application.properties " echo -e "Starting the $SERVER_NAME ..." nohup java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $CONFIG_FILES -jar $DEPLOY_DIR/lib/$JAR_NAME > $STDOUT_FILE 2>&1 & COUNT=0 while [ $COUNT -lt 1 ]; do echo -e ".\c" sleep 1 if [ -n "$SERVER_PORT" ]; then COUNT=`netstat -an | grep $SERVER_PORT | wc -l` else COUNT=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}' | wc -l` fi if [ $COUNT -gt 0 ]; then break fi done echo "OK!" PIDS=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'` echo "PID: $PIDS" echo "STDOUT: $STDOUT_FILE"
腳本用例:
# 啓動應用 ./start.sh # 以debug方式啓動 ./start debug # 啓動任務並開啓jmx監控 ./start jmx # 獲取當前的運行狀態 ./start status
中止腳本:stop.sh
#!/bin/bash cd `dirname $0` BIN_DIR=`pwd` cd .. DEPLOY_DIR=`pwd` CONF_DIR=$DEPLOY_DIR/config SERVER_NAME=$DEPLOY_DIR PIDS=`ps -ef | grep java | 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 $BIN_DIR/dump.sh fi echo -e "Stopping the $SERVER_NAME ...\c" for PID in $PIDS ; do kill $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 -f -p $PID | grep java` if [ -n "$PID_EXIST" ]; then COUNT=0 break fi done done echo "OK!" echo "PID: $PIDS"
windows環境的啓動腳本:
echo off set APP_NAME=springboot-vue.jar set CONFIG= -Dlogging.path=../logs -Dlogging.config=../config/log4j2.xml -Dspring.config.location=../config/application.yml set DEBUG_OPTS= if ""%1"" == ""debug"" ( set DEBUG_OPTS= -Xloggc:../logs/gc.log -verbose:gc -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=../logs goto debug ) set JMX_OPTS= if ""%1"" == ""jmx"" ( set JMX_OPTS= -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9888 -Dcom.sun.management.jmxremote.ssl=FALSE -Dcom.sun.management.jmxremote.authenticate=FALSE goto jmx ) echo "Starting the %APP_NAME%" java -Xms512m -Xmx512m -server %DEBUG_OPTS% %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME% goto end :debug echo "debug" java -Xms512m -Xmx512m -server %DEBUG_OPTS% %CONFIG% -jar ../lib/%APP_NAME% goto end :jmx java -Xms512m -Xmx512m -server %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME% goto end :end pause
對於不一樣的springboot項目,只須要適當修改一下腳本就能夠了,爲了節約篇幅這裏就不列出其餘的腳本了,能夠參考我提交的demo:https://github.com/Shalousun/springboot-vue.git,對於demo若有疑問可添加該羣:170651381
ps:以上腳本參考自dubbo官方。其實對於dubbo項目的輕量化構建也是相似的
重點:上面講了這麼多腳本,其實每次建立項目添加一堆assembly的配置也是比較麻煩的,並且須要去修改腳本中的jar名稱,修改的不一致還可能出錯,所以能夠採用本人開源的項目框架搭建腳手架https://gitee.com/sunyurepository/ApplicationPower來自動建立這些配置而後複製到您的項目中,您就只須要安安心心寫寫業務代碼。
在第二節的圖中能夠看到打包的應用日誌通常統一輸出到logs目錄中,可是對於不一樣的系統平臺,雖然配置的日誌輸出路徑是同樣的,可是最後不必定輸出到logs中。通過測試在windows平臺中使用相對的日誌路徑../logs是沒有問題的,可是對於linux系統下使用相對路徑就不能輸出到logs下,所以建議在linux平臺下就寫絕對路徑吧。不過在我提供的腳本中設置輸出日誌的路徑
-Dlogging.path=../logs
所以結合log4j2的強大解析能力徹底能夠設置log42的日誌路徑(開發時則手動指定路徑):
<property name="LOG_HOME">${sys:logging.path}</property>
可是對於springboot應用的訪問日誌在linux下彷佛只能使用絕對路徑了。
# server config server: port: 8080 undertow: accesslog: enabled: true dir: /usr/xxx/logs logging: path: /usr/xxx/logs
固然後面有使用配置解決的同窗能夠提醒糾正下。
總結:這個方案自己並無帶來什麼新東西,甚至腳本大多數是參考了dubbo官方的腳本,只是在上面作了些完善。可是重要的一點是怎麼去根據實際的技術應用場景,思考使用這項技術須要作的服務化和工程化。