为了保持我们在Spring-boot中开发的微服务在Cloud Foundry上运行,占用空间更小,我们正在寻找实现相同目标的最佳方法。
这方面的任何意见或指示都会受到欢迎。
从最小的依赖性开始,总是最好地构建应用程序,并且仅在需要时才添加。是否有更多良好实践可以使应用程序进一步缩小尺寸?
答案 0 :(得分:31)
Below are some personal ideas on how to reach a smaller footprint with Spring Boot. Your question is too broad for these recommandations to be taken into account in any other context. I'm not entirely sure you want to follow these in most situation, it simply answers "how to achieve a smaller footprint".
(1) Only specify required dependencies
I wouldn't personally worry about it, but if the goal is to have a smaller footprint, you may avoid using starter-* dependencies
. Only specify the dependencies you actually use.
Avoid this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
In my sample project, the artifact produced with starter-* dependencies is ~25MB
Do this:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
In my sample project, the artifact produced without starter-* dependencies is ~15MB
(2) Exclude AutoConfigurations
Exclude the AutoConfiguration you don't need:
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}
(3) Spring Boot properties
Disable as much as you can in the application.properties (while making sure it does not have a negative impact too):
spring.main.web-environment=false
spring.main.banner-mode=off
spring.jmx.enabled=false
server.error.whitelabel.enabled=false
server.jsp-servlet.registered=false
spring.freemarker.enabled=false
spring.groovy.template.enabled=false
spring.http.multipart.enabled=false
spring.mobile.sitepreference.enabled=false
spring.session.jdbc.initializer.enabled=false
spring.thymeleaf.cache=false
...
(4) Choose your embedded web container wisely
If launching spring boot with an embedded web container, you may choose a different one:
(5) Spring's recommandations
java -Xmx32m -Xss256k -jar target/demo-0.0.1-SNAPSHOT.jar
server.tomcat.max-threads: 4
(6) See also: