Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Service-to-service communication with Spring Boot
📖 Scenario: You are building two small Spring Boot services that need to talk to each other. The first service will call the second service to get a greeting message.This is like when you ask a friend to get information for you from another friend. Here, one service asks another service for data.
🎯 Goal: Create two Spring Boot services where ServiceA calls ServiceB using RestTemplate to get a greeting message and display it.
📋 What You'll Learn
Create a Spring Boot application for ServiceB that returns a greeting string at /greeting endpoint
Create a Spring Boot application for ServiceA that calls ServiceB's /greeting endpoint using RestTemplate
Configure the base URL of ServiceB in ServiceA as a variable
Display the greeting message received from ServiceB in ServiceA's /show-greeting endpoint
💡 Why This Matters
🌍 Real World
Microservices often need to communicate with each other to share data or trigger actions. This project shows the basic way to do that using REST calls in Spring Boot.
💼 Career
Understanding service-to-service communication is essential for backend developers working with microservices architecture, enabling them to build scalable and modular applications.
Progress0 / 4 steps
1
Create ServiceB with a greeting endpoint
Create a Spring Boot controller class called GreetingController in ServiceB with a method getGreeting() mapped to /greeting that returns the string "Hello from ServiceB!".
Spring Boot
Hint
Use @RestController and @GetMapping annotations to create the endpoint.
2
Configure ServiceB base URL in ServiceA
In ServiceA, create a String variable called serviceBBaseUrl and set it to "http://localhost:8081" which is the base URL of ServiceB.
Spring Boot
Hint
This variable will hold the address where ServiceB is running.
3
Use RestTemplate in ServiceA to call ServiceB's greeting
In ServiceA, create a RestTemplate object called restTemplate. Then write a method fetchGreeting() that uses restTemplate.getForObject to call serviceBBaseUrl + "/greeting" and returns the greeting string.
Spring Boot
Hint
Use new RestTemplate() to create the client and getForObject to call the URL.
4
Create endpoint in ServiceA to show greeting from ServiceB
In ServiceA, create a Spring Boot controller class called ShowGreetingController with a method showGreeting() mapped to /show-greeting. This method should call fetchGreeting() and return the greeting string.
Spring Boot
Hint
Use @RestController and @GetMapping to create the endpoint that calls fetchGreeting().
Practice
(1/5)
1. What is the main purpose of service-to-service communication in Spring Boot microservices?
easy
A. To create user interfaces for microservices
B. To allow different microservices to exchange data and work together
C. To store data in a database
D. To compile Java code faster
Solution
Step 1: Understand microservices architecture
Microservices are small services that work independently but often need to share data or trigger actions in other services.
Step 2: Identify the role of service-to-service communication
This communication allows microservices to interact and cooperate by exchanging data or requests.
Final Answer:
To allow different microservices to exchange data and work together -> Option B
Quick Check:
Service communication = microservices working together [OK]
Hint: Microservices talk to each other to share data [OK]
Common Mistakes:
Confusing service communication with UI creation
Thinking it manages database storage
Assuming it speeds up code compilation
2. Which of the following is the correct way to create a RestTemplate bean in Spring Boot for service-to-service calls?
easy
A. @Component
public void restTemplate() {
return new RestTemplate();
}
B. @Service
public RestTemplate restTemplate() {
return new RestTemplate();
}
C. @Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
D. RestTemplate restTemplate = new RestTemplate();
Solution
Step 1: Understand Spring bean creation
To create a reusable RestTemplate, define a method annotated with @Bean inside a @Configuration class.
Step 2: Check the correct syntax
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
} correctly uses @Bean and returns a new RestTemplate instance.
Final Answer:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
} -> Option C
Quick Check:
@Bean method returns RestTemplate instance [OK]
Hint: Use @Bean to create reusable RestTemplate [OK]
Common Mistakes:
Using @Service instead of @Bean
Not returning RestTemplate instance
Missing @Bean annotation
3. Given the following Spring Boot code snippet using WebClient, what will be the output if the called service returns "Hello from Service B"?
Hint: Always define RestTemplate as a @Bean before autowiring [OK]
Common Mistakes:
Forgetting to create RestTemplate bean
Using incomplete URL
Misunderstanding getForEntity method
5. You want to call Service D from Service E using WebClient with a timeout of 2 seconds and handle errors gracefully. Which code snippet correctly implements this?
hard
A. WebClient client = WebClient.create("http://service-d/api");
String result = client.get()
.retrieve()
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(2))
.onErrorReturn("Timeout or error")
.block();
B. WebClient client = WebClient.create();
String result = client.get()
.uri("http://service-d/api")
.retrieve()
.bodyToMono(String.class)
.block(Duration.ofSeconds(2));
C. RestTemplate restTemplate = new RestTemplate();
restTemplate.setTimeout(2000);
String result = restTemplate.getForObject("http://service-d/api", String.class);
D. WebClient client = WebClient.builder()
.baseUrl("http://service-d/api")
.build();
String result = client.get()
.retrieve()
.bodyToMono(String.class)
.block();
Solution
Step 1: Setup WebClient with timeout and error handling
WebClient client = WebClient.create("http://service-d/api");
String result = client.get()
.retrieve()
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(2))
.onErrorReturn("Timeout or error")
.block(); uses timeout(Duration.ofSeconds(2)) to limit wait time and onErrorReturn to provide fallback on errors.
Step 2: Verify other options
WebClient client = WebClient.create();
String result = client.get()
.uri("http://service-d/api")
.retrieve()
.bodyToMono(String.class)
.block(Duration.ofSeconds(2)); but uses block(Duration) which times out and throws an exception instead of providing a fallback; RestTemplate restTemplate = new RestTemplate();
restTemplate.setTimeout(2000);
String result = restTemplate.getForObject("http://service-d/api", String.class); tries to set timeout on RestTemplate incorrectly; WebClient client = WebClient.builder()
.baseUrl("http://service-d/api")
.build();
String result = client.get()
.retrieve()
.bodyToMono(String.class)
.block(); lacks timeout and error handling.
Final Answer:
WebClient client = WebClient.create("http://service-d/api");
String result = client.get()
.retrieve()
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(2))
.onErrorReturn("Timeout or error")
.block(); -> Option A