zoukankan      html  css  js  c++  java
  • Spring BOOT PERFORMANCE

    转自:http://www.alexecollins.com/spring-boot-performance/

    官方优化文档: https://spring.io/blog/2015/12/10/spring-boot-memory-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:

    1. 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.
    2. 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:

    tomcat

    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:

    undertow

    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

  • 相关阅读:
    WordPress Pretty Photo插件‘hashrel’参数跨站脚本漏洞
    WordPress Suco Themes ‘themify-ajax.php’任意文件上传漏洞
    nginx 安全漏洞 (CVE-2013-4547)
    Linux kernel ‘uio_mmap_physical’函数缓冲区溢出漏洞
    OpenSSH ‘mm_newkeys_from_blob’函数权限许可和访问控制漏洞
    WordPress Kernel Theme ‘upload-handler.php’任意文件上传漏洞
    Wordpress Jigoshop插件路径泄露漏洞
    WordPress Think Responsive Themes ‘upload_settings_image.php’任意文件上传漏洞
    Linux Kernel ‘write_tag_3_packet()’函数本地基于堆的缓冲区溢出漏洞
    SpringMVC配置数据验证(JSR-303)
  • 原文地址:https://www.cnblogs.com/fangyuan303687320/p/5631411.html
Copyright © 2011-2022 走看看