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:
The following is a summary of the core components of
Spring Cloud Netflixand # Without further ado, let’s start the Spring Cloud series directly. Synchronous communication: dobbo uses RPC remote procedure call , springcloud calls via REST interface json, etc. Asynchronous: message queue, such as: The circuit breaker mechanism is a microservice link protection mechanism to deal with theavalanche 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. 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 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 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. 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. 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. 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. 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. ribbon is a load balancing client that can well control some behaviors of http and tcp. 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: Usage method 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. 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 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. 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. When the service is released, specify the corresponding service name and register the service to the registration center ( Add This question is more practical. It depends on whether you have memorized the interview questions. People who have not practiced it will not know. 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 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. 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. 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. 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 In the Usage: 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/M0NZoSpring Cloud Alibaba
: serial gun walking
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.
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. In layman’s terms: 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. 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.
When using Spring Boot to develop distributed microservices, we face the following problems
4. How do microservices communicate independently?
RabbitMq
,ActiveM
,Kafka
and other message queues.5.What is a service circuit breaker? What is service downgrade?
Hystrix
Related 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), thefallback
function will be executed and an error message will be returned.6. Please tell me the difference between Eureka and zookeeper?
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.
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.
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
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.
10. What is Hystrix circuit breaker? Do we need it?
11. Talk about the implementation principle of RPC
12. What is the self-protection mechanism of eureka?
13. What is Ribbon?
feign integrates ribbon
by default.#14, What is Netflix Feign? What are its advantages?
@EnableFeignClients
@FeignClient(name="xxx")
Specify which service to call
#15. What is the difference between Ribbon and Feign?
16. What are the core components of Spring Cloud?
17. Talk about the relationship between Spring Boot and Spring Cloud
is what we often call the registration and discovery of services. Access other services directly through remote procedure calls.
Message
19. How does Spring Cloud implement service registration?
Eureka, Zookeeper)
.@EnableEurekaServer
to the registration center, use@EnableDiscoveryClient
for the service, and then use ribbon or feign to directly call and discover the service.20. What is a service circuit breaker?
21. Do you understand Eureka’s self-protection mechanism?
22. Are you familiar with Spring Cloud Bus?
23. What is the function of Spring Cloud circuit breaker?
24. Do you know Spring Cloud Config?
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.Spring Cloud Config
component, there are two roles, one is config server and the other is config client.
25. Talk about your understanding of Spring Cloud Gateway
##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!