您现在的位置是:首页 >学无止境 >【工具篇】SpringBoot基于assembly的服务化打包方案网站首页学无止境

【工具篇】SpringBoot基于assembly的服务化打包方案

光阴迷客 2024-05-09 00:00:02
简介【工具篇】SpringBoot基于assembly的服务化打包方案

简介

最近项目中,使用插件式开发方式,将多个Web应用打成一个FatJar程序包部署运行。但考虑到原始裸Jar包方式交付,有很多不便之处,比如启动命令过长(JVM参数配置、Spring环境配置等)、配置无法修改等问题会造成很大的运维负担。

针对上述问题,我们要去做服务化和工程化,主要有两点考虑:

  • 让SpringBoot能够加载jar外的配置文件
  • 提供一系列服务化的运维脚本

通过assemblySpring Boot项目进行服务化打包,便能解决上面提到的两个问题。

服务化打包过程

Nacos效果图

这里先来看下Nacos项目使用assemblySpringBoot服务化打包后的效果图。

`-- nacos
    |-- LICENSE
    |-- NOTICE
    |-- bin
    |   |-- shutdown.cmd
    |   |-- shutdown.sh
    |   |-- startup.cmd
    |   `-- startup.sh
    |-- conf
    |   |-- 1.4.0-ipv6_support-update.sql
    |   |-- announcement.conf
    |   |-- application.properties
    |   |-- application.properties.example
    |   |-- cluster.conf.example
    |   |-- derby-schema.sql
    |   |-- mysql-schema.sql
    |   `-- nacos-logback.xml
    `-- target
        `-- nacos-server.jar

打包过程

引入assembly插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <!-- zip包文件名不加上assembly.xml中配置的id属性值 -->
        <appendAssemblyId>false</appendAssemblyId>
        <descriptors>
        	<!--具体的配置文件-->
            <descriptor>release-nacos.xml</descriptor>
        </descriptors>
        <tarLongFileMode>posix</tarLongFileMode>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <!--绑定到maven操作类型上-->
            <phase>install</phase>
            <!--运行一次-->
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

一般情况下,assembly的配置文件项目根目录下,和pom文件平级,在项目中的大致结构图如下:
在这里插入图片描述

assembly.xml配置

assembly的配置内容,可参照下面配置,基本包括打包服务脚本、FatJar、配置文件等。从下面的代码中可以发现,项目配置文件打到了conf目录下,这样就实现了配置文件外置的目标。

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <id>server-${project.version}</id>
    <!--是否包装一层-->
    <includeBaseDirectory>true</includeBaseDirectory>
    <!-- 打包的类型,如果有N个,将会打N个类型的包 -->
    <formats>
        <format>dir</format>
        <format>tar.gz</format>
        <format>zip</format>
    </formats>
    <!--文件设置-->
    <fileSets>
        <fileSet>
            <includes>
                <include>plugins/**</include>
            </includes>
        </fileSet>

        <fileSet>
            <includes>
                <include>conf/**</include>
            </includes>
        </fileSet>
		<!--
            0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限;
            0644->即用户具有读写权限,组用户和其它用户具有只读权限;
        -->
        <fileSet>
            <includes>
                <include>bin/*</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <source>LICENSE-BIN</source>
            <destName>LICENSE</destName>
        </file>
        <file>
            <source>NOTICE-BIN</source>
            <destName>NOTICE</destName>
        </file>
        <file>
            <!--打好的jar包名称和放置目录-->
            <source>../console/target/nacos-server.jar</source>
            <outputDirectory>target/</outputDirectory>
        </file>
    </files>

    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>com.alibaba.nacos:nacos-console</include>
            </includes>
        </moduleSet>
    </moduleSets>
</assembly>

项目展开示意图如下:
在这里插入图片描述
对于不同的Springboot项目,运维脚本可参考Nacos的进行定制修改,为了节约篇幅这里就不再赘述。

打包后日志&配置加载

有兴趣的同学,可以仔细看一下Nacos的startup.sh脚本,是如何将外置配置文件生效的,其实就是通过Program Arguments的方式传入的:

JAVA_OPT="${JAVA_OPT} --spring.config.additional-location=${BASE_DIR}/conf"
JAVA_OPT="${JAVA_OPT} --logging.config=${BASE_DIR}/conf/nacos-logback.xml"

maven-assembly详解

参见:maven–插件篇(assembly插件)

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。