Bird
Raised Fist0
Spring Bootframework~10 mins

Service-to-service communication in Spring Boot - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - Service-to-service communication
Service A wants data
Service A sends HTTP request
Service B receives request
Service B processes request
Service B sends HTTP response
Service A receives response
Service A uses data
Service A sends a request to Service B, which processes it and sends back a response. Service A then uses that response.
Execution Sample
Spring Boot
RestTemplate restTemplate = new RestTemplate();
String url = "http://serviceb/api/data";
String response = restTemplate.getForObject(url, String.class);
System.out.println(response);
Service A uses RestTemplate to call Service B's API and prints the response.
Execution Table
StepActionRequest URLResponseState Change
1Service A prepares HTTP GET requesthttp://serviceb/api/dataN/ARequest ready
2Service A sends request to Service Bhttp://serviceb/api/dataN/AWaiting for response
3Service B receives requesthttp://serviceb/api/dataN/AProcessing request
4Service B processes and prepares responsehttp://serviceb/api/data"Hello from Service B"Response ready
5Service B sends response backhttp://serviceb/api/data"Hello from Service B"Response sent
6Service A receives responsehttp://serviceb/api/data"Hello from Service B"Response received
7Service A prints responsehttp://serviceb/api/data"Hello from Service B"Output displayed
8EndN/AN/ACommunication complete
💡 Communication ends after Service A receives and uses the response.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 6Final
restTemplatenullnew RestTemplate()new RestTemplate()new RestTemplate()new RestTemplate()
urlnull"http://serviceb/api/data""http://serviceb/api/data""http://serviceb/api/data""http://serviceb/api/data"
responsenullnullnull"Hello from Service B""Hello from Service B"
Key Moments - 3 Insights
Why does Service A wait after sending the request?
Service A waits because it needs Service B's response before continuing. This is shown in execution_table step 2 where the state is 'Waiting for response'.
What happens if Service B takes time to process?
Service A remains waiting until Service B sends the response. The processing time is between steps 3 and 5 in the execution_table.
Why is the response stored in a variable?
Storing the response allows Service A to use the data later, like printing it in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'response' after step 6?
Anull
B"Hello from Service B"
C"Request sent"
D"Processing request"
💡 Hint
Check the 'Response' column at step 6 in the execution_table.
At which step does Service B send the response back to Service A?
AStep 3
BStep 4
CStep 5
DStep 7
💡 Hint
Look for the action 'Service B sends response back' in the execution_table.
If Service B never sends a response, what happens to Service A's state after step 2?
AIt remains 'Waiting for response'
BIt changes to 'Request ready'
CIt changes to 'Response received'
DIt changes to 'Output displayed'
💡 Hint
Refer to the 'State Change' column at step 2 and what happens if no response arrives.
Concept Snapshot
Service-to-service communication in Spring Boot:
- Use RestTemplate or WebClient to send HTTP requests.
- Service A sends request to Service B's URL.
- Service B processes and returns response.
- Service A receives and uses response.
- Communication is synchronous by default.
- Handle waiting and errors properly.
Full Transcript
Service-to-service communication means one service calls another to get data or perform actions. In Spring Boot, Service A uses RestTemplate to send an HTTP GET request to Service B's API URL. Service B receives the request, processes it, and sends back a response. Service A waits for this response, then uses it, for example, printing it. The process involves preparing the request, sending it, waiting, receiving the response, and using the data. Variables like 'response' hold the data returned. If Service B delays or fails to respond, Service A stays waiting or must handle errors. This flow is common in microservices to share data or trigger actions.

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

  1. Step 1: Understand microservices architecture

    Microservices are small services that work independently but often need to share data or trigger actions in other services.
  2. Step 2: Identify the role of service-to-service communication

    This communication allows microservices to interact and cooperate by exchanging data or requests.
  3. Final Answer:

    To allow different microservices to exchange data and work together -> Option B
  4. 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

  1. Step 1: Understand Spring bean creation

    To create a reusable RestTemplate, define a method annotated with @Bean inside a @Configuration class.
  2. Step 2: Check the correct syntax

    @Bean public RestTemplate restTemplate() { return new RestTemplate(); } correctly uses @Bean and returns a new RestTemplate instance.
  3. Final Answer:

    @Bean public RestTemplate restTemplate() { return new RestTemplate(); } -> Option C
  4. 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"?
WebClient client = WebClient.create("http://service-b/api/greet");
String response = client.get()
    .retrieve()
    .bodyToMono(String.class)
    .block();
System.out.println(response);
medium
A. "Hello from Service B"
B. null
C. An exception is thrown
D. "Error: Service not found"

Solution

  1. Step 1: Understand WebClient call

    The WebClient sends a GET request to the URL and retrieves the response body as a String.
  2. Step 2: Analyze the response handling

    The block() method waits for the response synchronously and returns the body content.
  3. Final Answer:

    "Hello from Service B" -> Option A
  4. Quick Check:

    WebClient returns response body string [OK]
Hint: block() waits and returns response body string [OK]
Common Mistakes:
  • Assuming asynchronous call returns immediately
  • Expecting null without response
  • Confusing error message with normal output
4. Identify the error in this Spring Boot service-to-service call using RestTemplate:
@Autowired
private RestTemplate restTemplate;

public String callService() {
    String url = "http://service-c/api/data";
    ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
    return response.getBody();
}
medium
A. getForEntity method does not exist
B. URL string is missing protocol
C. ResponseEntity cannot hold String type
D. RestTemplate bean is not defined in the configuration

Solution

  1. Step 1: Check RestTemplate injection

    The RestTemplate must be defined as a bean for @Autowired to inject it properly.
  2. Step 2: Verify URL and method usage

    The URL includes protocol and getForEntity is a valid method returning ResponseEntity<String>.
  3. Final Answer:

    RestTemplate bean is not defined in the configuration -> Option D
  4. Quick Check:

    Missing RestTemplate bean causes injection error [OK]
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

  1. 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.
  2. 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.
  3. 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
  4. Quick Check:

    Timeout + onErrorReturn = safe WebClient call [OK]
Hint: Use timeout() and onErrorReturn() for safe WebClient calls [OK]
Common Mistakes:
  • Using block(Duration) which is invalid
  • Trying to set timeout directly on RestTemplate
  • Ignoring error handling in WebClient calls