Switching from Embedded Netty to Apache Tomcat

By default, Spring Boot is geared up to use embedded Netty (http://netty.io). Why? Because it's one of the most popular solutions for reactive applications. And when it comes to reactive applications, it's critical that the entire stack be reactive.

However, it's possible to switch to another embedded container. We can experiment with using Apache Tomcat and its asynchronous Servlet 3.1 API. All we have to do is to make some tweaks to the dependency settings in build.gradle, as follows:

    compile('org.springframework.boot:spring-boot-starter-webflux') { 
      exclude group: 'org.springframework.boot', 
      module: 'spring-boot-starter-reactor-netty' 
    } 
    compile('org.springframework.boot:spring-boot-starter-tomcat') 

What's happening in the preceding code? This can be explained as follows:

  • spring-boot-starter-webflux excludes spring-boot-starter-reactor-netty, taking it off the classpath
  • spring-boot-starter-tomcat is added to the classpath
  • Spring Boot's TomcatAutoConfiguration kicks in, and configures the container to work using TomcatReactiveWebServerFactory

It's important to point out that there are these other containers available:

  • Jetty
  • Undertow

For the rest of this title, we'll stick with Spring Boot's default, Netty.

It's interesting to refer to these as containers given that they are contained inside our application. It used to be standard practice to install Apache Tomcat (or whatever container we picked) and install the application into it. But Spring Boot has made embedded containers a core feature, inverting this concept of apps inside containers and putting the container inside the app instead. For an entertaining presentation on how this change has swept the Java community, check out Eberhard Wolff's Java Application Servers Are Dead (http://www.slideshare.net/ewolff/java-application-servers-are-dead) presentation.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.7