In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to analyze Spring Cloud Ribbon, Spring Cloud Feign and circuit breaker, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
1.spring-cloud is based on spring-boot. The difference between spring-boot and traditional spring projects is that the "built framework" is lighter and faster to get started.
The difference between 2.spring-cloud and traditional projects is that the former is distributed and the latter is a single architecture, that is, each sub-project opens services independently and some services support multiple other service calls [equivalent to simplified duplicate code]. Of course, two important services are needed in this distribution.
2.1. Gateway service (equivalent to 'distributing' to different routes) 2.2 Authentication service (limited private backend requests require permissions, which may be login permissions or role permissions)
3. Service registration and discovery: eruke is commonly used in registries, followed by service providers and service consumers
Eruke [high availability service registry]: 1. There can be multiple registries 2. Multiple registries can register with each other. One of the registries is down, and the others can provide services.
4. Service consumers: generally use Ribbon and Feign
1. Spring Cloud Ribbon
1.1 Spring Cloud Ribbon is a set of client load balancing tools based on Netflix Ribbon. It is a client load balancer based on HTTP and TCP. It can balance the load by configuring ribbonServerList in the client to set the server list to poll access.
1.2 retry mechanism
1.3 Hunger loading (eager-load) mode
2. Spring Cloud Feign
2.1. Ribbon is a load balancer based on HTTP and TCP clients. Ribbon is also used in Feign.
Spring Cloud Feign is a set of declarative service invocation clients based on Netflix Feign implementation. It makes it easier to write Web service clients. We just need to create the interface and configure it with annotations to bind to the Web service interface. It has pluggable annotation support, including Feign comments, JAX-RS comments. It also supports pluggable encoders and decoders. Spring Cloud Feign also extends support for Spring MVC annotations while integrating Ribbon and Eureka to provide load-balancing HTTP client implementations.
3. Service fault-tolerant protection (Hystrix service degradation, dependency isolation, circuit breaker)
Service degradation: add @ HystrixCommand annotation to the function that executes the logic to specify the service degradation method. This annotation has the identification of which service will be lost if the service fails.
Dependency isolation: if the operation of the new service is unstable or has problems after it is connected, it will not affect other requests of the application at all.
Circuit breaker: in the distributed architecture, the function of the circuit breaker mode is similar, when a service unit fails (similar to a short circuit in an electrical appliance), through the fault monitoring of the circuit breaker (similar to a fuse), directly cut off the original main logic call.
3.1How to get triggered exception in Hystrix downgrade logic
* * Hystrix two different * * ways to obtain triggered exceptions
The above implementation is the same as the annotated implementation in the previous section, and the exception information that triggers the degradation can be obtained through the getFailedExecutionException method when using inheritance.
3.2 do not use Hystrix
3.3.Using Hystrix to solve the problem of internal calls throwing exceptions
In this class, this method is a callback method that causes an exception, so we can see that if an exception is thrown, if the exception is directly handled by HystrixBadRequestException, it will be thrown after the exception is directly handled (the circuit breaker mechanism will not be triggered here), instead of entering the callback method.
3.4 request for a merge
Usually, the dependency in micro-service architecture is realized by remote invocation, and the most common problems in remote invocation are communication consumption and connection occupation. In the case of high concurrency, the total communication time consumption will become less ideal due to the increase of communication times. At the same time, due to the limited resources of the service-dependent thread pool, there will be queuing and response delays. To optimize these two issues, Hystrix provides HystrixCollapser to merge requests to reduce communication consumption and thread usage.
Solution
* * method one inherits the rewriting method, * *
In the constructor above, we set the time delay property for the request combiner, which collects requests for a single User within this time window and assembles them into a single batch request at the end of the time window. The following getRequestArgument method returns the given single request parameter userId, and createCommand and mapResponseToRequests are the two cores of the request combiner:
CreateCommand: all requests for a single User collected in the delay window are saved in the collapsedRequests parameter of this method. Organize the batch request commands we prepared above by getting the parameters of these requests
UserBatchCommand instance.
MapResponseToRequests: after the batch command UserBatchCommand instance is triggered for execution, the method starts execution, where the batchResponse parameter saves the return result of the batch request command organized in createCommand, and the collapsedRequests parameter represents each merged request. Here we complete the conversion of the batch result to a single request result by traversing the batch result batchResponse object and setting the return result for each single request before merging in collapsedRequests.
* * method 2 [* * use annotations to implement request combiner],
Use @ HystrixProperty (name= "timerDelayInMilliseconds", value = "100s") to set the merge time window to 100ms.
Note: the delay of the request command itself. If the service-dependent request command itself is a high-latency command, you can use the request combiner.
After reading the above, have you mastered how to analyze Spring Cloud Ribbon, Spring Cloud Feign and circuit breakers? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.