Table of Contents
Building Microservices with Spring Boot
Book: Hands on Microservices with Spring Boot and Spring Cloud
Configuring Application
Config Files
- For each MS
build.gradle
or pom.xml
for dependencies
application.properties
or application.yml
to configure server, DB, and deployment (host/port + docker profile)
Dockerfile
to find what JAR to package into Docker image
- For landscape
settings.gradle
to import individual dependency files and build all MSes
docker-compose.yml
to list all MSes and deployment settings
Databases
- If you want your project to use a database, don’t install it locally.
- Instead, configure the project to use a Docker image for the database.
Error Handling
- Add this to config file to have Spring Boot include error details on failed HTTP responses
server.error.include-message=always
Building a v1.0 Backend
- Separate API/backend into layers:
- Objects: POJOs
- Controller / Protocol Layer: API-level code, endpoints/URIs.
- Service / Service Layer: Business logic before accessing database
- Repository / Persistence Layer: Code used to access/modify the database for transactions.
- Integration Layer: Only for composite microservice. Used to link to the service layer of core/internal microservices, so that it can call them to complete requests.
- Each microservice is its own complete app. It has its own separate:
- Config files
- Database
- Backend (all the components above)
Objects
- Entity objects mapped to the database.
- Use
@Entity
and @Table
to map to DB
@GeneratedValue
, GenerationType = Identity
to autogenerate unique table IDs on insertion
- API objects (optional) if you want to add extra service-specific fields to Entity objects.
- Add a field to track the version of each DB Entity
@Version
, int
- Keeps track of version of each entity in table. When a entity is updated, the version number is incremented.
- If you try to save an object with an outdated version, the request will fail.
repository.save(updatedObj)
, but updatedObj.version
< version of entity in DB.