Anatomy of a Microservice - Components and Structure
Tags: java, microservices
Published: 2022-10-31 01:06:36

## 1. Introduction
Are you new to microservice and are wondering how you should create your projects? Then this article is right for you.

Microservice is more like the successor to SOA or Service Oriented Architecture. It lets you create a software product built from a set of related services loosely coupled manner. The idea is to generate each service as lightweight and fine-graned as possible. The goal is to deliver them independently of each service. This, in theory, should enable other teams to simultaneously provide services, which means faster delivery.

## 2. Requirements of a Microservice Project
A microservice should be able to provide the following functionalities.

### 2.1 Business Logic
The heart of the service. It usually provides a single feature for the application. In most cases, this module is web-based and contains the persistence layer. For example, in an online service marketplace, this could be an IAM or order service.

### 2.2 API Client
This module exposed the business logic consumed by the other services. It doesn't contain any business logic by itself but instead provides an abstraction to retrieve or save information to the application.

### 2.2 API DTO
This module contains the abstraction provided by the business logic and consumed by the client.

## 3. Anatomy of the Project
This section will convert the 3 modules we have identified into a more concrete example using Maven and Spring Boot technology.

A single microservice will have a parent maven project with 3 sub-modules.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxzibszebywp6jr0xif4.png)

### 3.1 app
A Spring Boot REST project. It contains the endpoints or controllers of the application that will be abstracted in the client sub-module. It's commonly designed to follow the MVC pattern. It uses Spring web and persistence dependencies

### 3.2 api
A Java project that contains the DTOs that are the request and response objects of the business logic. It only requires validation, annotation, and Lombok dependencies.

```
<dependencies>
<!-- Toolkit -->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
</dependency>
 
<!-- Validation -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
</dependency>
 
<!-- Utilities -->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>
```

### 3.3 client
A Java project where you can use any library to provide REST access. You can use apache-HTTP, OpenFeign, etc. In my case, I normally use OpenFeign, or if I'm using API first, I use apache-http.

With OpenFeign, you can easily proxy a REST endpoint using annotations. Here's a sample proxy for a booking endpoint.

```
@FeignClient(name = "gigBookingProxy", url = "${app.cx.client.booking-services.url}")
public interface GigBookingProxy {
 
  @PostMapping(
      path = EndpointConstants.PATH_GIG_BOOKINGS,
      produces = MediaType.APPLICATION_JSON_VALUE)
  @ResponseStatus(value = HttpStatus.OK)
  GigBookingDto placeBooking(@PathVariable(name = "gigId") @Valid @NotNull Long gigId,
      @RequestBody @Valid @NotNull CreateBookingDto createBookingDto);
}
```

Here's the actual endpoint from the app sub-module.

```
@PostMapping(
  path = EndpointConstants.PATH_GIG_BOOKINGS,
  produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(value = HttpStatus.OK)
public CompletableFuture<GigBookingDto> placeBooking(@PathVariable(name = "gigId") @Valid @NotNull Long gigId,
  @RequestBody @Valid @NotNull CreateBookingDto createBookingDto) {
return gigBookingService.placeBooking(gigId, web2ServiceMapper.toCreateBooking(createBookingDto))
    .thenApply(service2WebMapper::toGigBookingDto);
}
```

The app and the client module depend on the api module.

## 4. docker
You can create a docker folder to put your Dockerfile and docker-compose file at the same level as the sub-modules.

## 5. Github Repository
For a more detailed code, here's the GitHub repository https://github.com/czetsuya/sparqr-feign-projects.