Developing microservice with java

Microservice architecture is another trending service oriented architecture. I have another post about microservices architecture.

1-microservices-gateway-v3

Java is one of the most used language for back-end development which is suitable for developing microservices. There are different ways of developing java microservices with either containerized technologies or non-containerized one. In this post I have described these two approaches.

Java applications can be running either in a web server or be self-contained with an embedded server. You can decide either ways according to the architectural decision that is made in an organization regarding different parameters:

  • Runtime environment:
    • containerized
    • non-containerized
  • API management
  • Development strategies

You can choose web server based technologies such as Jax-rs to develop your microservice whose end-result is generating a war file deployable to a web-server. The other approach is using spring-boot with a self-contained tomcat server which is bundled in a jar file including both application and a server. The jar file is separately runnable on a machine with a java run-time environment. I am gonna show both jax-rs and spring-boot approach in the following sections.

spring-boot

Project structure:

 

Every java application needs a build framework such as maven, graddle, ant, etc. I am going to use maven the most used build and dependency management tool for java. In order to develop a spring boot you need to include the following dependencies in your pom.xml file.

    <parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>_Brixton_.RELEASE</version>  <!-- Name of release train -->
    </parent>
    <dependencies>
        <dependency>
            <!-- Setup Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <!-- Setup Spring MVC & REST, use Embedded Tomcat -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <!-- Spring Cloud starter -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>

        <dependency>
            <!-- Eureka for service registration -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>

Since spring boot is self-contained and produces a runnable jar file, we need the java main method to run our application. Then the following class is required to be added to the project:

package hello;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {
    
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
        
        System.out.println("Let's inspect the beans provided by Spring Boot:");
        
        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }

}

 

 

The next step is to create a controller:

package hello;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {
    
    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }
    
}

aa