java microservice frameworks

Hello everyone. Microservices architecture is very commonly used these days and to facilitate the development using this architecture, we have the frameworks to help us. Every programming language like Java, Python, Golang, C#, etc has its framework to develop the microservices. In this article, we are going to look in detail at popular microservices frameworks for Java language and understand their features. We will also look at when to use these frameworks and discuss a hybrid approach where you can use different Microservice frameworks for different Microservice to explore their benefits. Let’s get started.

Microservices Frameworks for Java

Since the advent of Microservices, a lot of frameworks for Java have been evolving over time, and the list is growing.

Image Source: https://vedcraft.com/wp-content/uploads/2021/06/microservices-java-1-1024x578.png

Out of these microservices frameworks for Java, the popular ones are as follows

  1. Spring Boot
  2. Quarkus
  3. MicroNaut
  4. Vert.x
  5. DropWizard
  6. Helidon

All these frameworks look similar to each other in terms of using maven or gradle dependencies as well as building and deploying the jar files into the embedded servers. The coding, IDE, and almost everything will look the same except for some different annotations. The major difference lies in their respective architecture internals like bootstrapping the server, injecting dependencies, routing the requests, and components used. If you know one of these, learning the other is not difficult as it still uses Java and runs on JVM.

1. What is Spring Boot?

Spring Boot is the most famously and widely used framework for developing microservices in Java. Together with Spring Cloud, it is the best option for developing Microservices in Java as it is the most established and tested framework with larger community support.

Spring Cloud provides many features out-of-the-box which are needed in a Microservice architecture like service discovery, load balancing, connectivity to external systems like Message Queues, etc. It follows Netflix Microservice architecture, which is also the most common implementation.

Image Source: https://miro.medium.com/max/597/1*nk46Jtgmh8vKlBIYF2-7QQ.png

The Spring boot provides application development using 2 frameworks namely

1. Servlet Stack

The Servlet Stack provides the Spring Model View Controller (MVC) approach and uses the Thread pool mechanism to achieve the request-response model. This architecture style is also called a blocking model where all the incoming requests are mapped to their respective threads, which get blocked until the response is ready. This framework also provides inbuilt support to other Spring modules like Spring Security, Embedded Application Servers also called Servlet Containers, and Spring Data JPA support for ORM frameworks and databases.

The maven dependency to use Spring boot using traditional blocking way is as follows

<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-web</artifactId>  
    <version>3.0.2</version>  
</dependency>

2. Reactive Stack

Spring WebFlux is intended to provide an alternative to Spring MVC in the form of a non-blocking model using a mechanism called Event loops.

Webflux may not be not necessarily faster than Spring MVC but it is better when it comes to dealing with issues of scalability while making efficient use of hardware resources. This stack has support for Reactive Spring Security and Reactive Database support. We can unleash the full power of Spring Webflux only by using a 100% reactive stack i.e using reactive libraries for servers, applications, and database drivers.

The maven dependency to use Spring Webflux is as follows

<dependency>   
   <groupId>org.springframework.boot</groupId>  
   <artifactId>spring-boot-starter-webflux</artifactId>  
   <version>2.7.0</version>   
</dependency>

The easiest way to start a spring project is to go to Spring Initializr and then select the dependencies needed to download a skeleton project. Though the Servlet Stack is the most commonly used in most microservices applications, the Reactive Stack is gaining traction and is good to learn as applications that need high performance and low latency will be needing this framework.

2. Quarkus

Quarkus is a framework developed by Redhat for developing high-performance and scalable Java applications. It is an open-source Java framework optimized to run on OpenJDK and GraalVM that provides support for imperative and reactive programming.

Quarkus is

  • Subatomic because it is small and lightweight
  • Supersonic because of ultra-fast ignition time

Quarkus is a Kubernetes-native Java framework that allows developers to write modern cloud-native microservices and makes it easier to deploy applications to Kubernetes with minimal configuration. It allows developers to automatically generate the resources for Kubernetes like deploying container images without any manual YAML files.

As seen above in the architecture diagram, Quarkus has a core module called Quarkus Core and other modules to generate bytecode, provide Dependency Injection support, and so on. Quarkus also provides an extension to the Netty Reactive Server, Hibernate ORM, and many other hybrid libraries.

The easiest way to start Quarkus is by using the following command in the terminal if you are using Linux or MacOS

mvn io.quarkus:quarkus-maven-plugin:1.13.2.Final:create \  
    -DprojectGroupId=com.example \  
    -DprojectArtifactId=hello-world \  
    -DclassName="com.example" \  
    -Dpath="/hello"

This will download all the maven dependencies as shown below

Quarkus achieves fast ignition time by doing as much work as possible at build time to keep the resulting application as small and fast as possible. Runtime should only contain classes that are needed to actually run the application and that is how it achieves a Low footprint.

Quarkus is faster than Spring Boot in terms of startup time both in JVM and native versions. Furthermore, Quarkus build time was also much quicker in the case of native images.

If you are looking to develop fast and reactive Microservices, then Quarkus is a good contender to learn.

3. MicroNaut

Micronaut is a modern JVM-based full-stack framework for building modular, easily testable Microservice and Serverless applications

MicroNaut was created by OCI, the company that created Grails, a popular web development framework for Groovy developers. It is inspired by Spring, Spring Boot, and Grails frameworks and supports Java, Kotlin, and Groovy languages.

As seen in the architecture diagram, Micronaut provides support to a wide range of features including Micronaut Core, Dependency Injection, AOP, Reactive streams, Netty server, Security, and so on.

This is pretty much like the Spring Boot framework and it also provides libraries

  • Distributed Configuration
  • Service Discovery
  • HTTP Routing
  • Client-Side Load Balancing

It has improvised over Spring, Spring Boot, and Grails by providing the following features

  • Fast startup time
  • Reduced memory footprint
  • Minimal use of reflection
  • Minimal use of proxies
  • No runtime bytecode generation
  • Easy Unit Testing

You can fill in the required dependencies in Micronaut Launch to download and start the project. This is exactly similar to Spring Initilizr: Micronaut Launch

After I imported the downloaded project, my pom.xml looks like below

Since Micronaut provides like-to-like features like Spring Boot and is more efficient, it is getting a lot of attention from the Java community. Big companies like Oracle and JetBrains are putting their weight behind MicroNaut. So it is definitely a strong contender to Spring Boot and Spring Cloud and definitely worth learning in 2023.

4. Vert. x

Vert.x from Eclipse is a lightweight and high-performance framework to develop Reactive Microservices for the JVM. Vert.x is polyglot and reactive on the JVM and it supports multiple languages like Java, JavaScript, Apache Groovy, Ruby, Scala, and Kotlin.

Vert.x does not provide an all-in-one solution like a Spring boot, instead, it provides a toolkit and building block to build your own component. Since it’s a toolkit, it provides flexibility to use it as a standalone or embed it inside a Java program or inside frameworks Spring, Quarkus, etc.

Vert. x supports a non-blocking and asynchronous development model and allows to run distributed microservices applications on Java Virtual Machine. It uses an event bus to communicate with different parts of the application and passes events, asynchronously to handlers when they are available. It is quite similar to Spring Webflux but more efficient and faster.

Vert.x operates a single event loop like NodeJS. Node runs on a single core, but Vert.x runs on the multi-core depending on the number of available cores to it. Vert.x maintains a thread pool with a size that can match the number of available cores. With greater concurrency support, Vert.x is suitable for not only IO operations but also CPU-heavy processes that require parallel computing.

Verticles are pieces of code that Vert.x engine executes in a single thread. The Verticles can be considered as the controller handling the dispatcher requests. The toolkit provides us with many abstract verticle classes, which can be extended, and implemented.

The Vert.x Instance is the container for all the Verticles and they are all executed simultaneously inside the same Vert.x instance. An application would typically be composed of multiple verticles running in the same Vert.x instance and communicate with each other using events via the Event Bus. The message can be anything from a string to a complex object.

Asynchronous messages are queued to the event bus and control is returned to the sender. Later it’s dequeued to the listening verticle. The response is sent using Future and callback methods.

Vert.x can be used by adding the following Maven dependency.

<dependency>  
    <groupId>io.vertx</groupId>  
    <artifactId>vertx-core</artifactId>  
    <version>3.4.1</version>  
</dependency>

Vert.x frameworks allow us to create an HttpServer that can listen to the port we configure.

To host a RESTful web service, we need to use another Vert.x module called vertx-web as follows

<dependency>  
    <groupId>io.vertx</groupId>  
    <artifactId>vertx-web</artifactId>  
    <version>3.4.1</version>  
</dependency>

Then we need to add the router to the server which routes the requests to the correct handle method. Vert.x can be added to a Spring Boot application to provide reactive support on top of the Spring Servlet stack as it is faster than Spring Webflux. Vert.x is also used with other frameworks as well as it is just a toolkit.

5. DropWizard

DropWizard is another powerful open-source Java framework for creating RESTful web services and microservices. It was developed by Yammer, a child company of Microsoft Corporation, to power theirs. DropWizard uses popular Java libraries like Jetty, Jackson, and Jersey for creating ops-friendly, high-performance RESTful backends.

Dropwizard is a framework that bundles a few other frameworks and Java libraries needed for the fast bootstrapping of Java REST services. Dropwizard provides in-built support to use all of these and it easily consists mostly of glue code to automatically connect and configure these components.

  1. Embedded Jetty Server
  2. JAX-RS Jersey for RESTful web services
  3. Jackson library for JSON
  4. Logging using Logback and SLF4J.
  5. Hibernate Validator API for declarative validation.
  6. Monitoring using the Metrics library

It also includes some other libraries such as JDBI for database access, Guava for easier collections, caching, functional programming, etc.,

In a nutshell, it’s a bundle of great frameworks and libraries that makes it much easier to develop REST services with Java without worrying about putting them all together.

We only need the following dependency to create a microservice

<dependency>  
    <groupId>io.dropwizard</groupId>  
    <artifactId>dropwizard-core</artifactId>  
    <version>2.0.0</version>  
</dependency>

The dropwizard-core the module provides you with everything you’ll need for most of your applications.

Setting up the application is very easy. Lets us write code to create the Java Bean, Repository, and RESTful resource.

public class User {  
    private final Long id;  
    private final String name;  
}  
 
public class UserRepository {  
    private final List<User> users;  
  
   public List<User> findAll(int size) {  
        return users.stream()  
          .limit(size)  
          .collect(Collectors.toList());  
    }  
}

The Dropwizard uses JAX-RS by default with Jersey as implementation and hence the annotations from this specification are used to expose the REST API endpoints

@Path("/users")  
@Produces(MediaType.APPLICATION_JSON)  
public class UserResource {  
      
    private final UserRepository userRepository;  
  
    public UserResource(UserRepository userRepository) {  
      this.userRepository = userRepository;  
    }  
  
    @GET  
    public List<User> getUsers() {  
        return userRepository.findAll();  
    }  
}

Now we have to register the resource in the Application class by initializing the needed dependencies. Likewise, all the Resources used in the application must be registered in this method.

@Override  
public void run(BasicConfiguration basicConfiguration, Environment environment) {  
    UserRepository userRepository = new UserRepository();  
    UserResource userResource = new UserResource(userRepository);  
  
    environment  
      .jersey()  
      .register(userResource);  
}

Please take a look at the link below for a detailed understanding: Getting Started — Dropwizard

6. Helidon

Helidon is the new Java microservice framework that was recently open-sourced by Oracle. It was used initially used in Oracle projects under the name J4C (Java for Cloud).

Helidon framework supports two programming models for developing microservices namely Helidon SE and Helidon MP

Helidon SE is a simple, functional, lightweight microframework that supports reactive programming. It uses 3 modules namely Config, Security, and the RxServer i.e Netty.

Helidon MP is an Eclipse Microprofile runtime that allows Java EE/Jakarta EE developers to run portable microservices. It is a thin layer on top of Helidon SE components. It consists of JSON-P, JAX-RS/Jersey, and CDI components that are required for the Microprofile implementation.

Both Helidon SE and Helidon MP allow developers to write microservices that start a small HTTP server from the main method.

Dependencies for Helidon SE

  1. Webserver API

To use the Webserver API, we need to use the following Maven dependency

<dependency>  
    <groupId>io.helidon.webserver</groupId>  
    <artifactId>helidon-webserver</artifactId>  
    <version>0.10.4</version>  
</dependency>

The WebServer is an SPI i.e Server Provider Interface and hence we need to provide an actual implementation of it. To add the Netty Server provided by Helidon, we need to use the following dependency

<dependency>  
    <groupId>io.helidon.webserver</groupId>  
    <artifactId>helidon-webserver-netty</artifactId>  
    <version>0.10.4</version>  
    <scope>runtime</scope>  
</dependency>

2. Config API

To add the configuration in Helidon, we need to add the following Helidon Config API dependency

<dependency>  
    <groupId>io.helidon.config</groupId>  
    <artifactId>helidon-config</artifactId>  
    <version>0.10.4</version>  
</dependency>

3. Security

Helidon also provides security in its framework similar to Spring Security. The security module offers many providers for authentication and authorization. To that, we need to add the following dependencies

<dependency>  
    <groupId>io.helidon.security</groupId>  
    <artifactId>helidon-security</artifactId>  
    <version>0.10.4</version>  
</dependency>  
<dependency>  
    <groupId>io.helidon.security</groupId>  
    <artifactId>helidon-security-provider-http-auth</artifactId>  
    <version>0.10.4</version>  
</dependency>  
<dependency>  
    <groupId>io.helidon.security</groupId>  
    <artifactId>helidon-security-integration-webserver</artifactId>  
    <version>0.10.4</version>  
</dependency>

Dependencies for Helidon MP

Helidon MP provides a runtime for running MicroProfile-based microservices. We need to add the following 2 dependencies and it will work along with the previous dependencies of Helidon SE.

<dependency>  
    <groupId>io.helidon.microprofile.bundles</groupId>  
    <artifactId>helidon-microprofile-1.2</artifactId>  
    <version>0.10.4</version>  
</dependency>  
<dependency>  
    <groupId>org.glassfish.jersey.media</groupId>  
    <artifactId>jersey-media-json-binding</artifactId>  
    <version>2.26</version>  
</dependency>

Which framework to choose?

Well, after seeing the 6 Microservices for Java above, the next question is which one should I learn or which one should I use?

The answer to this question depends on a lot of factors.

If you are looking for a big ecosystem and community support, then Spring Boot is the ideal choice. Spring Boot is still the most widely used framework to develop microservice in Java as a lot of resources are available online to learn. If you look at any job description, you will see Spring Boot in that obviously.

But if you are going for reactive programming, then Spring Webflux is a bit on the slower side. Vert.x is very lean and faster for reactive programming. It can be used as a toolkit within any framework like Spring Boot, Quarkus, etc.

Micronaut is very similar to Spring Boot and is very simple to use and more efficient. Micronaut has the advantage of supporting spring code and libraries thanks to micronaut-spring feature. It is getting a bigger community and some big companies have started using it. Since Micronaut has addressed the drawbacks of the Spring Framework, it is a great alternative to learn and use in 2023.

If you are looking to write microservices that run on AWS Lambda or Serverless, then Quarkus would be the choice because of the support for deployment descriptors. Even for deployments into Kubernetes, Quarkus provides great support and generation of configuration. The boot time is less with a low memory footprint.

If you want to write simple and faster microservices, then DropWizard is a good option. It does not have a huge community and support like Spring Boot, but it is great to write microservices.

Helidon is the newest among all and has a quick start-up time and a low memory footprint. It is also a great option to develop reactive microservices.

Hybrid Approach

It is not necessary to just learn and use a single Microservice Framework for your architecture. We know that Microservices can be polyglot i.e every individual microservice can be developed using different programming languages like GoLang, Python, C#, NodeJS, Java, etc. Likewise, the microservices can be developed using hybrid microservices frameworks as shown below

Image Source: http://d2uetvsama7sl8.cloudfront.net/prod/wp-content/uploads/2015/06/01140142/architecture.png

eg: Account-Service using Helidon, User-Service using Spring Boot, Cart-Service using DropWizard, etc. There will be a learning curve but you will get the benefit of all the frameworks if you implement them the right way.

Summary

In this article, we looked in detail at popular microservices frameworks for Java languages and understood their features. We looked at Spring Boot, Quarkus, Micronaut, Vert.x, Dropwizard, and Helidon in-depth and discussed how frameworks can be chosen. We also looked at the hybrid approach where you can use different frameworks for different microservices inside a single application according to the use cases.