开发具有更小占地面积的弹簧靴应用

Ash*_*pta 22 spring-boot

为了保持我们的微服务,在Spring-boot中开发,在Cloud Foundry上运行,占用空间更小,我们正在寻找实现相同目标的最佳方法.

这方面的任何输入或指示都将受到欢迎.

从最小的依赖性开始,总是最好地构建应用程序,并且仅在需要时才添加.是否有更多良好实践可以使应用程序进一步缩小尺寸?

ale*_*xbt 42

以下是有关如何使用Spring Boot 缩小占用空间的一些个人想法.您的问题过于宽泛,无法在任何其他情况下考虑这些建议.我不完全确定你想在大多数情况下都遵循这些,它只是回答"如何实现更小的足迹".

(1)仅指定必需的依赖项

我个人并不担心,但如果目标是减少占地面积,你可以避免使用starter-* dependencies.仅指定实际使用的依赖项.

避免这个:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在我的示例项目中,使用starter-*依赖项生成的工件大约为25MB

这样做:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在我的示例项目中,没有starter-*依赖项生成的工件大约是15MB

(2)排除自动配置

排除您不需要的自动配置:

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}
Run Code Online (Sandbox Code Playgroud)

(3)Spring Boot属性

尽可能在application.properties中禁用(同时确保它也没有负面影响):

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
...
Run Code Online (Sandbox Code Playgroud)

(4)明智地选择嵌入式Web容器

如果使用嵌入式Web容器启动spring boot,您可以选择另一个:

(5)Spring的建议

(6)另见: