springboot之瘦身部署

1. 什么是瘦身部署,有什么好处

springboot的瘦身部署就是将外部依赖库(jar包)和配置文件等静态资源打包分离,启动时可以指定资源路径。

优势:

  1. 很大程度上减少项目jar包,可以提高升级资源传输速度,尤其是云服务。
  2. 配置等静态资源变动不用重新打包,可以直接替换资源重启
  3. 不用再分环境打包,在启动时指定环境启动

2. 如何实现打包分离

<build>
    <!-- jar包名 -->
    <finalName>${project.artifactId}-${project.version}-dev</finalName>
    <plugins>
        <!-- 分离lib -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <!-- 依赖包输出目录,将来不打进jar包里 -->
                        <outputDirectory>${project.build.directory}/lib-settle</outputDirectory>
                        <excludeTransitive>false</excludeTransitive>
                        <stripVersion>false</stripVersion>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- copy资源文件 -->
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                            </resource>
                        </resources>
                        <outputDirectory>${project.build.directory}/resources-settle</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- 打jar包时忽略配置文件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>**/*.yml
                    **/*.xml</exclude>
                </excludes>
            </configuration>
        </plugin>
        <!-- spring boot repackage -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <layout>ZIP</layout>
                <includes>
                    <include>
                        <groupId>com.yuantiaokj</groupId>
                        <artifactId>pay-centre-common</artifactId>
                    </include>
                    <include>
                        <groupId>com.yuantiaokj.module</groupId>
                        <artifactId>mail-module</artifactId>
                    </include>
                    <include>
                        <groupId>com.yuantiaokj.module</groupId>
                        <artifactId>excel-module</artifactId>
                    </include>
                    <include>
                        <groupId>com.yuantiaokj</groupId>
                        <artifactId>paycentre-api-my</artifactId>
                    </include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!--不执行test-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

效果图

springboot之瘦身部署_第1张图片

3. 如何部署

将 lib,resources,jar上传服务

启动命令,指定资源

nohup java -Xms128m -Xmx128m -Dloader.path=lib-settle,resources-settle -Dspring.config.location=resources-settle/application-dev.yml   -Dlogging.config=resources-settle/logback-spring.xml  -jar paycentre-settle-6110-2.1-dev.jar  >>/data/logs/paycentre/paycentre-settle-6110.log 2>&1 &

你可能感兴趣的:(java技术)