简体   繁体   English

Spring Boot 嵌入式 Tomcat 性能

[英]Spring Boot Embedded Tomcat Performance

I am developing Microservices API for my application.我正在为我的应用程序开发微服务 API。 I started with Spring Boot application.我从 Spring Boot 应用程序开始。 I created two artifacts - "business code with embedded tomcat" and "business code without embedded tomcat" .我创建了两个工件—— “带有嵌入式 tomcat 的业务代码”和“不带嵌入式 tomcat 的业务代码”

When I compare the performance results, I can see that the "non-embedded tomcat" (ie executing on standalone tomcat) gives good output because of native execution.当我比较性能结果时,我可以看到“非嵌入式 tomcat”(即在独立 tomcat 上执行)由于本机执行而提供了良好的输出。

So basically what is the difference between the embedded tomcat and the standalone tomcat regarding implementation?那么基本上嵌入式tomcat和独立tomcat在实现方面有什么区别?

How the performance varies between two executions?两次执行之间的性能如何变化?

I found out actual root cause of this issue.我发现了这个问题的实际根本原因。

APR (Apache Portable Runtime) plays important role in tomcat thread execution. APR(Apache Portable Runtime)在tomcat线程执行中扮演着重要的角色。

By Default, embedded tomcat executes NIO.默认情况下,嵌入式 tomcat 执行 NIO。 NIO and BIO are Java based executions whereas APR is native execution. NIO 和 BIO 是基于 Java 的执行,而 APR 是本机执行。 When we compare performance of NIO and APR, APR is pretty much faster.当我们比较 NIO 和 APR 的性能时,APR 快得多。

In fact all the Linux based tomcat bundles are shipped with APR libs under the tomcat lib folder.事实上,所有基于 Linux 的 tomcat 包都在 tomcat lib 文件夹下随 APR 库一起提供。

After I enabled APR in embedded tomcat (ie Spring Boot) performance execution was same compared to standalone tomcat.在我在嵌入式 tomcat(即 Spring Boot)中启用 APR 后,与独立 tomcat 相比,性能执行是相同的。

http://tomcat.apache.org/tomcat-7.0-doc/apr.html http://tomcat.apache.org/tomcat-7.0-doc/apr.html

We can enable APR in springboot embeded tomcat by overiding the org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory and providing new org.apache.catalina.connector.Connector with org.apache.coyote.http11.Http11AprProtocol protocol.我们可以通过覆盖 org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory 并使用 org.apache.coyote.http11.Http11AprProtocol 协议提供新的 org.apache.catalina.connector.Connector 在 springboot 嵌入式 tomcat 中启用 APR。

The below code might help to get it done.下面的代码可能有助于完成它。

@Bean
public TomcatServletWebServerFactory servletContainerFactoryProd() {
    TomcatServletWebServerFactory tomcat = new 
        TomcatServletWebServerFactory() {
        @Override
        protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
            // to create new directories and files and add them to Context
            return super.getTomcatWebServer(tomcat);
        }
    };
    
    Connector connector = new Connector("org.apache.coyote.http11.Http11AprProtocol");
    Http11AprProtocol protocol = (Http11AprProtocol) connector.getProtocolHandler();
    connector.setProperty("compression", "on");
    // can also enable ssl and provide certificate details
    tomcat.addAdditionalTomcatConnectors(connector);
    return tomcat;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM