spring boot嵌入tomcat项目在部署时,报内存不足,进过调整启动参数后,终于能正常启动了但是一运行,相关的功能模块后,内存溢出.
搜索了一下spring boot项目的优化:
发现这篇神文
https://www.cnblogs.com/chen110xi/p/6198481.html
操作一番之后发现需要将自己定义的用注解导入的类都需要通过@impor注解导入.编译重新部署后程序正常运行.
@Configuration
@ImportResource(locations={"classpath:conf_client/dataSetConfig.xml","classpath:conf_app/dataSourceConfig.xml"})
@Import({
CodecsAutoConfiguration.class,
//DataSourceAutoConfiguration.class,
//DataSourceJmxConfiguration.class,
/*DataSourceTransactionManagerAutoConfiguration.class,
DispatcherServletAutoConfiguration.class,
EmbeddedWebServerFactoryCustomizerAutoConfiguration.class,
ErrorMvcAutoConfiguration.class,
// GenericCacheConfiguration.class,
HttpEncodingAutoConfiguration.class,
HypermediaAutoConfiguration.class,
JacksonAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class,
JmxAutoConfiguration.class,
MultipartAutoConfiguration.class,
MybatisAutoConfiguration.class,
// NoOpCacheConfiguration.class,
// OAuth2AutoConfiguration.class,
//OAuth2RestOperationsConfiguration.class,
PersistenceExceptionTranslationAutoConfiguration.class,
RepositoryRestMvcAutoConfiguration.class,
RestTemplateAutoConfiguration.class,
SecurityAutoConfiguration.class,
SecurityFilterAutoConfiguration.class,
SecurityRequestMatcherProviderAutoConfiguration.class,
ServletWebServerFactoryAutoConfiguration.class,
//SimpleCacheConfiguration.class,
TransactionAutoConfiguration.class,
UserDetailsServiceAutoConfiguration.class,
ValidationAutoConfiguration.class,
WebMvcAutoConfiguration.class,
WebSecurityEnablerConfiguration.class,
WebSocketServletAutoConfiguration.class,*/
//HttpEncodingAutoConfiguration.class,
// HypermediaAutoConfiguration.class,
//OAuth2AutoConfiguration.class,
HttpEncodingAutoConfiguration.class,
JacksonAutoConfiguration.class,
DispatcherServletAutoConfiguration.class,
//WebSocketServletAutoConfiguration.class,
WebMvcAutoConfiguration.class,
RepositoryRestMvcAutoConfiguration.class,
RestTemplateAutoConfiguration.class,
TransactionAutoConfiguration.class,
SecurityAutoConfiguration.class,
// SecurityFilterAutoConfiguration.class,
//SecurityRequestMatcherProviderAutoConfiguration.class,
ServletWebServerFactoryAutoConfiguration.class,
ClientConfig.class,
DataSourceConfig.class,
OAuth2ServerConfig.class,
DataClientController.class,
DataSetService.class,
DaoSupport.class,
HistoryService.class,
SpringUtil.class
//HistoryDao.class,
//IPlatAnnoDao.class
// MapperScannerConfigurer.class
})
原文在这里:
https://www.alexecollins.com/spring-boot-performance/
Spring Boot Performance
This is an article on how to improve the performance of Spring Boot applications. I've recently been working on a new project. As we primarily use Java and Spring, we've been looking at Spring Boot. It's allowed us to get up and running quickly.
Early on, I came across a problem with a prototype for one of our new applications. It was loading the Velocity web page template engine. I could not understand why – it was just some REST services, no web pages. I spent a bit of time looking into this issue, and how to improve the performance of Spring Boot applications, and this is what I found.
Component Scanning Slows Start-up
By default, you may find yourself using the @SpringBootApplication
annotation to get your application configured automatically. This has a couple of side-effects. One is to enable component scanning. This looks through the classes to find ones annotated with Spring "stereotypes", such as @Component
. This is convenient, especially when you start out, but it has two side-effects:
- It slows application start-up time. This will have a greater impact if you have a large application, or a large number of integration tests that need to start up the application to run.
- It may load beans you don't want or need.
You can disable component scanning by removing the @SpringBootApplication
and @ComponentScan
annotations. You'll then need to make each bean explicit in your configuration.
// remove @SpringBootApplication and @ComponentScan, replace with @EnableAutoConfiguration
@Configuration
@EnableAutoConfiguration
public class SampleWebUiApplication {
// ...
// you must explicitly list all beans that were being component scanned @Bean
public MessageController messageController(MessageRepository messageRepository) {
return new MessageController(messageRepository);
}
Auto-Configuration Can Load More Than You Need
The @SpringBootApplication
annotation implies the @EnableAutoConfiguration
annotation. This enables auto-configuration. This can load components you don't need, slowing application start-up and increasing memory and CPU usage. Lets look at how to use this in a more controlled fashion.
If you start your application using -Ddebug
it'll print a report of the components it auto-configures:
mvn spring-boot:run -Ddebug
…
=========================
AUTO-CONFIGURATION REPORT
=========================
Positive matches:
-----------------
DispatcherServletAutoConfiguration
- @ConditionalOnClass classes found: org.springframework.web.servlet.DispatcherServlet (OnClassCondition)
- found web application StandardServletEnvironment (OnWebApplicationCondition)
...
Copy the classes mentioned in the ""positive matches" section of the report:
DispatcherServletAutoConfiguration
EmbeddedServletContainerAutoConfiguration
ErrorMvcAutoConfiguration
HttpEncodingAutoConfiguration
HttpMessageConvertersAutoConfiguration
JacksonAutoConfiguration
JmxAutoConfiguration
MultipartAutoConfiguration
ServerPropertiesAutoConfiguration
PropertyPlaceholderAutoConfiguration
ThymeleafAutoConfiguration
WebMvcAutoConfiguration
WebSocketAutoConfiguration
Update your configuration to explicitly import them, and run your tests to make sure everything is OK.
@Configuration
@Import({
DispatcherServletAutoConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class,
ErrorMvcAutoConfiguration.class,
HttpEncodingAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
JacksonAutoConfiguration.class,
JmxAutoConfiguration.class,
MultipartAutoConfiguration.class,
ServerPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
ThymeleafAutoConfiguration.class,
WebMvcAutoConfiguration.class,
WebSocketAutoConfiguration.class,
})
public class SampleWebUiApplication {
I can see that both JMX and web sockets are listed, but I know I'm not using them. I can delete them, and any other dependencies I don't need, to get a performance improvement. Run your tests again to make sure everything is OK.
Change Servlet Container To Undertow
By default, Spring Boot uses Tomcat. Tomcat uses around 110mb of heap, and has ~16 threads:
Undertow is a lightweight servlet container from JBoss. You can switch to Undertow to get a performance improvement. Firstly, exclude Tomcat from your dependencies:
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
Add Undertow:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
Undertow uses around 90MB and has ~13 threads:
Conclusion
These are a few small tips on improving the performance of your Spring Boot applications. The benefits are smaller for smaller applications, but for larger applications can quickly become pronounced. Try it out and tell me what you think.
As usual, the code is on Github.
References
- https://www.techempower.com/benchmarks/
- https://cloud.google.com/appengine/articles/spring_optimization
Related