Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpringBoot系列之—瘦身部署 #41

Open
johnnian opened this issue Nov 12, 2017 · 9 comments
Open

SpringBoot系列之—瘦身部署 #41

johnnian opened this issue Nov 12, 2017 · 9 comments

Comments

@johnnian
Copy link
Owner

johnnian commented Nov 12, 2017

一、前言

SpringBoot部署起来虽然简单,如果服务器部署在公司内网,速度还行,但是如果部署在公网(阿里云等云服务器上),部署起来实在头疼: 编译出来的 Jar 包很大,如果工程引入了许多开源组件(SpringCloud等),那就更大了。

这个时候如果想要对线上运行工程有一些微调,则非常痛苦, :(

二、瘦身前的Jar包

Tomcat在部署Web工程的时候,可以进行增量更新,SpringBoot也是可以的~

SpringBoot编译出来的Jar包中,磁盘占用大的,是一些外部依赖库(jar包),例如:

进入项目工程根目录,执行 mvn clean install 命令,得到的Jar包,用压缩软件打开,目录结构如下:

qq20171112-191453 2x

整个Jar包 18.18 MB, 但是 BOOT-INF/lib 就占用了将近 18 MB:

qq20171112-192041 2x

三、解决方法

步骤1: 正常编译JAR包,解压出lib文件夹

POM文件如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
		        <mainClass>com.johnnian.App</mainClass>
		        <layout>ZIP</layout>
            </configuration>
            <executions>
		    <execution>
		         <goals>
		             <goal>repackage</goal>
		         </goals>
		     </execution>
           </executions>
        </plugin>
     <plugins>
<build>

进入项目根目录,执行命令: mvn clean install

将编译后的Jar包解压,拷贝 BOOT-INF 目录下的lib文件夹 到目标路径;

步骤2: 修改pom.xml配置,编译出不带 lib 文件夹的Jar包

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
		        <mainClass>com.johnnian.App</mainClass>
		        <layout>ZIP</layout>
		        <includes> 
			        <include>
			            <groupId>nothing</groupId>
			            <artifactId>nothing</artifactId>
			        </include>  
			    </includes>
		    </configuration>
		    <executions>
		        <execution>
		            <goals>
		                <goal>repackage</goal>
		            </goals>
		        </execution>
		    </executions>
        </plugin>
     <plugins>
<build>

配置完成后,再次执行编译:mvn clean install

生成的 Jar 包体积明显变小,如下所示, 外部的 jar 包已经不会被引入了:

qq20171112-193001 2x

步骤3: 运行编译后的Jar包

步骤1 解压出来的lib文件夹步骤2编译的jar包放在同一个目录, 运行下面命令:

java -Dloader.path=/path/to/lib -jar /path/to/springboot-jsp-0.0.1-SNAPSHOT.jar 

备注:

  • /path/to/改成实际的路径。
  • -Dloader.path=lib文件夹路径

最终目录文件结构是:

├── lib   #lib文件夹
└── springboot-jsp-0.0.1-SNAPSHOT.jar 

说明

1、通常,一个工程项目架构确定后,引入的jar包基本上不会变,改变的大部分是业务逻辑;

2、后面如果需要变更业务逻辑,只需要轻量地编译工程,大大提高项目部署的效率。

@Linda-Tan
Copy link

我初步尝试是不行的,你确定是能正常运行的吗

@johnnian
Copy link
Owner Author

johnnian commented Mar 30, 2018

@Linda-Tan 我们公司的项目,SpringBoot的工程,都是以这种部署的。

请问,你那边无法运行,具体有报什么错吗? 是编译的时候有问题?还是运行的时候有问题?

@Linda-Tan
Copy link

我在windows 环境下 无法正常启动,说找不到loader.path

@johnnian
Copy link
Owner Author

johnnian commented Apr 6, 2018

@Linda-Tan 我在Windows下测试过,运行是没有问题的.

  1. 请按照上面文章的内容,再试一遍~
  2. 请检查-Dloader.path参数的路径是否写正确了?我教程上写的是Linux格式的路径,你要改成用Windows下的风格;

例如,我将制作好的包,放在 D:\test\ 目录下,运行下面命令

java -Dloader.path=D:\test\lib -jar D:\test\ springboot-jsp-0.0.1-SNAPSHOT.jar 

@Linda-Tan
Copy link

嗯嗯,用原来的命令提示符(CMD)是可以的,win10 默认用他的Powershell 是不支持的,MS很恶心。谢谢!

@foreveryang321
Copy link

foreveryang321 commented Apr 27, 2018

enjoy

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.5.11.RELEASE</version>
	<configuration>
	    <layout>ZIP</layout>
	    <includes>
		<include>
		    <groupId>nothing</groupId>
		    <artifactId>nothing</artifactId>
		</include>
	    </includes>
	    <attach>false</attach>
	</configuration>
</plugin>
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
        <version>3.0.2</version>
	<executions>
	    <execution>
		<id>copy-dependencies</id>
		<phase>package</phase>
		<goals>
		    <goal>copy-dependencies</goal>
		</goals>
		<configuration>
		    <type>jar</type>
		    <includeTypes>jar</includeTypes>
		    <includeScope>runtime</includeScope>
		    <outputDirectory>
			${project.build.directory}/lib
		    </outputDirectory>
		</configuration>
	    </execution>
	</executions>
</plugin>
java -Dloader.path=D:\test\lib\ -jar D:\test\ springboot-jsp-0.0.1-SNAPSHOT.jar

@Linda-Tan
Copy link

我发现这个并不能在spring cloud 中使用。 有些不要数据源的消费者,也会被加载数据源jar,导致无法启动

@clvspk
Copy link

clvspk commented Jan 16, 2019

能问下 你这是什么解压软件吗?

@johnnian
Copy link
Owner Author

能问下 你这是什么解压软件吗?

使用的是 : Dr.unarchiver.app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants