Home  >  Article  >  Java  >  Interview feedback Spring Cloud's 25-shot series

Interview feedback Spring Cloud's 25-shot series

Java后端技术全栈
Java后端技术全栈forward
2023-08-24 15:57:321045browse


Preface

Some time ago, due to special reasons, our interviews were interrupted in a row What's more, it happened that last week an old man went to an interview and was asked about Spring Cloud, and based on his feedback, today we continue the Spring Cloud interview series.

Welcome everyone to follow me:

##Summary of Spring Cloud core knowledge

The following is a Spring Cloud core component relationship diagram:

Interview feedback Spring Cloud's 25-shot series


##From this picture, we can actually get a lot of information, I hope you can savor it carefully.

The following is a summary of the core components of

Spring Cloud Netflix

and Spring Cloud Alibaba: <figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;display: flex;flex-direction: column;justify-content: center;align-items: center;"><p style="text-align: center;"><img src="https://img.php.cn/upload/article/001/273/727/84b463de133d0a90c542eef5f76e291b-1.png" alt="Interview feedback Spring Cloud's 25-shot series" ></p></figure><p data-tool="mdnice编辑器" style="max-width:90%"># Without further ado, let’s start the Spring Cloud series directly. </p> <h2 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;border-bottom: 2px solid rgb(239, 112, 96);font-size: 1.3em;"> <span style="display: none;"></span><span style="display: inline-block;background: rgb(239, 112, 96);color: rgb(255, 255, 255);padding: 3px 10px 1px;border-top-right-radius: 3px;border-top-left-radius: 3px;margin-right: 3px;"> serial gun walking</span><span style="display: inline-block;vertical-align: bottom;border-bottom: 36px solid #efebe9;border-right: 20px solid transparent;"> </span> </h2> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 20px;"> <span style="display: none;">##1. What is Spring Cloud? </span><span style="display: none;"></span> </h3>Spring cloud streaming application starter is a Spring integration application based on Spring Boot that provides integration with external systems. Spring Cloud Task, a short-lived microservice framework for quickly building applications that perform limited data processing. <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;margin-top: 1px;margin-bottom: 1px;"></p> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 20px;"> <span style="display: none;">2. What are microservices? </span><span style="display: none;"></span> </h3>Microservice architecture is an architectural pattern or an architectural style that advocates dividing a single application into a set of small services, with each service running on its own independent In their own process, services coordinate and cooperate with each other to provide users with ultimate value. Services use lightweight communication mechanisms to communicate with each other (usually RESTful API based on HTTP). Each service is built around a specific business and can be independently built in a production environment, production-like environment, etc. In addition, a unified and centralized service management mechanism should be avoided. For a specific service, appropriate languages ​​and tools should be selected according to the business context to build it. There can be a very lightweight centralized management. To coordinate these services, different languages ​​can be used to write the services, and different data stores can be used. <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;margin-top: 1px;margin-bottom: 1px;"></p>In layman’s terms: <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;margin-top: 1px;margin-bottom: 1px;"></p>A microservice is an independent service application with a single responsibility. In the intellij idea tool, there are independent modules developed using maven. Specifically, it is a small module developed using springboot to handle a single professional business logic. Each module only does one thing. <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;margin-top: 1px;margin-bottom: 1px;"></p> Microservices emphasize the size of the service and focus on a certain point. A service application corresponding to a specific problem/implementation can be regarded as a module in the idea. <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;margin-top: 1px;margin-bottom: 1px;"></p> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 20px;"> <span style="display: none;">3. What are the advantages of Spring Cloud?</span><span style="display: none;"></span> </h3>When using Spring Boot to develop distributed microservices, we face the following problems<p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;margin-top: 1px;margin-bottom: 1px;"></p> <ul class="list-paddingleft-2" data-tool="mdnice编辑器" style="margin-top: 8px;margin-bottom: 8px;padding-left: 25px;"> <li><section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">Complexity associated with distributed systems - This overhead includes network issues, latency overhead, bandwidth issues, security issues. </section></li> <li><section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> Service Discovery - Service discovery tools manage how processes and services in a cluster find and talk to each other. It involves a service catalog, registering services in that catalog, and then being able to find and connect to services in that catalog. </section></li> <li><section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">Redundancy - Redundancy issues in distributed systems. </section></li> <li><section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);"> 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. </section></li> <li><section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">Performance-Issue Performance issues due to various operational overheads. </section></li> <li><section style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">Deployment Complexity - Requirements for Devops skills. </section></li> </ul> <h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;font-weight: bold;font-size: 20px;"> <span style="display: none;"></span>4. How do microservices communicate independently? <span style="display: none;"></span> </h3> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;margin-top: 1px;margin-bottom: 1px;">Synchronous communication: dobbo uses RPC remote procedure call , springcloud calls via REST interface json, etc. </p> <p data-tool="mdnice编辑器" style="padding-top: 8px;padding-bottom: 8px;line-height: 26px;margin-top: 1px;margin-bottom: 1px;">Asynchronous: message queue, such as: <code style='font-size: 14px;padding: 2px 4px;border-radius: 4px;margin-right: 2px;margin-left: 2px;background-color: rgba(27, 31, 35, 0.05);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(239, 112, 96);'>RabbitMq, ActiveM, Kafka and other message queues.

5. What is a service circuit breaker? What is service downgrade?

The circuit breaker mechanism is a microservice link protection mechanism to deal with the avalanche effect. When a certain microservice is unavailable or the response time is too long, the service will be degraded, thereby interrupting the call of the microservice on the node and quickly returning "error" response information. When it is detected that the node's microservice call response is normal, the call link is restored. In the Spring Cloud framework, the circuit breaker mechanism is implemented through Hystrix. Hystrix will monitor the status of calls between microservices. When failed calls reach a certain threshold, the default is 20 calls within 5 seconds. If it fails, the circuit breaker mechanism will be activated.

Service degradation is generally considered from the overall load. That is, when a service is disconnected, the server will no longer be called. At this time, the client can prepare a local fallback callback and return a default value. In this way, although the level is reduced, it is still usable, which is better than dying directly.

HystrixRelated Notes@EnableHystrix: Turn on circuit breaker@HystrixCommand(fallbackMethod="XXX"), declare a failure rollback processing functionXXX , when the annotated method execution times out (default is 1000 milliseconds), the fallback function will be executed and an error message will be returned.

6. Please tell me the difference between Eureka and zookeeper?

Zookeeper guarantees CP and Eureka guarantees AP.

A: High availability

C: Consistency

P: Partition fault tolerance

1. When querying the registration center When listing services, we can tolerate the registration center returning information from a few minutes ago, but we cannot tolerate being directly down and unavailable. In other words, the service registration function has relatively high requirements for high availability, but there will be a situation in ZooKeeper. When the master node loses contact with other nodes due to network failure, the remaining nodes will re-elect the leader. The problem is that the leader selection time is too long, 30 ~ 120s, and the zk cluster is not available during the selection period, which will cause the registration service to be paralyzed during the selection period. In a cloud deployment environment, it is a high probability that the zk cluster will lose the master node due to network problems. Although the service can be restored, the long-term unavailability of registration caused by the long selection time is intolerable.

2. Eureka ensures availability. Each Eureka node is equal. The failure of several nodes will not affect the work of normal nodes. The remaining nodes can still provide registration and query services. If a connection failure occurs when the Eureka client registers or discovers a certain Eureka, it will automatically switch to other nodes. As long as one Eureka is still available, the registration service can be guaranteed to be available, but the information found may not be the latest. In addition, Eureka also has a self-protection mechanism. If more than 85% of the nodes do not have normal heartbeats within 15 minutes, then Eureka will think that a network failure has occurred between the client and the registration center. At this time, the following situations will occur:

①. Eureka no longer removes services from the registration list that should expire because they have not received heartbeats for a long time.

②. Eureka can still accept registration and query requests for new services, but it will not be synchronized to other nodes (that is, ensuring that the current node is still available)

③. When the network is stable, the new registration information of the current instance will be synchronized to other nodes.

Therefore, Eureka can well cope with the situation where some nodes lose contact due to network failure, and will not paralyze the entire microservice like Zookeeper

##7, What is the difference between SpringBoot and SpringCloud?

SpringBoot focuses on developing individual individual microservices quickly and easily.

SpringCloud is a microservice coordination and governance framework that focuses on the overall situation. It integrates and manages individual microservices developed by SpringBoot, and

provides configuration management for each microservice. , service discovery, circuit breaker, routing, micro agent, event bus, global lock, decision election, distributed session and other integrated services

SpringBoot can be used independently without SpringCloud for development projects, but SpringCloud cannot be separated from SpringBoot. It is a dependency relationship.

SpringBoot focuses on the rapid and convenient development of individual microservices, while SpringCloud focuses on the global service governance framework.

8. What is the meaning of load balancing?

In computing, 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 aims to optimize resource usage, maximize throughput, minimize response time and avoid overloading 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 multilayer switches or Domain Name System server processes.

9. 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, stop cascading failures and stop cascading failures when failures are unavoidable Achieving resiliency in complex distributed systems.

Usually for systems developed using microservice architecture, many microservices are involved. These microservices collaborate with each other.

Think about the following microservices

Interview feedback Spring Cloud's 25-shot series

Assume that if microservice 9 in the above picture fails, then use traditional methods We will propagate an exception. But this still causes the entire 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 comes in. We will use the Fallback method feature of Hystrix in this case. We have two services employee-consumer which uses services exposed by employee-consumer.

The simplified diagram is as follows

Interview feedback Spring Cloud's 25-shot series

Now assume that for some reason, the service exposed by employee-producer will throw Exception occurred. We have defined a fallback method using Hystrix in this case. This fallback method should have the same return type as the public service. If an exception occurs in the exposed service, the fallback method will return some value.

10. What is Hystrix circuit breaker? Do we need it?

For some reasons, the employee-consumer public 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.

Interview feedback Spring Cloud's 25-shot series


If the exception in firstPage method() continues to occur, the Hystrix circuit will break and the employee will use Or will skip the firtsPage method altogether 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 exception recovery. What may be happening is that under lighter load, the problem that caused the exception has a better chance of recovery.

Interview feedback Spring Cloud's 25-shot series


11. Talk about the implementation principle of RPC

First of all, there is a module that handles network connection communication, which is responsible for connection establishment, management and message transmission. Secondly, a coding and decoding module is needed, because network communications are all transmitted bytecodes, and the objects we use need to be serialized and deserialized. The rest is the client and server parts. The server exposes the service interface to be opened. The client calls a proxy implementation of the service interface. This proxy implementation is responsible for collecting data, encoding and transmitting it to the server and then waiting for the results to be returned.

12. What is the self-protection mechanism of eureka?

When the Eureka Server node loses the connections of too many instances in a short period of time ( For example, network failure or frequent startup and shutdown of the client) the node will enter the self-protection mode to protect the registration information and will no longer delete the registration data. When the failure recovers, it will automatically exit the self-protection mode.

13. What is Ribbon?

ribbon is a load balancing client that can well control some behaviors of http and tcp. feign integrates ribbon by default.

#14, 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 to http apis, regardless of their stability.

Features:

  • Feign uses interface-based annotations
  • Feign integrates ribbon and has load balancing capabilities
  • Integrated Hystrix, with circuit breaker capability

Usage method

  • Add pom dependency.
  • Start class addition@EnableFeignClients
  • Define an interface@FeignClient(name="xxx") Specify which service to call

#15. What is the difference between Ribbon and Feign?

1. Ribbon calls other services, but in different ways. 2. The startup class annotations are different. Ribbon is @RibbonClient and feign is @EnableFeignClients. 3. The specified location of the service is different. Ribbon is declared on the @RibbonClient annotation, while Feign is declared using @FeignClient in the interface that defines the abstract method. 4. The calling methods are different. Ribbon needs to construct the http request by itself and simulate the http request.

16. What are the core components of Spring Cloud?

  • Eureka: Service registered with Discovery.
  • Feign: Based on the dynamic proxy mechanism, based on the annotation and selected machine, the request url address is spliced ​​and the request is initiated.
  • Ribbon: Implement load balancing and select one from multiple machines for a service.
  • Hystrix: Provides a thread pool. Different services use different thread pools, which realizes the isolation of different service calls and avoids the problem of service avalanche.
  • Zuul: Gateway management, the Zuul gateway forwards requests to the corresponding services.

17. Talk about the relationship between Spring Boot and Spring Cloud

Spring Boot is launched by Spring to solve the traditional A Maven-based solution with redundant framework configuration files and complex assembly components, designed to quickly build a single microservice Spring Cloud focuses on solving the coordination and configuration between various microservices, communication between services, circuit breaker, load balancing, etc. The technical dimensions are not the same, and Spring Cloud depends on Spring Boot, but Spring Boot does not depend on Spring Cloud, and can even perform excellent integrated development with Dubbo

Summary

  • SpringBoot focuses on the quick and convenient development of individual microservices
  • SpringCloud is a microservice coordination and governance framework that focuses on the overall situation, integrating and managing each microservice, and providing services for each microservice. Provides integration services such as configuration management, service discovery, circuit breakers, routing, event buses, etc.
  • Spring Boot does not depend on Spring Cloud. Spring Cloud depends on Spring Boot and is a dependency. Relationship
  • Spring Boot focuses on quickly and conveniently developing individual microservices, while Spring Cloud focuses on the global service governance framework

##18. Talk about how microservices communicate independently?

Remote Procedure Invocation

is what we often call the registration and discovery of services. Access other services directly through remote procedure calls.

Advantages: Simple and common, because there is no middleware proxy, the system is simpler

Disadvantages: Only supports request/response mode, not Supporting others, such as notifications, request/asynchronous response, publish/subscribe, publish/asynchronous response, reduces usability because both the client and the server must be available during the request process.

Message

Use asynchronous messages for inter-service communication. Services communicate by exchanging messages through message pipes.

Advantages: Decouple the client and server, looser coupling, and improve availability, because the message middleware caches the message until the consumer can consume it, and supports many communication mechanisms such as notifications , request/asynchronous response, publish/subscribe, publish/asynchronous response.

Disadvantages: Message middleware has additional complexity.

19. How does Spring Cloud implement service registration?

When the service is released, specify the corresponding service name and register the service to the registration center (Eureka, Zookeeper).

Add @EnableEurekaServer to the registration center, use @EnableDiscoveryClient for the service, and then use ribbon or feign to directly call and discover the service.

This question is more practical. It depends on whether you have memorized the interview questions. People who have not practiced it will not know.

20. What is a service circuit breaker?

In a complex distributed system, the mutual calls between microservices may cause service blocking for various reasons. In high concurrency scenarios, the service Blocking means blocking of threads, causing the current thread to be unavailable. All threads of the server are blocked, causing the server to crash. Since the calling relationship between services is synchronous, it will cause a service avalanche for the entire microservice system

In order To solve the problem that the call response time of a certain microservice is too long or is unavailable, which takes up more and more system resources and causes an avalanche effect, it is necessary to carry out service circuit breaker and service degradation processing.

The so-called service circuit breaker refers to a certain service failure or abnormality, which is similar to the "fuse" in the display world. When an abnormal condition is triggered, the entire service is directly blown, instead of waiting until the service times out.

Service fusing is equivalent to the fuse of our electric switch. Once a service avalanche occurs, the entire service will be fusing. By maintaining a thread pool of its own, when the thread reaches the threshold, service degradation will be initiated. If other requests If you continue to access, you will directly return to the default value of fallback

21. Do you understand Eureka’s self-protection mechanism?

When the Eureka Server node loses connections to too many instances in a short period of time (such as network failure or frequent startup and shutdown of clients) the node will enter self-protection mode to protect registration information. Registration data will no longer be deleted, and the self-protection mode will automatically exit when the fault is restored.

22. Are you familiar with Spring Cloud Bus?

spring cloud bus connects distributed nodes with lightweight message brokers. Can be used to broadcast configuration file changes or direct service communication, and can also be used for monitoring. If the configuration file is modified and a request is sent, all clients will re-read the configuration file.

23. What is the function of Spring Cloud circuit breaker?

When a service calls another service and there is a problem due to network reasons or its own reasons, The caller will wait for the callee's response. When more service requests to these resources lead to more requests waiting, a chain effect (avalanche effect) occurs. If it cannot be called a certain number of times within a period of time and there is no sign of recovery after multiple monitoring, then the circuit breaker is fully opened and the service will not be requested next time.

Half-open: There are signs of recovery in a short period of time. The circuit breaker will send some requests to the service. When called normally, the circuit breaker is closed. Closed: When the service is always in a normal state and can be called normally.

24. Do you know Spring Cloud Config?

In a distributed system, due to the huge number of services, in order to facilitate the unification of service configuration files Management and real-time updates require distributed configuration center components. In Spring Cloud, there is a distributed configuration center component Spring Cloud Config, which supports the configuration service to be placed in the memory of the configuration service (that is, local), and also supports being placed in a remote Git repository.

In the Spring Cloud Config component, there are two roles, one is config server and the other is config client.

Usage:

  • Add pom dependency
  • Add related configuration to configuration file
  • Add annotation @EnableConfigServer to startup class

25. Talk about your understanding of Spring Cloud Gateway

Spring Cloud Gateway is the second generation officially launched by Spring Cloud Gateway framework, replacing Zuul gateway. As a traffic controller, gateways play a very important role in microservice systems. Common functions of gateways include routing and forwarding, permission verification, and current limiting control.

A RouteLocatorBuilder bean is used to create routes. In addition to creating routes, RouteLocatorBuilder allows you to add various predicates and filters. The meaning of predicates assertions, as the name suggests, is to be processed by specific routes according to specific request rules. , filters are various filters used to make various judgments and modifications to requests.

Reference; http://1pgqu.cn/M0NZo

##Summary

Spring Cloud It is quite popular at the moment and is almost one of the essential skills for Java developers. It is normal to be asked this question during the interview. Many people may have used it for a long time but failed the interview without understanding the principles. Memorizing test questions is still very useful on a large level. But from a long-term perspective, I hope everyone will learn and practice at a deeper level. Only when you really master it can you call it NB.

The above is the detailed content of Interview feedback Spring Cloud's 25-shot series. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Java后端技术全栈. If there is any infringement, please contact admin@php.cn delete