以下是一些有关如何通过Spring Boot 缩小足迹的
个人想法。您的问题过于广泛,以致于在任何其他情况下都无法考虑这些建议。我不确定在大多数情况下是否要遵循这些原则,它只是回答“如何实现更小的占地面积”。
(1)仅指定必需的依赖项
我个人不会为此担心,但是如果目标是缩小占地面积,则可以避免使用
starter-* dependencies。仅指定您实际使用的依赖项。
避免这种情况 :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency>
在我的示例项目中, 使用starter- 依赖项生成的工件 约为25MB*
这样做 :
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId></dependency>
在我的样本项目中, 没有starter- 依赖项的工件 约为15MB*
(2)排除自动配置
排除不需要的自动配置:
@Configuration@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})public 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,则可以选择其他容器:
- Tomcat(默认情况下)
- undertow(https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-undertow)
- 码头(https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-jetty)
- …
(5)Spring的建议
- 记忆:
java -Xmx32m -Xss256k -jar target/demo-0.0.1-SNAPSHOT.jar
- 线程数:
server.tomcat.max-threads: 4
- 来源:spring-boot-memory-performance
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)