Microservices with Helidon

Allen

Microservices are way to the future. Most of the modern developments in Java are focused on microservice architectures. Spring boot is the most preferred microservice architecture as it is a thin layer over the well-known Spring Framework. Although there are other microservices available to work with Java, I would like to talk about a newer addition to the list, ‘Helidon’.

What are microservices?

Microservices Architecture alias Microservices is an architectural style that structures an application as a collection of services. Each service can either be focused on a business capability or a subdomain which are independently deployable and testable. Since every service are loosely coupled modules, they are highly maintainable and can be owned by a small team.

Microservices architecture

Figure 1: Microservice Architecture

The microservice architecture ensures a rapid, frequent and reliable delivery of any large, complex applications. Organizations will be free to use various technology stack across the microservices and evolving to any new technology stack will be quicker and easier.

Advantages of microservices

We can define an architecture that structures the application as a set of loosely coupled, collaborating services.

  • Agile Development: Loosely coupled individual service modules can be developed in a sprint which can be independently deployed and tested. If planned properly, every release can be considered as a complete product even without future services.
  • Handling by Dev-ops: each Microservices are developed by a small team which is essential for high productivity. Individual services can be deployed without having to coordinate with other teams. Continuous integration will ensure the final product is as stable as individual services.
  • Versioning individual modules: Since microservices are independent on other services within the entire product, adding functionalities, features or upgrades within a service can be done without affecting the entire product.
  • Scaling up and down: Microservices can be scaled independently and separately using pools and clusters. Instead of scaling the entire product, scaling only the required services will save resources and time.

What is Helidon?

Helidon is a Java based microservice framework initially developed by Oracle which implements MicroProfile 1.1 standards. It has been recently open sourced by Oracle. Helidon is just a collection of Java libraries designed to be simple and fast for creating microservices based applications. These libraries, although can be used separately from each other, when used together would help the developer to create the foundation of a microservice, configuration, security and a web server.

Flavours of Helidon

Helidon supports two programming models for writing microservices, Microframeworks and MicroProfile.

1.      Helidon SE

Helidon SE is simple, functional, lightweight microframework that supports the reactive programming model. It used JDK as its runtime.

2.      Helidon MP

Helidon MP is an eclipse Microprofile implementation similar to Java EE/Jakarta EE which allows the user to run microservices in a portable way.

Helidon Architecture

Helidon architecture includes Helidon SE components as the base and Helidon MP components as a thin layer on top of Helidon SE components. Helidon can be easily integrated to Oracle Cloud services.

Figure 2 High Level Architecture of Helidon

Helidon SE Components are Config, Security and RxServer. Helidon runs on an asynchronous and reactive web server on top of Netty (a client server framework).

Helidon MP components (The Java EE/Jakarta EE components) are JAX-RS(Jersey), JSON processing (JSON-P) and CDI. These components are required for MicroProfile implementation.

Setup with Maven

Helidon can be started by configuring the Helidon bom-pom in a Maven Project with Java 8 or higher.

<dependencyManagement>
   <dependencies>
       <dependency>
           <groupId>io.helidon</groupId>
           <artifactId>helidon-bom</artifactId>
           <version>${helidon-version}</version>
           <type>pom</type>
           <scope>import</scope>
       </dependency>
   </dependencies>
</dependencyManagement>

Helidon SE

Helidon SE is the base to build lightweight reactive microservices.

A Hello world implementation:

Routing routing = Routing.builder() 
      .get("/hello", (req, res) -> res.send("Hello World"))      
 .build(); 

WebServer.create(routing)  
     .start();

The above program will start a webserver on a random free port and can be accessed via the endpoint ‘/hello’

Helidon MP

A Hello World implementation which creates an Eclipse Microprofile and a runtime for microservices are given below.

@Path("hello")
@RequestScoped //allows us to use CDI injection
public class HelloWorld {   
    @GET   
    @Metered  
    public String hello() {
      return "Hello World";   
   }
}

The above program can be started by using the code below

Server.builder()   
    .addResourceClass(HelloWorld.class) 
      .build()   
    .start();

CDI injections can be triggered in this project by creating beans.xml file in the src/main/resources/META-INF directory.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javae

http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
       bean-discovery-mode="annotated">
</beans>

The above configurations would start a webserver on the default port (7001) and can be accessed via the endpoint ‘/hello’.

Conclusion

Originally Helidon was developed as an architecture for cloud integration and was then called as J4C (Java for Cloud). More Helidon components are still in development and will be open sourced in near future. These light weight java libraries based on Java SE can be preferred over other Java EE based microservices for a wide variety of projects of all sizes.

 

Author : Allen | Sr. Software Engineer at Heidelsoft.
© Heidelsoft Technologies Private Limited