在使用assembly來打包springboot微服務項目前,我想說一說,目前springboot項目的幾種常見的部署方式。
使用docker容器去部署,將springboot的應用構建成一個docker image,然后通過容器去啟動鏡像 ,這種方式在需要部署大規模的應用和應用擴展時是非常方便的,屬于目前工業級的部署方案,但是需要掌握docker的生態圈技術。
使用fatjar直接部署啟動,這是很多初學者或者極小規模情況下的一個簡單應用部署方式。
本文主要針對第二種部署方式提供一種更加友好的打包方案,是部署管理更加輕松,第一種方式可能未來我會在自己博客中寫。
一、為什么要將springboot服務化打包 ?
最近我看到一個項目團隊,他們在采用springboot開發完項目構建交互給運維團隊就是一個spring boot 的fatjar。而且這種原始打出的包在傳統型項目開發公司,對于運維人員來說無疑是很致命的,項目交付后整個配置文件都被隱藏到打成的jar中,針對不同的環境修改配置文件就變成了一件很困難的事情。因此,我們在公司引入任何新技術時,一定要考慮怎么去做服務化和工程化,如果僅僅引用技術框架,很多時候可能只需要加入幾個依賴,看下api寫幾行代碼就能跑起來。
針對上面的這種問題,要去做服務化和工程化,大致要解決兩點問題:
讓springboot能夠加載jar外的配置文件。
提供一個服務化的啟動腳本,這個腳本一般是shell或者windows下的bat ,有了springboot的應用服務腳本后,就可以容器的去啟動和停止springboot的應用了。
二、打包后的springboot應用結構圖
這里先來看下使用assembly將springboot服務化打包后的效果圖。
三、服務化打包重要步驟
下面是打包springboot的詳細步驟。
3.1 加入assembly打包插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<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在項目中的大致結構圖:
3.2 assembly.xml配置
assembly的配置不同的應用和下面配置也差不多,無非就是打包服務腳本、jar、配置文件等。從下面的代碼中config配置就會發現, assembly將配置文件打到了config下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<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> |
3.3 編寫服務腳本
現在寫linux環境的腳本。
第一個:start.sh啟動腳本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
#!/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環境的啟動腳本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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
注意:以上腳本參考自dubbo官方。其實對于dubbo項目的輕量化構建也是類似的。
四、打包后應用的日志路徑處理
在第二節的圖中可以看到打包的應用日志一般統一輸出到logs目錄中,但是對于不同的系統平臺,雖然配置的日志輸出路徑是一樣的,但是最后不一定輸出到logs中。經過測試在windows平臺中使用相對的日志路徑../logs是沒有問題的,但是對于linux系統下使用相對路徑就不能輸出到logs下,因此建議在linux平臺下就寫絕對路徑吧。不過在我提供的腳本中設置輸出日志的路徑
1
|
-dlogging.path=../logs |
因此結合log4j2的強大解析能力完全可以設置log42的日志路徑:
1
|
<property name= "log_home" >${sys:logging.path}</property> |
但是對于springboot應用的訪問日志在linux下似乎只能使用絕對路徑了。
1
2
3
4
5
6
7
8
9
|
# server config server: port: 8080 undertow: accesslog: enabled: true dir: /usr/xxx/logs logging: path: /usr/xxx/logs |
當然后面有使用配置解決的同學可以提醒糾正下。
總結:
這個方案本身并沒有帶來什么新東西,甚至腳本大多數是參考了dubbo官方的腳本,只是在上面做了些完善。但是重要的一點是怎么去根據實際的技術應用場景,思考使用這項技術需要做的服務化和工程化。
以上所述是小編給大家介紹的springboot基于assembly的服務化打包方案,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://my.oschina.net/u/1760791/blog/1587996