Essential Spring Boot Practices for Building Robust Microservices
This article outlines the golden rules for constructing Spring Boot microservices, covering monitoring with Spring Boot Admin and Grafana, exposing metrics via Actuator, centralized logging with ELK, clear API documentation using Swagger, YApi or smart‑doc, transparent build info, and keeping dependencies up‑to‑date.
Introduction
In this article we share the "golden rules" for building Spring Boot microservice applications.
System Runs on a White Box
Monitoring and visualization systems such as Spring Boot Admin or Grafana can reveal most runtime issues, provide charts and alerts, and are especially important in microservice architectures.
Spring Boot Actuator can expose runtime metrics; adding the following dependency enables it:
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</code>The metric
http.server.requestsis a key indicator, offering request count and response time statistics.
Common storage backends for Actuator metrics are InfluxDB and Prometheus; Prometheus pulls data from endpoints, while InfluxDB requires the application to push via REST API. In small projects, Spring Boot Admin (in‑memory) can be used.
Be sure to block external exposure in production environments.
Logging
Logging is often overlooked during development but becomes critical for operations; it should be a key review and testing criterion.
Use a consistent logging standard across microservices, record trace IDs with MDC, differentiate instances, and capture request execution times.
Centralized log storage is recommended; the Elastic Stack (ELK) is a popular choice.
<code><!-- integrate logstash -->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.3</version>
</dependency>
</code>For small projects or limited expertise, cloud provider log services (e.g., Qiniu LogKit, LogEasy) are inexpensive alternatives.
Ensuring Clear API Documentation
Microservice APIs require timely and accurate documentation.
Swagger is a well‑known tool for generating online API docs with minimal configuration.
For a more comprehensive DevOps workflow, YApi offers powerful API management, supporting Swagger import and real‑time maintenance.
Dubbo services can use smart‑doc, which generates documentation from annotations without code intrusion.
Application Information Transparency
Rapid delivery in microservice environments demands exposing version and change information for each release.
The Spring Boot Maven plugin can generate build info; accessing
/actuator/inforeveals details such as artifact, version, build time, and group.
<code><plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</code>Visit
/actuator/infoto see build metadata.
SpringBootAdmin (with git‑commit‑id‑plugin) can list registered applications and display their version or commit information.
Timely Dependency Updates
Spring Boot and Spring Cloud release new versions monthly, bringing features and bug fixes; newer versions improve startup time and memory usage, making them well‑suited for containerization.
Example Maven parent and dependencyManagement configuration for the latest stable releases:
<code><parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</code>Spring Cloud version details are illustrated below.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.