1. CRUD Spring Boot
Account Microservice
The Account microservice is responsible for managing user accounts, basically, almost every application has a user account system. This microservice provides the necessary endpoints to create, read, update, and delete accounts. The microservice is built using Spring Boot and follows the Domain-Driven Design (DDD) approach.
The microservice is divided into two main modules: account
and account-service
:
- the
account
module contains the API definition and the data transfer objects (DTOs) for the Account microservice; - the
account-service
module contains the service implementation, repository, and entity classes.
classDiagram
namespace account {
class AccountController {
+create(AccountIn accountIn): AccountOut
+delete(String id): void
+findAll(): List<AccountOut>
+findById(String id): AccountOut
}
class AccountIn {
-String name
-String email
-String password
}
class AccountOut {
-String id
-String name
-String email
}
}
namespace account-service {
class AccountResource {
+create(AccountIn accountIn): AccountOut
+delete(String id): void
+findAll(): List<AccountOut>
+findById(String id): AccountOut
}
class AccountService {
+create(AccountIn accountIn): AccountOut
+delete(String id): void
+findAll(): List<AccountOut>
+findById(String id): AccountOut
}
class AccountRepository {
+create(AccountIn accountIn): AccountOut
+delete(String id): void
+findAll(): List<AccountOut>
+findById(String id): AccountOut
}
class Account {
-String id
-String name
-String email
-String password
-String sha256
}
class AccountModel {
+create(AccountIn accountIn): AccountOut
+delete(String id): void
+findAll(): List<AccountOut>
+findById(String id): AccountOut
}
}
<<Interface>> AccountController
AccountController ..> AccountIn
AccountController ..> AccountOut
<<Interface>> AccountRepository
AccountController <|-- AccountResource
AccountResource *-- AccountService
AccountService *-- AccountRepository
AccountService ..> Account
AccountService ..> AccountModel
AccountRepository ..> AccountModel
This approach allows the separation of concerns and the organization of the codebase into different modules, making it easier to maintain and scale the application. Also, it creates a facility to reuse the microservice by other microservices in the future - builts in Java.
The construction of the Account microservice follows the Clean Architecture approach, which promotes the total decoupling of business rules from interface layers. The diagram below illustrates the flow of data among the layers of the Account microservice:
sequenceDiagram
title Clean architecture's approach
Actor Request
Request ->>+ Controller:
Controller ->>+ Service: parser (AccountIn -> Account)
Service ->>+ Repository: parser (Account -> AccountModel)
Repository ->>+ Database:
Database ->>- Repository:
Repository ->>- Service: parser (Account <- AccountModel)
Service ->>- Controller: parser (AccountOut <- Account)
Controller ->>- Request:
Previously to build the Account microservice, it is necessary to prepare the environment by installing the database to persist the data. For that, we will use a Docker Compose file to create a PostgreSQL container, as well as, a cluster to isolate the microservices from external access, creating a secure environment - trusted layer. A Docker Compose file is a YAML file that defines how Docker containers should behave in production. The file contains the configuration for the database, the microservices, and the network configuration.
architecture-beta
service internet(internet)[request]
group api(cloud)[Docker Compose]
service db(database)[db] in api
service account(server)[account] in api
internet:R --> L:account
db:L -- R:account
Docker Compose
[+] Running 2/2
✔ Network store_default Created 0.1s
✔ Container store-db-1 Started 0.2s
Account
📁 api
└── 📁 account
├── 📁 src
│ └── 📁 main
│ └── 📁 java
│ └── 📁 store
│ └── 📁 account
│ ├── 📄 AccountController.java
│ ├── 📄 AccountIn.java
│ └── 📄 AccountOut.java
└── 📄 pom.xml
Source
Account-Service
📁 api
└── 📁 account-service
├── 📁 src
│ └── 📁 main
│ ├── 📁 java
│ │ └── 📁 store
│ │ └── 📁 account
│ │ ├── 📄 Account.java
│ │ ├── 📄 AccountApplication.java
│ │ ├── 📄 AccountModel.java
│ │ ├── 📄 AccountParser.java
│ │ ├── 📄 AccountRepository.java
│ │ ├── 📄 AccountResource.java
│ │ └── 📄 AccountService.java
│ └── 📁 resources
│ └── 📄 application.yaml
├── 📄 pom.xml
└── 📄 Dockerfile