Did you encounter these common spring cloud questions during the interview? Do you know how to solve it perfectly?

Time:2021-11-27

Did you encounter these common spring cloud questions during the interview? Do you know how to solve it perfectly?

Why do I need to learn spring cloud

Both commercial applications and user applications are very simple in the early stage of business. We usually implement them as single structure applications. However, with the gradual development of business, the product idea will become more and more complex, and the application of monomer structure will become more and more complex. This will bring the following problems to the application:

  • Disordered code structure:The business is complex, resulting in a large amount of code, and the management will be more and more difficult. At the same time, it will also bring great challenges to the rapid iteration of business;
  • Low development efficiency:Developers develop a set of code at the same time, it is difficult to avoid code conflict. The development process will be accompanied by the process of constantly resolving conflicts, which will seriously affect the development efficiency;
  • High cost of Troubleshooting:The process of finding and fixing bugs in online business may be very simple. However, because there is only one set of code, it needs to be recompiled, packaged and online, which is very expensive.

Due to the application of monomer structure, with the increase of system complexity, a variety of problems will be exposed. In recent years, microservice architecture has gradually replaced monomer architecture, and this trend will become more and more popular. Spring cloud is the most commonly used microservice development framework, which has been widely used in enterprise development.

What is spring cloud

Spring cloud is an ordered collection of frameworks. Taking advantage of the development convenience of spring boot, it cleverly simplifies the development of distributed system infrastructure, such as service discovery and registration, configuration center, intelligent routing, message bus, load balancing, circuit breaker, data monitoring, etc., which can be started and deployed with one click with the development style of spring boot.

Spring cloud does not repeatedly manufacture wheels. It just combines the relatively mature and practical service frameworks developed by various companies, re encapsulates them through the spring boot style, shields the complex configuration and implementation principles, and finally leaves a set of simple, easy to understand, easy to deploy and easy to maintain distributed system development toolkit for developers.

Design objectives and advantages and disadvantages

design goal

Coordinate various micro services and simplify distributed system development.

Advantages and disadvantages

There are so many microservice frameworks, such as Dubbo and kubernetes. Why use spring cloud?

advantage:

  • Spring comes from the spring family. Spring is unmatched in the enterprise level development framework and has a great background, which can ensure the subsequent update and improvement
  • Rich components and complete functions. Spring cloud provides very complete support for microservice architecture. For example, configuration management, service discovery, circuit breaker, microservice gateway, etc;
  • The spring cloud community is highly active and has rich tutorials. It is easy to find solutions to problems
  • The service splitting granularity is finer and the coupling degree is relatively low, which is conducive to the reuse of resources and the improvement of development efficiency
  • It can more accurately formulate and optimize the service scheme and improve the maintainability of the system
  • To reduce the cost of the team, you can develop in parallel without paying attention to how others develop, and pay attention to your own development first
  • Microservices can be cross platform and can be developed in any language
  • Suitable for the Internet era, the product iteration cycle is shorter

Disadvantages:

  • There are too many micro services and high governance costs, which is not conducive to the maintenance of the system
  • The high cost of distributed system development (fault tolerance, distributed transactions, etc.) poses great challenges to the team

Generally speaking, the advantages outweigh the disadvantages. At present, spring cloud is a very perfect distributed framework. At present, many enterprises begin to use microservices and the advantages of spring cloud are obvious. Therefore, for students who want to study micro service architecture, learning spring cloud is a good choice.

Development prospect of spring cloud

Spring cloud is a blessing for small and medium-sized Internet companies, because such companies often do not have the strength or sufficient capital investment to develop their own distributed system infrastructure. Using spring cloud one-stop solution can greatly reduce the development cost while calmly coping with business development.

Meanwhile, with the popularity of microservice architecture and docker container concept in recent years, spring cloud will also have a place in the future more and more “cloud” software development style. In particular, it provides standardized and all-stop technical solutions in a variety of distributed solutions, which may be as meaningful as the birth of servlet specification in that year, Effectively promote the progress of the technical level of the server software system.

Overall architecture

Did you encounter these common spring cloud questions during the interview? Do you know how to solve it perfectly?

Main items

The subprojects of spring cloud can be roughly divided into two categories. One is the encapsulation and abstraction of the existing mature framework “spring boot”, which is also the largest number of projects; The second type is the implementation of the infrastructure of some distributed systems. For example, spring cloud stream plays the role of Kafka and ActiveMQ.

Spring Cloud Config

The centralized configuration management tool is the unified external configuration management in the distributed system. Git is used to store the configuration by default. It can support the refresh, encryption and decryption of client configuration.

Spring Cloud Netflix

Netflix OSS open source component integration, including Eureka, hystrix, ribbon, feign, zuul and other core components.

  • Eureka: service governance component, including the registry of the server and the service discovery mechanism of the client;
  • Ribbon: load balancing service invocation component, with multiple load balancing invocation strategies;
  • Hystrix: Service fault-tolerant component, which realizes the circuit breaker mode and provides fault tolerance for service dependent errors and delays;
  • Feign: a declarative service invocation component based on ribbon and hystrix;
  • Zuul: API gateway component, which provides routing and filtering functions for requests.

Spring Cloud Bus

The message bus used to propagate cluster state changes uses lightweight message agents to link nodes in the distributed system, which can be used to dynamically refresh the service configuration in the cluster.

Spring Cloud Consul

Service governance component based on hashicorp consult.

Spring Cloud Security

The security toolkit supports load balancing oauth2 client and login authentication in zuul agent.

Spring Cloud Sleuth

The distributed request link tracking of spring cloud applications supports Zipkin, htrace and log based tracking (such as elk).

Spring Cloud Stream

The lightweight event driven microservice framework can use a simple declarative model to send and receive messages, mainly implemented as Apache Kafka and rabbitmq.

Spring Cloud Task

The microservice framework is used to quickly build short and limited data processing tasks, and is used to add functional and non functional features to applications.

Spring Cloud Zookeeper

Service governance component based on Apache zookeeper.

Spring Cloud Gateway

API gateway component, which provides routing and filtering functions for requests.

Spring Cloud OpenFeign

The declarative service invocation component based on ribbon and hystrix can dynamically create an interface implementation based on spring MVC annotation for service invocation. It has replaced feign as a first-class citizen in spring cloud 2.0.

Version relationship of spring cloud

Spring cloud is a comprehensive project composed of many subprojects. Each subproject has different release rhythms. In order to manage the version dependency between spring cloud and each subproject, a list is released, including the subproject version corresponding to a spring cloud version.

In order to avoid confusion between the spring cloud version number and the sub project version number, the spring cloud version adopts the name rather than the version number. The names of these versions adopt the name of London underground station and correspond to the version chronological order according to the alphabetical order. For example, angel is the first version and Brixton is the second version.

When the release content of spring cloud accumulates to the critical point or a major bug is solved, a “service releases” version, referred to as SRX version, will be released. For example, greenwich.sr2 is the second SRX version of Greenwich released by spring cloud. At present, the latest version of spring cloud is Hoxton.

Correspondence between spring cloud and springboot versions

Did you encounter these common spring cloud questions during the interview? Do you know how to solve it perfectly?

Corresponding relationship between spring cloud and each subproject version

Did you encounter these common spring cloud questions during the interview? Do you know how to solve it perfectly?

Note: the Hoxton version is based on springboot version 2.2. X and is not applicable to version 1.5. X. As springboot 1.5. X stops maintenance in August 2019, Edgware will also stop maintenance.

What is the difference between springboot and springcloud?

Springboot focuses on developing individual micro services quickly and easily.

Springcloud is a microservice coordination and management framework that focuses on the overall situation. It integrates and manages individual microservices developed by springboot,

It provides integrated services between micro services, such as configuration management, service discovery, circuit breaker, routing, micro agent, event bus, global lock, decision-making campaign, distributed session, etc

Springboot can independently use development projects without springcloud, but springcloud is inseparable from springboot and belongs to dependency

Springboot focuses on developing individual microservices quickly and conveniently, and springcloud focuses on the overall service governance framework.

When using spring boot to develop distributed microservices, we face the following problems

(1) Complexity associated with distributed systems – this overhead includes network problems, delay overhead, bandwidth problems, and security problems.

(2) Service discovery – service discovery tools manage how processes and services in a cluster find and talk to each other. It involves a service directory in which services are registered, and then services in the directory can be found and connected.

(3) Redundancy – the problem of redundancy in distributed systems.

(4) Load balancing — load balancing improves the distribution of workloads across multiple computing resources, such as computers, computer clusters, network links, central processing units, or disk drives.

(5) Performance issues – performance issues due to various operational overhead.

(6) Deployment complexity – Requirements for Devops skills.

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. As more and more services are developed and deployed, adding and modifying these properties becomes more complex. Some services may decline and some locations may change. Manually changing properties can cause problems.

Eureka service registration and discovery can help in this case. Since all services are registered on the Eureka server and the lookup is completed by calling the Eureka server, there is no need to process any changes and processing of the service location.

What is the difference between spring cloud and Dubbo?

(1) The service invocation method Dubbo is RPC springcloud rest API

(2) In the registry, Dubbo is zookeeper, spring cloud is Eureka, or zookeeper

(3) The service gateway is not implemented by Dubbo itself and can only be integrated through other third-party technologies. Springcloud has zuul routing gateway as a routing server to distribute consumers’ requests. Springcloud supports circuit breakers and is perfectly integrated with GIT. The configuration file supports version control, The event bus implements a series of micro service architecture elements, such as configuration file update and service automatic assembly.

What is the meaning of load balancing?

In computing, load balancing can improve the workload distribution across computers, computer clusters, network links, central processing units or disk drives. Load balancing aims to optimize resource usage, maximize throughput, minimize response time, and avoid overload of any single resource. Using multiple components for load balancing instead of a single component may improve reliability and availability through redundancy. Load balancing usually involves dedicated 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 delay and fault-tolerant library designed to isolate the access points of remote systems, services and third-party libraries, stop cascading failures when failures are inevitable, and realize resilience in complex distributed systems.

Generally, many microservices are involved in the system developed using microservice architecture. These microservices collaborate with each other.

Think about the following micro services

Did you encounter these common spring cloud questions during the interview? Do you know how to solve it perfectly?

Suppose that if the microservice 9 in the figure above fails, we will propagate an exception using the traditional method. But this will still cause the whole system to crash.

As the number of microservices increases, this problem becomes more complex. The number of microservices can be as high as 1000. This is where hystrix appears. We will use the fallback method function of hystrix in this case. We have two services, employee consumer, that use the services exposed by employee consumer.

The simplified diagram is shown below

Did you encounter these common spring cloud questions during the interview? Do you know how to solve it perfectly?

Now suppose that for some reason, the service exposed by employee producer will throw an exception. In this case, we define a fallback method using hystrix. This fallback method should have the same return type as the exposed service. If an exception occurs in the exposed service, the fallback method returns some values.

What is a hystrix circuit breaker? Do we need it?

For some reason, the employee consumer public service throws an exception. In this case, using hystrix, we define a fallback method. If an exception occurs in the exposed service, the fallback method returns some default values.

Did you encounter these common spring cloud questions during the interview? Do you know how to solve it perfectly?

If the exception in firstpage method() continues to occur, the hystrix circuit will be interrupted, and the employee users will skip the firstpage method together and call the fallback method directly. The purpose of the circuit breaker is to allow time for the first page method or other methods that the first page method may call and cause abnormal recovery. What may happen is that in the case of low load, the problems causing exceptions have better recovery opportunities.

Did you encounter these common spring cloud questions during the interview? Do you know how to solve it perfectly?

What is Netflix feign? What are its advantages?

Feign is a java client binder inspired by retrofit, jaxrs-2.0 and websocket.

Feign’s first goal is to unify the complexity of constraint denominators into HTTP APIs without considering its stability.

In the example of employee consumer, we use the rest Service exposed by employee producer using the rest template.

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

(1) Use the ribbon for load balancing.

(2) Get the service instance, and then get the basic URL.

(3) Use rest templates to use services. The previous 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 has the opportunity of exceptions such as nullpointer, which is not optimal. We’ll see how to use Netflix feign to make calls easier and cleaner. If the Netflix ribbon dependency is also in the classpath, feign will also be responsible for load balancing by default.

What is spring cloud bus? Do we need it?

Consider the following situation: we have multiple applications that use spring cloud config to read properties, and spring cloud config reads these properties from GIT.

In the following example, multiple employee producer modules obtain the property registered by Eureka from the employee config module.

Did you encounter these common spring cloud questions during the interview? Do you know how to solve it perfectly?

What happens if you assume that the Eureka registration 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 the actuator endpoint / refresh. But we will have to call this URL separately for each module. For example, if employee producer 1 is deployed on port 8080, http: / / localhost: 8080 / refresh is called. Similarly, for employee producer 2, http: / / localhost: 8081 / refresh, etc. It’s troublesome again. This is where spring cloud bus works.

Did you encounter these common spring cloud questions during the interview? Do you know how to solve it perfectly?

Spring cloud bus provides the function of refreshing configuration across multiple instances. Therefore, in the above example, if we refresh employee producer 1, all other required modules will be refreshed automatically. This is particularly useful if we have multiple microservices up and running. This is achieved by connecting all microservices to a single message broker. Whenever the instance is refreshed, this event will subscribe to all microservices listening on this agent, and they will also be refreshed. You can refresh any single instance by using endpoint / bus / refresh.

Function of spring cloud circuit breaker

When a service invokes another service and has problems due to network or self reasons, the caller will wait for the response of the callee. More service requests will be sent to these resources, resulting in more request waiting and chain effect (avalanche effect)

The circuit breaker is fully open: it cannot be called for a certain number of times within a period of time, and there is no sign of recovery after multiple monitoring. If the circuit breaker is fully open, the service will not be requested in the next request

Half open: if there is a sign of recovery in a short time, the circuit breaker will send some requests to the service. When calling normally, the circuit breaker will be closed

Close: the service can be called normally when it is always in a normal state

What is spring cloud config?

In the distributed system, due to the huge number of services, in order to facilitate the unified management and real-time update of service configuration files, distributed configuration center components are required. In spring cloud, there is a distributed configuration center component spring cloud config, which supports the placement of configuration services in the memory of the configuration service (i.e. local) and in the remote git warehouse. In the spring cloud config component, there are two roles: config server and config client.

use:

(1) Add POM dependency

(2) Add related configuration to configuration file

(3) Annotate startup class @ enableconfigserver

What is spring cloud gateway?

Spring cloud gateway is the second-generation gateway framework officially launched by spring cloud, replacing zuul gateway. As the of traffic, gateway plays a very important role in microservice system. The common functions of gateway include routing forwarding, permission verification, current limit control and so on.

A routelocatorbuilder bean is used to create a route. In addition to creating a route, routelocatorbuilder allows you to add various predictions and filters. The meaning of predictions assertion, as the name suggests, is handled by a specific route according to the rules of a specific request. Filters are various filters used to judge and modify requests.

Write at the end

Welcome to my official account.Calm as a yard】, a large number of Java related articles and learning materials will be updated in it, and the sorted materials will also be put in it.

If you think it’s good, just like it and pay attention! Pay attention, don’t get lost, keep updating!!!