Complete microservices architecture with real examples — Step 1 — Department microservice

Hello everyone! Today we are going to show you how you can create a microservice as a part of a complete microservices architecture that we will implement step by step in future posts. You can see the full architecture in the below diagram.

Microservices Architecture

As the first step, we will implement Department Service in this post. You can find all details step by step of Department Service with all source code.

Step 1 — Department Microservice

Here we will quickly go to the technology stack that we are going to use in our implementation.

Initialization details

Spring Initialzr — Department Service
  • Project build tool: Maven
  • Language: Java
  • Spring boot: 2.4.2
  • Project Metadata

Group: com.agnasarp

Artifact: agnasarp-department-microservice

Name: agnasarp-department-microservice

Description: Agnasarp Department Microservice

Package name: com.agnasarp.department

Packaging: Jar

Java version: 8

  • Dependencies:

Spring Web: Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.

Spring Data JPA: Persist data in SQL stores with Java Persistence API using Spring Data and Hibernate.

H2 Database: Provides a fast in-memory database that supports JDBC API and R2DBC access, with a small (2mb) footprint. Supports embedded and server modes as well as a browser based console application.

Lomok: Java annotation library which helps to reduce boilerplate code.

Then, we will import the project into IntelliJ Idea.

Project structure

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.agnasarp</groupId>
<artifactId>agnasarp-department-microservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>agnasarp-department-microservice</name>
<description>Agnasarp Department Microservice</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

application.yml

server:
port: 8280

AgnasarpDepartmentMicroserviceApplication.java

package com.agnasarp.department;

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

@SpringBootApplication
public class AgnasarpDepartmentMicroserviceApplication {

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

}

DepartmentController.java

package com.agnasarp.department.controller;

import com.agnasarp.department.entity.Department;
import com.agnasarp.department.service.DepartmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/departments")

public class DepartmentController {
private static final Logger log = LoggerFactory.getLogger(DepartmentController.class);
@Autowired
private DepartmentService departmentService;

@PostMapping("/")
public Department saveDepartment(@RequestBody Department department) {
log.info("Inside saveDepartment method of DepartmentController");
return departmentService.saveDepartment(department);
}

@GetMapping("/{id}")
public Department getDepartmentById(@PathVariable("id") Long departmentId) {
log.info("Inside getDepartmentById method of DepartmentController");
return departmentService.getDepartmentById(departmentId);
}
}

DepartmentService.java

package com.agnasarp.department.service;

import com.agnasarp.department.entity.Department;
import com.agnasarp.department.repository.DepartmentRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DepartmentService {

private static final Logger log = LoggerFactory.getLogger(DepartmentService.class);
@Autowired
private DepartmentRepository departmentRepository;

public Department saveDepartment(Department department) {
log.info("Inside saveDepartment method of DepartmentService");
return departmentRepository.save(department);
}

public Department getDepartmentById(Long departmentId) {
log.info("Inside getDepartmentById method of DepartmentService");
return departmentRepository.getDepartmentByDepartmentId(departmentId);
}
}

DepartmentRepository.java

package com.agnasarp.department.repository;

import com.agnasarp.department.entity.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DepartmentRepository extends JpaRepository<Department, Long> {
Department getDepartmentByDepartmentId(Long departmentId);
}

Department.java

package com.agnasarp.department.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long departmentId;
private String departmentName;
private String departmentAddress;
private String departmentCode;
}

Service call

Create department scenario

curl --location --request POST 'http://localhost:8280/departments/' \ --header 'Content-Type: application/json' \ --data-raw '{ "departmentName":"IT", "departmentAddress":"Colombo 07, Srilanka", "departmentCode":"IT-001" }'

Get department scenario

curl --location --request GET 'http://localhost:8280/departments/1' \ --data-raw ''

Download source from github:

Originally published at https://www.agnasarp.com on February 17, 2021.

Agnasarp is a technology-focused blog that has enough information about cutting-edge technologies that you can use for your problems. Stay with us!