Chapter 2. Building microservices with Spring Boot
This chapter introduces microservices as an answer to the brittleness and complexity of large monolithic systems, especially those built with rigid waterfall practices. It contrasts tightly coupled, leaky, and monolithic applications with microservices that are constrained to a single purpose, loosely coupled through standard protocols, isolated in their data ownership, and independently built and deployed. The chapter also explains why these qualities matter in cloud environments, where applications must support fast feature delivery, high uptime, and flexible scaling under uneven demand.
The architectural guidance centers on decomposing a business domain into service candidates by listening to the nouns, verbs, and data boundaries that emerge from real business conversations. Using the EagleEye example, the text shows how customer and product needs point to separate services such as organization, license, contract, and assets, while emphasizing that service boundaries should be chosen pragmatically and refined over time. It also warns against poor service granularity: services that are too large become overloaded and hard to test, while services that are too small create excessive chatter, dependency chains, and CRUD-heavy designs that fail to capture business value.
The chapter then shifts to implementation and operations, showing how Spring Boot makes it practical to create a microservice with minimal ceremony. A simple licensing service is built with Maven, a bootstrap class annotated to start the application, and a REST controller that exposes endpoints using HTTP, URI structure, JSON, and status codes. Finally, the chapter broadens to DevOps concerns, explaining that successful microservices must be packaged as self-contained artifacts, configured externally, discoverable without hard-coded locations, and health-monitored so failing instances can be routed around; it closes by tying together architectural, development, and operational perspectives as the foundation for building production-ready microservices.
Figure 2.3. You use the data model as the basis for decomposing a monolithic application into microservices.
Figure 2.7. In the Service Assembly step, source code is compiled and packaged with its runtime engine.
Summary
- To be successful with microservices, you need to integrate in the architect’s, software developer’s, and DevOps’ perspectives.
- Microservices, while a powerful architectural paradigm, have their benefits and tradeoffs. Not all applications should be microservice applications.
- From an architect’s perspective, microservices are small, self-contained, and distributed. Microservices should have narrow boundaries and manage a small set of data.
- From a developer’s perspective, microservices are typically built using a REST-style of design, with JSON as the payload for sending and receiving data from the service.
- Spring Boot is the ideal framework for building microservices because it lets you build a REST-based JSON service with a few simple annotations.
- From a DevOp’s perspective, how a microservice is packaged, deployed, and monitored are of critical importance.
- Out of the box, Spring Boot allows you to deliver a service as a single executable JAR file. An embedded Tomcat server in the producer JAR file hosts the service.
- Spring Actuator, which is included with the Spring Boot framework, exposes information about the operational health of the service along with information about the services runtime.
FAQ
What are the defining characteristics of a microservice in this chapter?
A microservice is described as constrained, loosely coupled, abstracted, and independent. It has a narrow responsibility, communicates through non-proprietary interfaces like HTTP/REST, owns its own data, and can be built and deployed separately from other services.Why are microservices a good fit for cloud-based applications?
They fit cloud architectures because cloud applications often need rapid feature delivery, high uptime, and elastic scaling. Microservices make it easier to isolate faults, deploy changes independently, and scale only the parts of the system under load.How should a business domain be decomposed into microservices?
Start by looking for the main nouns, verbs, and data cohesion in the business problem. Nouns often point to core domains, verbs reveal actions and workflows, and cohesive data groups suggest natural service boundaries.What microservice candidates are identified for the EagleEye example?
Based on the simplified EagleEye data model and business interviews, the main microservice candidates are organization, license, contract, and assets services.How do you decide the right granularity for a microservice?
It is usually better to start with broader services and refactor later than to split too finely too early. A service may be too coarse if it has too many responsibilities or too many tables, and too fine-grained if services become highly interdependent or devolve into simple CRUD services.What are the best practices for designing microservice interfaces?
Use REST principles, clear URIs that communicate intent, JSON for requests and responses, and HTTP status codes to communicate success or failure. The goal is to make services intuitive and easy for developers to consume.When should you avoid using microservices?
Microservices may not be a good choice when your organization lacks operational maturity, when server or container sprawl would be hard to manage, when the application is small or departmental in scope, or when you need complex cross-service data transformations and strong transaction consistency.What are the first steps to build a Spring Boot microservice?
Begin with a skeleton project, define the Maven build, create a bootstrap class annotated with@SpringBootApplication, and then add a controller class to expose HTTP endpoints. Spring Boot reduces boilerplate and makes the service easier to start and package.Why is @RestController important in Spring Boot?
@RestController tells Spring that the class provides REST endpoints and automatically handles request and response serialization, typically to JSON. It also removes the need to manually add @ResponseBody to each method.