Featured Article

Spring Boot Microservices Full Example: Build Scalable Cloud Apps

Kenneth Jul 13, 2026

Spring Boot, a popular Java-based framework, simplifies the development of microservices architectures by providing a comprehensive, opinionated solution for creating production-ready applications. Microservices, an architectural style that structures an application as a suite of small, independent services, enables continuous delivery, improved scalability, and enhanced fault isolation. Let's explore a full example of building a microservices architecture using Spring Boot.

Spring Boot and Microservices Roadmap for Beginners
Spring Boot and Microservices Roadmap for Beginners

Before delving into the example, ensure you have Java 8 or later installed, as well as Apache Maven or Gradle for build management. We'll be using Spring Boot 2.5.1 for this example.

Java + Spring Boot + Microservices Example - Step by Step Guide
Java + Spring Boot + Microservices Example - Step by Step Guide

Setting up the Project Structure

First, let's establish the project structure. We'll create three microservices: حساب (User), محصولات (Product), and سفارشات (Order). For simplicity, we'll use Maven and manually create the project structure.

Spring Boot Roadmap 2026: 8 Stages From Zero to Production-Ready
Spring Boot Roadmap 2026: 8 Stages From Zero to Production-Ready

Create three folders - user-service, product-service, and order-service - with the following contents:

Maven Dependencies

Learn Spring Boot by Annotations
Learn Spring Boot by Annotations

Each service requires the following dependencies in their pom.xml file:

  • Spring Boot dependencies: spring-boot-starter-web, spring-boot-starter-data-mongodb (or your preferred database), and spring-boot-starter-actuator
  • Lombok for boilerplate reduction (optional): lombok

Configuring the Application

Microservices with Spring Boot — Authentication with JWT and Spring Security
Microservices with Spring Boot — Authentication with JWT and Spring Security

Create an application.properties file in each service root with the appropriate configuration, such as database connections and server ports.

Set up a basic Spring Boot aplicación in each service using start.spring.io or your preferred IDE's generator. Ensure to select the required dependencies.

Developing the Microservices

Decouple Your Microservices with RabbitMQ 🐇✉️
Decouple Your Microservices with RabbitMQ 🐇✉️

Now, let's develop each microservice, starting with the User Service.

User Service

Spring Boot Folder Structure: Best Practices and Guide
Spring Boot Folder Structure: Best Practices and Guide
Spring Boot Clean Architecture 📁🚀
Spring Boot Clean Architecture 📁🚀
How To Develop REST API Using Spring Boot ?
How To Develop REST API Using Spring Boot ?
Spring Boot Version
Spring Boot Version
Dockerize Your Spring Boot App 🐳📦
Dockerize Your Spring Boot App 🐳📦
Top 10 Advanced Courses for Full Stack Java Developers
Top 10 Advanced Courses for Full Stack Java Developers
Monolith vs Microservices Architecture 🏗️🚀
Monolith vs Microservices Architecture 🏗️🚀
Microservices Architecture Explained 🏗️🚀
Microservices Architecture Explained 🏗️🚀
an audience is watching two performers on stage in front of a large group of people
an audience is watching two performers on stage in front of a large group of people

The User Service will manage user-related operations. Use Spring Data MongoDB for data storage and Spring Web for REST APIs.

Create a simple_user model class, ensuring to annotate fields with @Id, @Field, etc., for MongoDB mapping. Then, create a UserRepository interface extending MongoRepository, followed by a UserService class with methods for saving, finding, updating, and deleting users.

Create a REST controller, UserController, with endpoints for user CRUD operations, utilizing UserService for business logic.

Product Service

The Product Service will handle product data. Follow the same approach as the User Service, creating models, repositories, services, and controllers for product-related operations.

To demonstrate inter-service communication, make the Order Service depend on the Product Service for fetching product data during order placement.

Order Service

The Order Service will manage order-related operations. Like the previous services, create order models, repositories, services, and controllers for order CRUD operations.

When creating an order, use @Autowired ProductService to fetch product data from the Product Service and attach it to the order.

With each service developed, you now have a working microservices architecture. To test the services, run them sequentially and use tools like Postman or curl to interact with their REST APIs.

Service Discovery and Load Balancing with Eureka and Spring Cloud

To enable service discovery and load balancing, integrate Spring Cloud's Eureka and Ribbon into your project.

Add the following dependencies to each service's pom.xml file:

  • spring-cloud-starter-netflix-eureka-client
  • spring-cloud-starter-netflix-ribbon

Update each service's application.properties file to connect to a running Eureka server. You can create a dedicated Eureka service or use the Spring Cloud's sample Eureka server.

With Ribbon and Eureka in place, the Order Service will automatically discover and communicate with the Product Service, eliminating the need for manual service locating.

API Composition and Gateway with Spring Cloud Gateway

For centralized API composition and routing, use Spring Cloud Gateway as a reverse proxy for your microservices.

Create a new project, api-gateway, with the following dependencies:

  • spring-boot-starter-webflux
  • spring-cloud-starter-gateway
  • spring-cloud-starter-netflix-eureka-client

Configure routes in the ApiGatewayApplication class, enabling access to your microservices through a single entry point.

Embracing microservices architectures with Spring Boot offers numerous benefits, including improved scalability, flexibility, and resiliency. As your application grows, consider adopting more advanced techniques like distributed tracing, event-driven architecture, and Kubernetes orchestration for managing your microservices.

Happy coding, and welcome to the world of microservices!