Java Microservice Layer Architecture

Dennis Brysiuk
NEW IT Engineering
Published in
3 min readMay 13, 2022
Microservice Layer Architecture

In the WWW you can find a lot about Mircoservice Architecture, but the layer Architecture is usually skipped. This article should serve as an aid by creating a Microservice. The described Architecture is based on my personal experience.

Responsibilities of the individual functionalities are kept separate and modular by a separation of Microservice in 5 layers.

1. Presentation Layer

The presentation layer is only responsible for the interface, it defines the URLs for the endpoints and secures the application. In addition, it forwards requests to the underlying layer, the business layer.

2. Business Layer

The business layer contains the core logic of the system. All algorithms and data processing are performed in this layer. Data required processes are queried from the layer below, the persistence layer.

3. Persistence Layer

The persistence layer has four tasks: reading, storing, deleting and updating data. To hide and unify access to the data sources from business layer, this layer implements a interface for the data from all data sources.

4. Model Layer

The Model Layer encapsulates the data from the communication between external client and presentation layer (data transfer objects — DTOs) and/or between business and presentation layer (business entity — entity)

5. Database Layer

The database layer realized the basis of the data interface of the services, the database.

Please note that database layer is physically outsourced!

Package Structure

The Layer architecture consider the high level layers of the complete software architecture of the services and their tasks. The structure of the four upper layers (presentation, business, persistence and model), which are in the source code of the application, is described in the package structure.

The database layer is not considered there!

Package Structure

1. Controller Package

Controller Package is responsible for the endpoints (REST controller).

2. Service Package

Service Package contains all algorithms and business logic for data processing.

3. Repository Package

Repository Package processes and collects data from the various data sources.

4. Model Package

Model Package includes business, transfer data models and mapper between them.

4.1 DTO

Data Transfer Object is an abstract model object that organizes response and request model standardizes.

4.1.1/4.1.2 Response/Request Sub-Package

Represents data transfer model that are processed or responses by the application. Response/Request inherits the structure from the Model DTO class.

4.2 Entity

Entity is a lightweight persistence domain object

4.3 Mapper

Mapper represents map generator between object models (entities and DTOs).

5. Config Package

Config Package indicates @Bean methods and processed by the Java container to generate bean definitions and service requests for those beans at runtime.

6. Common Package

Common package includes helper and util classes, interfaces and methods that could be used in entire microsevice.

6.1 Internal Sub-Package

Internal Package contains the implementation for the internal communications between the application services.

6.2 External Sub-Package

External Package contains the implementation for the external communications of the 3rd party services.

6.3 Exception Sub-Package

Exception Package contains the exception handling for the service.

6.4 Util Sub-Package

Util includes general helper methods and classes.

--

--