Spring Boot Embedded Tomcat Performance
Asked Answered
C

2

13

I am developing Microservices API for my application. I started with Spring Boot application. I created two artifacts - "business code with embedded tomcat" and "business code without embedded tomcat".

When I compare the performance results, I can see that the "non-embedded tomcat" (i.e. executing on standalone tomcat) gives good output because of native execution.

So basically what is the difference between the embedded tomcat and the standalone tomcat regarding implementation?

How the performance varies between two executions?

Corkage answered 29/10, 2016 at 14:2 Comment(2)
How you did the tests? Could you explain? Thanks.Corrupt
@Rudge: Using Jmeter i simulated load on both scenarios. I am using camel in my business-code. At the end of transaction, i am printing message history which displays route info with execution time. when i compare execution time for both secanrio at least i am getting 20ms average delay on embedded tomcat .Corkage
C
20

I found out actual root cause of this issue.

APR (Apache Portable Runtime) plays important role in tomcat thread execution.

By Default, embedded tomcat executes NIO. NIO and BIO are Java based executions whereas APR is native execution. When we compare performance of NIO and APR, APR is pretty much faster.

In fact all the Linux based tomcat bundles are shipped with APR libs under the tomcat lib folder.

After I enabled APR in embedded tomcat (i.e. Spring Boot) performance execution was same compared to standalone tomcat.

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

Corkage answered 6/11, 2016 at 6:48 Comment(3)
hi @Peter Jerald, could you please share me how you enable APR in embedded tomcat?Cubic
@Cubic here is how: gist.github.com/andreldm/7f89a3279438467a0bd41e6c1249d014Freer
Peter could you please provide me how did you enable APR in spring boot.Liddle
P
0

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.

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;
}
Poacher answered 26/5, 2022 at 5:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.