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</exclude>
                    <exclude>**/*.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>

效果圖

在這裏插入圖片描述

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 &
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章