Summary of Spring Cloud Interview Questions — Peerless Son

What is Spring Cloud ?

The Spring  Cloud application starter is a Spring integration application based on the starter Spring Boot, providing integration with external systems. 
Spring  Cloud Task, a short-lived microservices framework for rapidly building applications that perform limited data processing.

What are the advantages using Spring Cloud ?

Spring  Cloud has a complete set of microservice solutions. Based on Spring Boot, it can achieve rapid integration and high development efficiency, so it can be called the gospel of small and medium-sized Internet companies. 
Spring  Cloud releases new features very frequently. At present, there are many major versions, and there is also a huge community support. 
Spring  Cloud focuses on all aspects of the entire service architecture. In Spring Cloud, there are all kinds of components, so that it has the advantages of rapid integration, convenience, and low cost.

What does service registration and discovery mean? How is Spring Cloud implemented ?

When we start a project, we usually do all the configuration in the properties file. 
Adding and modifying these properties becomes more complex as more services are developed and deployed. 
Some services may drop, and some locations may change. Changing properties manually can create problems. 
EureKa  service registration and discovery can help in this situation. Since all services are registered on the Eureka server and lookup is done by calling the Eureka server, there is no need to deal with any changes and processing of the service location.

What is the meaning of load balancing ?

Load balancing improves the distribution of workload across multiple computing resources such as computers, computer clusters, network links, central processing units or disk drives.
Load balancing is designed to optimize resource usage, maximize throughput, minimize response time and avoid overloading of any single resource, using multiple components for load balancing rather than a single component may improve reliability and availability through redundancy.
Load balancing typically involves specialized software or hardware, such as multi-layer switches or Domain Name System server processes.

What is Hystrix ? How does it achieve fault tolerance ?

Hystrix is ​​a latency and fault tolerance library designed to isolate access points to remote systems, services, and third-party libraries.
When failures are inevitable, stop cascading failures and achieve resiliency in complex distributed systems.

Typically for systems developed using a microservices architecture, there are many microservices involved that cooperate with each other.

Consider the following microservices :

Suppose if microservice 9 in the above diagram fails, then using the traditional approach we would propagate an exception but still crash the whole system.

The whole problem becomes more complicated as the number of microservices increases. The number of microservices can be as high as 1000.
This is where Hystrix comes in and we can use Hystrix's Fallback method functionality in this case.

Example:
Two services employee-consumer use the services exposed by employee-consumer.
The simplified diagram is as follows:

Suppose the service exposed by employee-producer throws an exception.
We define a fallback method using Hystrix in this case.
This fallback method should have the same return type as the exposed service.
If there is an exception in the exposed service, the fallback method will return some value.

What is a Hystrix circuit breaker ?

For some reason employee-consumer exposing the service throws an exception.
Using Hystrix in this case we define a fallback method.
If an exception occurs in the exposed service, the fallback method returns some default value.

If the exception in the firstPage method() continues to occur, the Hystrix circuit will be interrupted and the employee consumer will skip the firstPage method altogether and call the fallback method directly.
The purpose of the circuit breaker is to give the first page method or other methods that the first page method may call time to flow out and cause the exception to recover.
What might happen is that the problem that caused the exception has a better chance of recovery under a lighter load.

What is Netflix Feign ? What are its advantages ?

Feign  is a Java client-side binder inspired by Retrofit, JAXRS-2.0 and WebSocket. 
The first goal of Feign  is to unify the complexity of the constraint denominator to http apis, regardless of their stability.

In the employee-consumer example, we used the REST service exposed by employee-producer using a REST template.

But we have to write a lot of code to perform the following steps :

① Use the ribbon for load balancing.
② Get the service instance, and then get the base URL.
③ Use the REST template to use the service.

The code is as follows :

@Controller
public class ConsumerControllerClient {

        @Autowired
        private LoadBalancerClient loadBalancer;

public void getEmployee() throws RestClientException, IOException {

    ServiceInstance serviceInstance=loadBalancer.choose("employee-producer");

    System.out.println(serviceInstance.getUri());

    String baseUrl=serviceInstance.getUri().toString();

    baseUrl=baseUrl+"/employee";

    RestTemplate restTemplate = new RestTemplate();

    ResponseEntity<String> response=null;

    try{

response=restTemplate.exchange(baseUrl,HttpMethod.GET,getHeaders(),String.class);

    }catch (Exception ex){

        System.out.println(ex);

    }

    System.out.println(response.getBody());

}

The previous code, with a chance of exceptions like NullPointer, was not optimal.
We’ll see how to use Netflix Fegin to make calling easier and cleaner.
Fegin is also responsible for load balancing by default if the Netflix Ribbon dependency is also on the classpath.

What is Spring Cloud Bus ?

Spring Cloud Bus provides the ability to refresh configuration across multiple instances.

Example :

(Multiple employee producer modules get Eureka registered properties from Employee Config Module)

We have multiple applications that use Spring Cloud Config to read properties, and Spring Cloud Config reads these properties from GIT.

What happens if suppose the Eureka registry property in GIT is changed to point to another Eureka server.

In this case we will have to restart the service to get the updated properties.

There is another way to use actuator endpoint/refresh. But we will have to call this url individually for each module.

For example :

If Employee Producer1 is deployed on port 8080, call http:
//localhost:8080/refresh.

Same for Employee Producer2 http://localhost:8081/refresh and so on. This is troublesome again.

This is where Spring Cloud Bus comes into play.

If we refresh Employee Producer1, all other required modules are refreshed automatically.

This is especially useful if we have multiple microservices up and running. This is achieved by connecting all microservices to a single message broker.

Whenever an instance is refreshed, this event is subscribed to all microservices listening to this broker and they are also refreshed. Refreshing any single instance can be achieved by using endpoint/bus/refresh.

Leave a Comment

Your email address will not be published. Required fields are marked *