Monday, January 16, 2017

A Quick Start Guide for Writing Microservices with Spring Boot

Microservices architectural style is an approach to developing a single application as a suite of small services, each running in its own process. In this approach, instead of writing a monolithic application, we implement the same functionality by breaking it down to a set of lightweight services.

There are various frameworks that provide capability of writing microservices and in this post, I’m discussing how to do it using Spring Boot https://projects.spring.io/spring-boot/ .

I’m going to create an API for handling user operations and expose the operations as RESTful services. The service context is /api/user and based on the type of the HTTP request, appropriate operation will be decided. (I could have further divided this to four microservices... but let’s create them as one for the moment)


Let’s get started with the implementation now. I simply create a Maven project (java) with the following structure.


└── UserAPI_Microservice
    ├── pom.xml
    ├── src
    │   ├── main
    │   │   └── java
    │   │       └── microservice
    │   │           ├── App.java
    │   │           └── UserAPI.java


Add following parent and dependency to the pom.xml file of the project.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
</parent>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>


The App class has the main method which runs the UserAPI.

package com.tharindue;

import org.springframework.boot.SpringApplication;

public class App {

  public static void main(String[] args) throws Exception {
      SpringApplication.run(UserAPI.class, args);
  }
}

The UserAPI class exposes the methods in the API. I have defined the context api/user at class level and for the methods, I haven’t defined a path, but only the HTTP request type.

package com.tharindue;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
@RequestMapping("api/user")
public class UserAPI {

  @RequestMapping(method = RequestMethod.GET)
  @ResponseBody
  String list() {
      return "Listing User\n";
  }

  @RequestMapping(method = RequestMethod.POST)
  @ResponseBody
  String add() {
      return "User Added\n";
  }

  @RequestMapping(method = RequestMethod.PUT)
  @ResponseBody
  String update() {
      return "User Updated\n";
  }

  @RequestMapping(method = RequestMethod.DELETE)
  @ResponseBody
  String delete() {
      return "User Deleted\n";
  }

}


After building the project with maven, simple run the below command and the service will start serving in the 8080 port of localhost.

mvn spring-boot:run

If you need to change the port of the service, use the following command. (here instead of 8081, you can add the port number you wish).

mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8081'

You can also run the microservice with the “java -jar <file name>” command, provided that the following plugin is added to the pom.xml file. You need to specify the mainClass value pointing to the class where you have the main method. This will re-package the project and the jar file will contain the dependencies as well. When you run the jar file, the service will be started in the default port which is 8080. If you want to change the default port, run the command “java -jar <file name> --server.port=<port number>

<build>
  <plugins>
      <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
              <fork>true</fork>
              <mainClass>com.tharindue.App</mainClass>
          </configuration>
          <executions>
              <execution>
                  <goals>
                      <goal>repackage</goal>
                  </goals>
              </execution>
          </executions>
      </plugin>
  </plugins>
</build>

In my case, the service starts in 1.904 seconds. It’s a pretty good speed comparing the hassle you have to go through building a war file and then deploying it in an app service like tomcat. 


The REST services can be invoked as following using curl.






You can also use a browser plugin like RESTClient for testing the API.

So, that’s it ! You have an up and running micro service !



Tharindu Edirisinghe
Platform Security Team
WSO2

1 comment:

  1. You are working on API for handling user operations and expose the operations as RESTful services. It is a great effort and hopefully you will get best results.
    open source static code analysis tools

    ReplyDelete