【微服务】SpringBoot服务瘦身部署

(内容学习于up主"编程不良人")

SpringBoot瘦身实战

什么是jar瘦身

SpringBoot 应用瘦身顾名思议:就是将 SpringBoot 应用打包的 jar 利用合理的方式、方法减小体积。

为什么瘦身

SpringBoot虽然很方便就能搭建起来一个服务,带来的问题就是jar包很大。
这是自己搭建的微服务项目,file服务中,总代码文件不到10个,打包出来的jar已经快80M。
实际项目中打包出来的jar,已经达130M+。
【微服务】SpringBoot服务瘦身部署_第1张图片

虽然使用jenkins自动化部署已经很方便了,但如果jenkins与目标服务器不是在同一内网下,传输还是很慢。

瘦身部署实战

修改spring-boot-maven-plugin配置

  <build>
        <resources>
            <resource>
                <directory>src/main/resources/directory>
                <includes>
                    <include>**/*.ymlinclude>
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                    <include>**/*.htmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
            <resource>
                <directory>src/main/resources/directory>
                <filtering>falsefiltering>
                <excludes>
                    <exclude>**/*.ymlexclude>
                    <exclude>**/*.xmlexclude>
                    <exclude>**/*.propertiesexclude>
                excludes>
            resource>
        resources>
        <plugins>
            <plugin>
                <artifactId>maven-resources-pluginartifactId>
                <version>3.1.0version>
                <configuration>
                    <encoding>utf-8encoding>
                    <resources>
                        <resource>
                            <directory>src/main/resources/directory>
                            <filtering>truefiltering>
                        resource>
                    resources>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.8.1version>
                <configuration>
                    <target>1.8target>
                    <source>1.8source>
                    <encoding>utf-8encoding>
                configuration>
            plugin>

            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>2.2.4.RELEASEversion>
                <configuration>
                    <mainClass>pers.kw.CrmServermainClass>
                    <includeSystemScope>trueincludeSystemScope>
                    <fork>truefork>
                    
                    <jvmArguments>-Dfile.encoding=UTF-8jvmArguments>
                    <layout>
                        ZIP
                    layout>
                    <includes>
                        
                        <include>
                            <groupId>non-existsgroupId>
                            <artifactId>non-existsartifactId>
                        include>
                    includes>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>

导出依赖包

方式一 mvn插件

增加maven-dependency-plugin插件配置


<plugin>
  <groupId>org.apache.maven.pluginsgroupId>
  <artifactId>maven-dependency-pluginartifactId>
  <version>3.1.1version>
  <executions>
    <execution>
      <id>copy-dependenciesid>
      <phase>packagephase>
      <goals>
        <goal>copy-dependenciesgoal>
      goals>
      <configuration>
        <outputDirectory>${project.build.directory}/liboutputDirectory>
        
        <excludeTransitive>falseexcludeTransitive>
        
        <stripVersion>falsestripVersion>
        
        <includeScope>runtimeincludeScope>
      configuration>
    execution>
  executions>
plugin>

方式二 mvn命令

mvn dependency:copy-dependencies -DoutputDirectory=指定输出目录

测试运行

前台启动

java -jar -Dloader.path="./lib" kw-crm-service-1.0.0.jar

【微服务】SpringBoot服务瘦身部署_第2张图片

后台启动

nohup java -jar -Dloader.path="./lib" kw-crm-service-1.0.0.jar > /dev/null 2>&1 &

ps: 在mac中使用nohup命令,不知为何自动退出了。。。
在linux中测试成功

补充:如果是docker部署,则需要把lib进行挂载

你可能感兴趣的:(微服务架构,微服务,spring,boot,运维)