SpringBoot、微服务瘦身实战

  • 预期效果

    依赖分离
  • 实现方式

    • 在项目中配置插件

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      <build>
      <finalName>file-upload</finalName>
      <plugins>
      <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
      <mainClass>com.example.BaizhiFileAapplication</mainClass>
      <!-- 解决 windows 命令行窗口中文乱码 -->
      <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
      <layout>ZIP</layout>
      <!-- 配置需要打包进项目的 jar -->
      <includes>

      <include>
      <groupId>non-exists</groupId>
      <artifactId>no-exists</artifactId>
      </include>
      </includes>
      <!-- 排除的依赖 -->
      <excludes>
      <exclude>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      </exclude>
      </excludes>
      </configuration>
      </plugin>
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>3.1.1</version>
      <executions>
      <execution>
      <id>copy-dependencies</id>
      <phase>package</phase>
      <goals>
      <goal>copy-dependencies</goal>
      </goals>
      <configuration>
      <!-- 在编译输出目录下新建目录 lib -->
      <outputDirectory>${project.build.directory}/lib</outputDirectory>
      <!-- 是否排除传递性 -->
      <excludeTransitive>false</excludeTransitive>
      <!-- 是否去掉 jar 包版本信息 -->
      <stripVersion>false</stripVersion>
      <!-- 包含范围 -->
      <includeScope>runtime</includeScope>
      </configuration>
      </execution>
      </executions>
      </plugin>
      </plugins>
      </build>
  • 运行方式

    1
    java -jar -Dloader.path=./lib file-upload.jar
    启动测试
  • 分析

    主要将第三方依赖抽离出去,只将自己的代码进行了打包,在其他第三方依赖没有过多变化下,上传部署时,传输的包将尽可能地小,因为只改变了自己缩写代码的部分,第三方依赖是抽离地