Java Microservices Interview Questions
Java Microservices Interview Questions
• Here, we have a server. The server can be Tomcat, jetty, or any type.
• In this server, we have deployed our application.
• Inside this web application, we need to deploy all the modules of an application.
• The outcome will be a WAR file. Only if we deploy the war file, these modules
will be available.
This is the Monolithic Architecture.
7. Explain SOA.
SOA refers to Service-Oriented Architecture.
• This SOA architecture is a collection of multiple services.
• These multiple services do communicate with each other by using some
standardized protocols.
• Also in this design approach, applications are build as a collection of services that
are loosely coupled.
It communicates with each service over a network and then implements a specific business
function. The communication could be the normal passing of data or more than two services
sharing some common activity or any type of coordination.
Through some
Within the same Using some
lightweight
application, standardized
protocols, all the
components protocols, services
services
communicate with communicate with
communicate with
each other. each other.
Communication each other.
It also maintains
It maintains It maintains
centralized
centralized decentralized
development and here
development and development and
the services are
components deployed services deployed
Development deployed as monolithic
as a single unit. independently.
and Deployment applications.
11. Name three commonly used tools for Java Spring Boot Microservices.
There are different tools used for Java Spring Boot Microservices, some important tools are,
1. Docker: This is a containerization tool that allows developers to put applications
and their dependencies in a lightweight container, and provide stability across
multiple environments.
2. Kubernetes: This is an open-source container orchestration tool and it automates
the scaling, deployment, and management of containerized applications. It offers
features like service discovery, load balancing etc.
3. Spring Cloud: This is a framework in the Spring ecosystem for building
microservice-based applications. It is used to develop cloud-native applications. It
offers features like service discovery, configuration management, circuit breakers
etc.
13. How to Process the Request and Response between Two Services?
By establishing communication between the two services, microservices can handle requests
and responses between any two services using XML (Extensible Mark-up Language) and
JSON (JavaScript Object Notation).
• XML and JSON are data exchange formats and it helps to generate requests and
responses between two services.
• Most important thing is the data exchange format and both the services have to
know the data exchange format to request and respond accordingly.
• If we compare both formats, JSON is very simple to use in Microservices.
14. What is WebClient and How Java Microservices Communicate using WebClient?
An interface called WebClient represents the primary access point for web requests. It is
also known as Reactive Web Client that is introduced in Spring 5. The new client is a non-
blocking, reactive solution that works over HTTP/1.1 protocol. Also, it is the replacement of
classic RestTemplate. We can use the WebClient for Java Microservices Communication by
the following approach.
Add this dependency to the pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
After adding this library, create a Bean for the WebClient in the configuration file like below:
@Bean
public WebClient webClient() {
return WebClient.builder().baseUrl(addressBaseUrl).build();
}
After creating Bean, it is ready for use in the Service Class file. We can refer the below code:
@Service
public class EmployeeService {
// -------------
@Autowired
private WebClient webClient;
// --------------
// Using WebClient
AddressResponse addressResponse = webClient.get().uri("/address/" +
id).retrieve().bodyToMono(AddressResponse.class).block();
employeeResponse.setAddressResponse(addressResponse);
return employeeResponse;
}
}
For more details please refer to this article: Spring Boot Microservices Communication using
WebClient with Example
}
Create an Interface and define it with @FeignClient annotation and declare calling method as
below:
@FeignClient(name = "giveHereServiceName", url = "provideUrlHere", path =
"provideContextPathHere")
public interface AddressClient {
@GetMapping("/address/{id}")
public ResponseEntity<AddressResponse>
getAddressByEmployeeId(@PathVariable("id") int id);
}
Now it is ready to be used in the service class file. You can see the below code:
@Service
public class EmployeeService {
// -------------
// Using FeignClient
ResponseEntity<AddressResponse> addressResponse =
addressClient.getAddressByEmployeeId(id);
employeeResponse.setAddressResponse(addressResponse.getBody());
return employeeResponse;
}
}
For more details please refer to this article: Spring Boot Microservices Communication using
FeignClient with Example
17. How Client Side Load Balancing Happens in Java Spring Boot Microservices?
When a load balancer put on the client side along with assigning load balancing
responsibilities to the client, this is called Client-Side Load Balancing. Spring Cloud
LoadBalancer is one of the most popular client-side load balancers offered by Spring Cloud.
Spring Cloud LoadBalancer can be used for Client Side Load Balancing in Microservices by
the following approach:
Add the following dependencies to pom.xml file.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
Client Side Load Balancing can be done with the help of LoadBalancerClient. We need to
write the following logic in our client microservice in the Service class.
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;
// Get metadata
String contextPath = serviceInstance.getMetadata().get("configPath");
@GetMapping("/address/{id}")
public ResponseEntity<AddressResponse>
getAddressByEmployeeId(@PathVariable("id") int id);
}
Make the following changes to the application.properties file.
address-service.ribbon.listOfServers=http://localhost:8081, http://localhost:8082
For more details please refer to this article: Spring Boot Microservices – Load Balancing
using Netflix’s Ribbon
19. How Eureka Server and Client Communicate with Each Other in Java Microservices?
Service discovery is one of the key issues in microservices-based architecture. The Eureka is
Netflix service discovery, consists of a discovery server and a client. The server can be
configured and deployed to maximize performance, with each server copying the status of
registered services to the others.
Suppose, our discovery service is running on the port number 5000,
server.port=5000
Now if we run our microservice then we may get the “Connection refused” error. To fix this,
we have to add the following line in the application.properties file of microservice.
eureka.client.service-url.defaultZone=http://localhost:5000/eureka/
This will fix our issue.
For more details please refer to this article: How Eureka Server and Client Communicate with
Each Other in Microservices?
20. How to Develop API Gateway in Java Spring Boot Microservices?
We can develop API Gateway in Java Spring Boot Microservices by using Spring Cloud
Gateway. Spring Cloud Gateway provides a library for creating API gateways over Spring
and Java. It provides a flexible way to submit standards-based requests, while focusing on
contextual issues such as security, resiliency, and monitoring Spring Cloud Gateway features
some of the most important ones are:
• It is Built on Spring Framework 5, Project Reactor, and Spring Boot 2.0
• We can integrate the Circuit Breaker with the Spring Cloud Gateway.
• We can integrate Spring Cloud DiscoveryClient.
• Predicates and filters are specific to routes.
• Path Rewriting.
• It can match routes on any request attribute.
For more details please refer to this article: Java Spring Boot Microservices – Develop API
Gateway Using Spring Cloud Gateway
22. What is Client Side Service Discovery in Java Microservices and Provide Some
Examples of It?
In microservices, Service discovery refers to the process of dynamically locating and
connecting to available services within a distributed system.
• In a microservice system, applications perform multiple tasks and that are
composed of many independently deployable services.
• Service discovery is critical to facilitate communication between these different
services.
• If we put Service Discovery in client side then we called it as Client Side Service
Discovery.
Example:
• Netflix Eureka
• Zookeeper
• Consul
For more details please refer to this article: Client Side Service Discovery in Microservices
23. What is Server Side Service Discovery in Java Microservices and Provide Some
Examples of It?
In microservices, Service discovery refers to the process of dynamically locating and
connecting to available services within a distributed system.
• In a microservice system, applications perform multiple tasks and that are
composed of many independently deployable services.
• Service discovery is critical to facilitate communication between these different
services.
• If we put Service Discovery in server side then we called it as Server Side Service
Discovery.
Example:
• NGNIX
• AWS ELB
For more details please refer to this article: Server Side Service Discovery in Microservices
25. Explain 5 Major Challenges and Solutions of Java Spring Boot Microservices
Architecture.
There are 5 challenges are mentioned below with solutions that we might face while
developing microservices applications.
• Challenge 1: Service Configuration Management
o Solution: Spring Cloud Config Server centralizes configuration
management for microservices.
• Challenge 2: Service Discovery and Registration
o Solution: Eureka enables dynamic service discovery and
registration.
• Challenge 3: Load Balancing Across Microservices
o Solution: Spring Cloud LoadBalancer distributes traffic evenly
among microservice instances.
• Challenge 4: Resilience and Fault Tolerance
o Solution: resilience4j-ratelimiter implements rate limiting to
maintain stability under heavy load.
• Challenge 5: Handling Failures and Circuit Breakers
o Solution: Circuit Breaker pattern with tools like Hystrix provides
fault isolation and fallback mechanisms.
For more details please refer to this article: 5 Major Challenges and Solutions of
Microservices Architecture
26. Tell Some Major Reasons to Choose Spring Boot For Microservices Development.
Here are some major reason to Choose Spring Boot For Microservices Development.
• Embedded Server
• Support Load Balancer
• Auto Configuration
• Minimal Code using Annotations
• Loose Coupling
• Dependency Management
• Open Source
For more details please refer to this article: Major Reasons to Choose Spring Boot For
Microservices Development
29. What is the Main role of Docker in Microservices and How to deploy microservices in
Docker?
The main role of Docker in microservices is to provide containerization, which allows each
microservice with its dependencies to coordinate with the runtime environment, ensuring
stability in unique environments.
Steps to deploy microservices in Docker:
• Step 1: Containerize each microservice by creating a Dockerfile specifying the
application’s dependencies and runtime configuration.
• Step 2: Build Docker images for each microservice using the docker
build command.
• Step 3: Push the built Docker images to a Docker registry such as Docker Hub or a
private registry.
• Step 4: Create Docker Compose or Kubernetes manifests defining the
configuration for deploying and orchestrating the microservices.
• Step 5: Deploy the Dockerized microservices using Docker Compose or
Kubernetes by running docker-compose up or applying the Kubernetes manifests
respectively.
• Step 6: Monitor and manage the deployed microservices using Docker CLI
commands or Kubernetes dashboard.