Featured Article

Spring Boot Multiple Microservices Example A Complete Guide To Mastering Modular Development

Kenneth Jul 13, 2026

Spring Boot, a popular Java-based framework, simplifies building microservices by providing a robust platform with embedded servers, automatic configuration, and minimal XML. Multiple microservices can be developed, deployed, and scaled independently when built with Spring Boot. Let's explore a practical example of creating multiple microservices using Spring Boot.

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

First, let's set up our project structure. We'll create three microservices: 'user-service', 'catalog-service', and 'order-service'. Each will have its own database and responsibilities, promoting loose coupling and high cohesion.

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

Setting up the Project

For this example, we'll use Spring Initializr (https://start.spring.io/) to generate our projects. Choose 'Spring Web' as a dependency for all services, and add 'Spring Data JPA' and the appropriate database driver (e.g., 'MySQL Driver') based on your preferred database.

Microservices In Java
Microservices In Java

Configure your IDE to manage multiple modules, adding the generated projects as modules within a parent project. This will allow easy dependency management between services.

Application Configuration

Spring Boot @Transactional Annotation Example - How to Manage Transactions using Spring Framework?
Spring Boot @Transactional Annotation Example - How to Manage Transactions using Spring Framework?

In each service's 'application.properties' file, configure the database connection and other necessary properties. You can use Placeholders like ${database.url} to ensure consistency across environments. Also, configure the server port to avoid conflicts (e.g., 'order-service.port=8082').

```properties spring.datasource.url=jdbc:mysql://localhost(user-service)/user_db spring.datasource.username=user_service_user spring.datasource.password=(user-service-password) spring.jpa.hibernate.ddl-auto=update order-service.port=8082 ```

Domain and DTO Modeling

Spring Cloud Gateway with Eureka Service Registry in Spring Boot
Spring Cloud Gateway with Eureka Service Registry in Spring Boot

Define your domain models (e.g., User, Product, Order) in each service's core package. Create DTOs (Data Transfer Objects) for communication between services using tools like MapStruct or dozer for object mapping. This promotes encapsulation and ensures that services communicate using well-defined, independent objects.

For instance, in 'user-service', create a 'UserDTO' with fields like id, name, email, and password:

```java public class UserDTO { private Long id; private String name; private String email; private String password; // Getters and setters } ```

Spring Boot + Kafka Example (Single and Multiple Consumer) in Java
Spring Boot + Kafka Example (Single and Multiple Consumer) in Java

Implementing the Services

Next, create RESTful controllers and repositories for each domain model. Use annotations like @RestController and @Repository to declare them as such. Implement the necessary CRUD operations in each service.

what are microserries? info sheet
what are microserries? info sheet
Top 10 Advanced Courses for Full Stack Java Developers
Top 10 Advanced Courses for Full Stack Java Developers
5 Essential Spring Boot Annotations with Examples in Java - Tutorial
5 Essential Spring Boot Annotations with Examples in Java - Tutorial
Top 3 Design Patterns and Best Practices Java Developers Can Learn From Spring Framework
Top 3 Design Patterns and Best Practices Java Developers Can Learn From Spring Framework
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
Rugged Winter Hunting Boots, Nicks Boots Lace To Toe, English Hunting Boots, Viking Winter Boots, Nicks Handmade Boots, Role Club Boots, Wesco Logger Boots Size Chart, Nicks Work Boots, Nicks Boots
Rugged Winter Hunting Boots, Nicks Boots Lace To Toe, English Hunting Boots, Viking Winter Boots, Nicks Handmade Boots, Role Club Boots, Wesco Logger Boots Size Chart, Nicks Work Boots, Nicks Boots
a pair of green rain boots with flowers in front of a wooden fence and field
a pair of green rain boots with flowers in front of a wooden fence and field
Spring Declarative Transaction Management Example - Dinesh on Java
Spring Declarative Transaction Management Example - Dinesh on Java
What is ContextLoaderListener in Spring MVC? Example Tutorial
What is ContextLoaderListener in Spring MVC? Example Tutorial

In 'order-service', you might have an 'OrderController' to handle order-related operations:

```java @RestController @RequestMapping("/order") public class OrderController { @Autowired private OrderRepository orderRepository; @GetMapping("/{id}") public ResponseEntity getOrder(@PathVariable Long id) { return orderRepository.findById(id).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build()); } // Implement other CRUD operations... } ```

Inter-service Communication

For communication between services, use RESTful APIs or gRPC. For this example, we'll use REST. In 'order-service', you might need data from 'user-service' or 'catalog-service'. Use the @FeignClient annotation to create a Feign client for each dependent service.

```java @FeignClient(name = "user-service", url = "${user-service.url}") public interface UserClient { @GetMapping("/user/{id}") UserDTO getUser(@PathVariable Long id); } ```

Microservices Discovery and Load Balancing

To enable service discovery and load balancing, add 'Spring Cloud Netflix Eureka' as a dependency to all services. Configure EurekaServer in one of the services or use a dedicated EurekaServer. In each service, configure the 'eureka.client.serviceUrl.defaultZone' property to point to the EurekaServer.

With this setup, services can register and discover each other dynamically, enabling load balancing and fault tolerance.

Deployment

Build and package each microservice as a standalone JAR file. Deploy them to your preferred environment, ensuring each service is accessible via its network address and port.

Consider using containers (e.g., Docker) and orchestration tools (e.g., Kubernetes) for consistent deployment across environments and easier service management.

With these steps, you've created a practical example of multiple microservices using Spring Boot. Each service is independent, with its own database and responsibilities, yet communicates seamlessly with others when needed. To explore further, consider adding security, monitoring, and centralized logging across your microservices.