公司用了六年的 SpringBoot 项目部署方案,稳得一批!

本篇和大家分享的是springboot打包并结合shell脚本命令部署 , 重点在分享一个shell程序启动工具,希望能便利工作 。

  • profiles指定不同环境的配置
  • maven-assembly-plugin打发布压缩包
  • 分享shenniu_publish.sh程序启动工具
  • linux上使用shenniu_publish.sh启动程序

公司用了六年的 SpringBoot 项目部署方案,稳得一批!

文章插图
profiles指定不同环境的配置通常一套程序分为了很多个部署环境:开发,测试,uat,线上 等,我们要想对这些环境区分配置文件,可以通过两种方式:
  • 通过Application.yml中编码指定 profile.active=uat 方式指定
  • 通过mvn中profiles来区分不同环境对应的配置文件夹,人工可以手动在idea勾选生成不同环境的包(推荐)
这里我们要讲的是第二种,首先在mvn中配置如下内容:
<profiles><profile><id>node</id><properties><!--传递给脚本的参数值--><activeProfile>node</activeProfile><package-name>${scripts_packageName}</package-name><boot-mAIn>${scripts_bootMain}</boot-main></properties><activation><activeByDefault>true</activeByDefault></activation></profile><profile><id>node1</id><properties><activeProfile>node1</activeProfile><package-name>${scripts_packageName}</package-name><boot-main>${scripts_bootMain}</boot-main></properties></profile><profile><id>node2</id><properties><activeProfile>node2</activeProfile><package-name>${scripts_packageName}</package-name><boot-main>${scripts_bootMain}</boot-main></properties></profile></profiles>节点粗解:
  • id: 用来指定不同环境配置文件所在的目录,如下我这里:

公司用了六年的 SpringBoot 项目部署方案,稳得一批!

文章插图
  • properties: 该节点中的节点是可作为参数传递给其他配置文件,如我这里的package-name节点值就可以在另外的assembly.xml或者shell脚本文件中通过${package-name}获取到,如下:

公司用了六年的 SpringBoot 项目部署方案,稳得一批!

文章插图
  • activeByDefault: 指定默认环境配置文件夹
maven-assembly-plugin打发布压缩包对于springboot程序打包,可以分为jar和war,这里是jar包;有场景是咋们配置文件或者第三方等依赖包不想放到工程jar中,并且把这些文件压缩成一个zip包,方便上传到linux;此时通过maven-assembly-plugin和maven-jar-plugin就可以做到,mvn的配置如:
<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><id>make-a-jar</id><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><!-- The configuration of the plugin --><configuration><!-- Specifies the configuration file of the assembly plugin --><descriptors><descriptor>${project.basedir}/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>值得注意的地方如下几点: