开发具有较低占用空间的 Spring Boot 应用程序 [英] Developing spring boot application with lower footprint

查看:24
本文介绍了开发具有较低占用空间的 Spring Boot 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

欢迎任何在这个方向上的输入或指示.

最好总是从最低限度的依赖项开始向上构建应用程序,并且仅在需要时再添加.是否有更多好的做法可以遵循以进一步减小应用程序的大小?

解决方案

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

(1) 只指定需要的依赖

我个人不会为此担心,但如果目标是减少占用空间,您可以避免使用 starter-* 依赖项.仅指定您实际使用的依赖项.

避免这种情况:

<依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></依赖>

<块引用>

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

这样做:

<依赖><groupId>org.springframework.data</groupId><artifactId>spring-data-jpa</artifactId></依赖>

<块引用>

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

(2) 排除自动配置

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

@Configuration@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})公共类 MyConfiguration {}

(3) Spring Boot 属性

在 application.properties 中尽可能多地禁用(同时确保它不会产生负面影响):

spring.main.web-environment=falsespring.main.banner-mode=offspring.jmx.enabled=falseserver.error.whitelabel.enabled=falseserver.jsp-servlet.registered=falsespring.freemarker.enabled=falsespring.groovy.template.enabled=falsespring.http.multipart.enabled=falsespring.mobile.sitepreference.enabled=falsespring.session.jdbc.initializer.enabled=falsespring.thymeleaf.cache=false...

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

如果使用嵌入式 Web 容器启动 spring boot,您可以选择不同的:

(5) Spring 的建议

(6) 另见:

In an attempt to keep our microservices, developed in spring-boot to run on Cloud Foundry, smaller in footprint, we are looking for best approach to achieve the same.

Any inputs or pointers in this direction will be most welcomed.

It's surely the best that one always builds the application upwards starting from bare minimum dependencies, and the add any more only when required. Is there more of good practices to follow to further keep the application smaller in size?

解决方案

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

  • Memory: java -Xmx32m -Xss256k -jar target/demo-0.0.1-SNAPSHOT.jar
  • Number of threads: server.tomcat.max-threads: 4
  • source: spring-boot-memory-performance

(6) See also:

这篇关于开发具有较低占用空间的 Spring Boot 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆