Featured Article

Spring Boot Microservices Example Step by Step Tutorial

Kenneth Jul 13, 2026

Embarking on a microservices journey using Spring Boot? You're at the right place. Microservices transform traditional, monolithic applications into scalable, loosely coupled services. In this comprehensive guide, we'll create a Spring Boot microservices example step by step. Let's dive in!

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

First, ensure you have Java 8 or later, Apache Maven, and your favorite IDE set up. We'll be using Spring Initializr to generate our project, so keep that handy too.

Microservices In Java
Microservices In Java

Setting Up the Project

Let's create our first microservice: a simple greeting service. Head over to Spring Initializr, select 'Maven Project' and add 'Web', 'Lombok', and 'OpenFeign' dependencies. Name your project 'greeting-service'.

Spring Boot Dependency Injection Made Easy
Spring Boot Dependency Injection Made Easy

Unzip the downloaded project, import it into your IDE, and run it. You should see "Hello, World!" printed in the console. Perfect! Now, let's create an API.

Creating the Greeting API

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

Remove the Application class generated by Spring Initializr. Create a new class GreetingController with the following content:

```java @RestController public class GreetingController { @GetMapping("/greet") public Greeting greet(@RequestParam(name = "name", defaultValue = "World") String name) { return new Greeting("Hello, " + name + "!"); } } ```

And a Greeting class:

```java import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class Greeting { private String content; } ```

Now, run the service and navigate to http://localhost:8080/greet in your browser. You should see {"content":"Hello, World!"}.

Spring boot Microservice Project to Upload & Download File Explained with Example | #springboot
Spring boot Microservice Project to Upload & Download File Explained with Example | #springboot

Testing the API

To test the API, add a GreetingControllerTest class and write a test for our greeting method:

```java import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class GreetingControllerTest { @Autowired private TestRestTemplate restTemplate; @Test public void testGreeting() { ResponseEntity response = restTemplate.getForEntity("/greet", String.class); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(response.getBody()).contains("Hello, World!"); } } ```

Run the test and ensure it passes. Great! We have our first microservice. Now, let's create another one.

How To Implement AOP In Spring Boot Application?
How To Implement AOP In Spring Boot Application?

Creating a Second Microservice

Follow the same steps to create a farewell-service. Create a FarewellController with a goodbye API:

Spring Boot + Eureka in 3 Steps ⚡
Spring Boot + Eureka in 3 Steps ⚡
Spring Boot GraphQL API Project Tutorial with Example for Microservice Developers
Spring Boot GraphQL API Project Tutorial with Example for Microservice Developers
How To Handle Exceptions & Errors In Spring Boot?
How To Handle Exceptions & Errors In Spring Boot?
Decouple Your Microservices with RabbitMQ 🐇✉️
Decouple Your Microservices with RabbitMQ 🐇✉️
Spring Boot MVC REST Annotations With Examples - JavaTechOnline
Spring Boot MVC REST Annotations With Examples - JavaTechOnline
Spring Boot Clean Architecture 📁🚀
Spring Boot Clean Architecture 📁🚀
Dockerize Your Spring Boot App 🐳📦
Dockerize Your Spring Boot App 🐳📦
Introduction aux microservices : ce qu'il faut savoir
Introduction aux microservices : ce qu'il faut savoir
5 Must -Know Use Cases of Spring Boot Microservice Application
5 Must -Know Use Cases of Spring Boot Microservice Application

```java @RestController public class FarewellController { // Similar to GreetingController } ```

And a Farewell class. Now, it's time to make these services communicate with each other.

Feign Client for Communication

In the farewell-service, add an OpenFeign client to fetch greetings from the greeting-service. Create a new class GreetingClient:

```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient("greeting-service") public interface GreetingClient { @GetMapping("/greet") Greeting greet(@RequestParam(name = "name", defaultValue = "World") String name); } ```

Now, in the FarewellController, fetch the greeting and create a farewell message:

```java import org.springframework.beans.factory.annotation.Autowired; @RestController public class FarewellController { @Autowired private GreetingClient greetingClient; @GetMapping("/farewell") public Farewell farewell(@RequestParam(name = "name", defaultValue = "World") String name) { Greeting greeting = greetingClient.greet(name); return new Farewell(greeting.getContent() + ", Goodbye, " + name + "!"); } } ```

Run both services and navigate to http://localhost:8081/farewell. You should see a combined greeting and farewell message. Beautiful! You've just created your first Spring Boot microservices example.

As we look towards the future, consider adding more microservices, creating a service registry, and even implementing eventual consistency. Here's your starting point, and the possibilities are endless. Happy coding!