springboot轻量部署方案
背景:jar包启动时,由于依赖较多,包过大,重启耗时较多
需求:服务快速启动、资源分类部署
方法:
一、新建一个springboot项目,随便引入一些依赖
二、使用插件(maven-assembly-plugin)
<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <configuration> <finalName>${project.build.finalName}</finalName> <appendAssemblyId>false</appendAssemblyId> <descriptor>src/main/assembly.xml</descriptor><!--指定插件目录--> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
三、配置打包形式、脚本
1、shutdown.sh
#!/bin/sh basepath=$(cd `dirname $0`; pwd) serviceName=`head -n 1 ${basepath}/sname.cfg` processName=$serviceName pkill -f $processName
2、sname.cfg
packagedemo
3、startup.sh
#!/bin/sh #echo "当前目录:" `pwd` basepath=$(cd `dirname $0`; pwd) #echo "脚本文件所在目录:${basepath}" serviceName=`head -n 1 ${basepath}/sname.cfg` processName=$serviceName commandFile=$basepath/$processName if [ ! -f $commandFile ]; then ln -s $JAVA_HOME/bin/java $commandFile fi cd $basepath cd .. #echo "命令文件:${commandFile}" jarFile=`ls *.jar` #echo $jarFile $commandFile -jar $jarFile &
4、assembly.xml
<?xml version="1.0" encoding="UTF-8"?> <assembly> <id>assembly</id> <formats> <format>tar.gz</format> <format>dir</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <fileSet> <directory>src/bin</directory> <!-- 将src/main/bin目录下的文件打包到根目录(/bin)下. --> <outputDirectory>bin</outputDirectory> <fileMode>0755</fileMode> <!-- 0775的权限 随意权限--> </fileSet> <fileSet> <directory>src/main/resources</directory> <outputDirectory>config</outputDirectory> <includes> <include>**</include> </includes> </fileSet> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory /> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> <scope>runtime</scope> <excludes> <exclude>${groupId}:${artifactId}</exclude> </excludes> </dependencySet> </dependencySets> </assembly>
四、maven打包