Building a Spring Boot Microservices with Observability: A 2025 Guide
Hi, I’m Selene, and I’m thrilled to share this free Spring Boot microservices guide for 2025! With Spring Boot 3.3, building microservices is easier than ever, but observability is key to production success. In this tutorial, we’ll create an order service, covering essentials like @SpringBootApplication
, Spring Core, and observability with Prometheus and Grafana. Perfect for beginners or seasoned Java developers. Let’s dive into Spring Boot observability!
Coming from Medium? Non-members, enjoy this free tutorial with bonus tips! Explore more tech guides on my Medium profile.
Why Observability Matters for Spring Boot Microservices
In 2025, microservices power modern apps, but silent failures can derail them. Observability—tracking metrics, logs, and traces—ensures you catch issues early. Using Spring Boot Actuator, Prometheus for metrics, and Grafana for dashboards, we’ll make your microservice production-ready.
Prerequisites for Spring Boot 3.3
- Java 17 (Spring Boot 3.3 compatible)
- Maven installed
- Docker (for Prometheus and Grafana)
- IDE (IntelliJ IDEA, VS Code, or Eclipse)
- Basic Java knowledge (OOP, collections, exception handling)
Step 1: Java Basics for Spring Boot
Spring Boot builds on Java, so master these fundamentals:
- OOP: Classes, interfaces, inheritance for models and services.
- Collections: Use
List
andMap
for data (e.g., orders in our API). - Exception Handling: Manage REST endpoint errors with
try-catch
.
New to Java? Try Oracle’s Java tutorials for a quick refresher.
Step 2: Bootstrapping with Spring Initializr
Use Spring Initializr to create your project:
- Visit Spring Initializr.
- Configure:
- Build Tool: Maven
- Language: Java
- Spring Boot Version: 3.3.x (latest in 2025)
- Group:
com.example
- Artifact:
order-service
- Dependencies: Spring Web, Spring Boot Actuator, Micrometer Prometheus
- Click “Generate,” download the ZIP, unzip, and open in your IDE.
Spring Boot Starters like spring-boot-starter-web
(REST APIs) and spring-boot-starter-actuator
(monitoring) simplify dependency management.
Step 3: Spring Core and @SpringBootApplication
Your app’s entry point is:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
@SpringBootApplication
combines:
@Configuration
: Defines beans for services or controllers.@EnableAutoConfiguration
: Enables autoconfiguration, setting up components like Tomcat based on dependencies.@ComponentScan
: Scans for@Controller
,@Service
, etc.
Spring Core leverages Dependency Injection (DI) to inject services and Inversion of Control (IoC) for loose coupling, key to Spring Boot microservices.
Step 4: Building an Order Microservice
Create a REST API for orders. Model:
package com.example.orderservice.model;
public class Order {
private Long id;
private String product;
private double price;
public Order(Long id, String product, double price) {
this.id = id;
this.product = product;
this.price = price;
}
// Getters, setters
}
Service:
package com.example.orderservice.service;
import com.example.orderservice.model.Order;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class OrderService {
private final List orders = new ArrayList<>();
public List getAllOrders() {
return orders;
}
public Order addOrder(Order order) {
orders.add(order);
return order;
}
}
Controller:
package com.example.orderservice.controller;
import com.example.orderservice.model.Order;
import com.example.orderservice.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/orders")
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping
public List getAllOrders() {
return orderService.getAllOrders();
}
@PostMapping
public Order addOrder(@RequestBody Order order) {
return orderService.addOrder(order);
}
}
Run (mvn spring-boot:run
) and test with Postman:
- GET
http://localhost:8080/orders
to list orders. - POST
http://localhost:8080/orders
with JSON:{"id": 1, "product": "Laptop", "price": 999.99}
.
This uses Spring MVC for HTTP handling, a core part of Spring Boot microservices.
Step 5: Spring Boot Observability with Actuator and Prometheus
Enable Actuator metrics in application.properties
:
management.endpoints.web.exposure.include=health,metrics,prometheus
Access http://localhost:8080/actuator/prometheus
for metrics like HTTP request counts. This Spring Boot observability setup ensures robust monitoring in 2025.
Set up Prometheus and Grafana with Docker:
- Create
docker-compose.yml
:version: '3' services: prometheus: image: prom/prometheus ports: - "9090:9090" volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml grafana: image: grafana/grafana ports: - "3000:3000"
- Create
prometheus.yml
:scrape_configs: - job_name: 'spring-boot' static_configs: - targets: ['host.docker.internal:8080']
- Run
docker-compose up
. Access:- Prometheus:
http://localhost:9090
- Grafana:
http://localhost:3000
(login: admin/admin)
- Prometheus:
In Grafana, add Prometheus as a data source and visualize metrics like http_server_requests_seconds_count
in this Prometheus Grafana tutorial setup.
Step 6: Testing Your Microservice
Add spring-boot-starter-test
to pom.xml
and test:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class OrderControllerTest {
@Autowired
private MockMvc mvc;
@Test
void testGetAllOrders() throws Exception {
mvc.perform(get("/orders")).andExpect(status().isOk());
}
}
Run mvn test
to ensure your API works.
Why Spring Boot Microservices Matter in 2025
Microservices are the backbone of modern apps, and observability is non-negotiable. Spring Boot’s autoconfiguration and starters streamline setup, while Actuator, Prometheus, and Grafana provide insights into performance. This mirrors real-world deployments.
Next Steps for Spring Boot Developers
- Add a database with
spring-boot-starter-data-jpa
. - Secure your API with
spring-boot-starter-security
. Check my upcoming security tutorial. - Deploy to AWS or Azure using Docker. Learn Docker basics here.
Bonus Tips for Spring Boot Observability
Level up your microservices monitoring with these tips:
- Custom Metrics: Use Micrometer for Spring Boot observability to track business metrics (e.g., order count) in Prometheus.
- Grafana Dashboards: Set alerts for high error rates. Explore Grafana’s official docs for templates.
- Logging: Integrate SLF4J with Logback for detailed logs alongside metrics.
Post a Comment