Preface
In the microservice architecture, Spring Cloud provides a series of tools and technologies to ensure the stability and reliability of the system. Among them, current limiting, fusion breaking and degrading are three important concepts, and they play a key role in different scenarios. This article will introduce in detail the differences between current limiting, fuse and downgrade in Spring Cloud, and explore their specific implementation methods.
1. The concept of current limiting, fuse and downgrade
(I) Current limit
Current limiting means limiting the number of requests the system can process over a period of time to prevent the system from crashing due to excessive requests. Current limiting can effectively protect the system's resources and ensure that the system can still operate normally under high load conditions.
For example, an e-commerce website may face a large number of user requests during a promotion. If the flow limit is not performed, the system may crash because it cannot handle so many requests, causing users to be unable to access the website. Through current limiting, the number of requests that can be processed per second can be limited, ensuring that the system is not overwhelmed by excessive requests.
(II) Fuse
Fuse means that when a system fails or abnormal conditions occur, the call to the faulty service will be automatically cut off to prevent the fault from spreading. When the system detects that a service has failed, the call to the service will be stopped immediately and a preset error response will be returned. This can avoid affecting the stability of the entire system due to a failure of one service.
For example, an order service in a microservice architecture depends on a payment service. If the payment service fails, the order service will wait while calling the payment service, resulting in the order service not working properly. Through the circuit breaker mechanism, the order service can immediately stop calling the payment service when it detects a payment service failure and return an error response of "payment service is temporarily unavailable", thus avoiding affecting the user's ordering experience.
(III) Downgrade
Degradation refers to reducing the system's functions or performance in the event of a system failure or high load to ensure that the core functions of the system can operate normally. Degradation can be either active or passive. Active downgrade means that when designing the system, it takes into account possible failures and formulates a downgrade strategy in advance. Passive downgrade refers to the temporary downgrade measures taken according to actual conditions when a system fails.
For example, an online education platform may experience video playback stuttering under high load conditions. In order to ensure that users can learn normally, downgrade measures can be taken to reduce the clarity of the video, or suspend some non-core functions, such as online discussions, to ensure the smoothness of video playback.
2. The difference between current limiting, fuse and downgrade
(I) Different purposes
- Current limit: The main purpose is to limit the number of requests of the system, prevent the system from crashing due to excessive requests, and protect the system's resources.
- Fuse: The main purpose is to automatically cut off the call to the faulty service when the system fails, prevent the fault from spreading, and ensure the stability of the system.
- Downgrade: The main purpose is to reduce the system's functions or performance in the event of a system failure or high load, and ensure that the core functions of the system can operate normally.
(II) Different trigger conditions
- Current limit: Usually triggered when the system load reaches a certain level, for example, the number of requests per second exceeds the system's processing capacity.
- Fuse: Usually triggered when the system detects a service failure, such as the service response time is too long, the service throws an exception, etc.
- Downgrade: It can be triggered when the system fails, high load, resource shortage, etc.
(III) Different treatment methods
- Current limit: Protect the system by limiting the number of requests, which may reject some requests, or put the requests in the queue and wait for processing.
- Fuse: Automatically cut off calls to the failed service and return a preset error response.
- Downgrade: Reduce the system's functions or performance, such as reducing the quality of services, suspending some non-core functions, etc.
3. Implementation of Spring Cloud Current Limit
(I) Guava RateLimiter
Guava RateLimiter is a current limiting tool provided by the Google Guava library. It is implemented based on the token bucket algorithm and can limit the number of requests that can be processed per second.
The steps to using Guava RateLimiter are as follows:
- Introduce dependencies for Google Guava library in your project.
<dependency> <groupId></groupId> <artifactId>guava</artifactId> <version>31.1-jre</version> </dependency>
- Create a RateLimiter object and set the number of requests that can be processed per second.
import ; public class RateLimiterExample { public static void main(String[] args) { // Create a RateLimiter object that can handle 10 requests per secondRateLimiter rateLimiter = (10); // Simulate request processingfor (int i = 0; i < 20; i++) { if (()) { // Process the request("Processing a request" + i); } else { // The request is restricted("ask " + i + "Price restricted");} } } }
(II) Spring Cloud Gateway Current Limit
Spring Cloud Gateway is an API gateway provided by Spring Cloud, which can implement requested routing, filtering, and current limiting functions.
The steps to use Spring Cloud Gateway to limit current are as follows:
- Introduce Spring Cloud Gateway dependencies in your project.
<dependency> <groupId></groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
- Configure the current limit rules in the configuration file.
spring: cloud: gateway: routes: - id: myroute uri: http://localhost:8080 predicates: - Path=/myapi/** filters: - name: RequestRateLimiter args: key-resolver: '#{@pathKeyResolver}' : 10 : 20
In the above configuration, we used Redis as the current-limiting storage medium and set the number of requests that can be processed per second to be 10 and the burst capacity is 20.
- Creates an implementation class for the KeyResolver interface to determine the keys for current limits.
import ; import ; import ; import ; import ; import ; @Configuration public class RateLimitConfig { @Bean public KeyResolver pathKeyResolver() { return exchange -> (().getPath().toString()); } }
In the above code, we create an implementation class of the KeyResolver interface to determine the current limiting key based on the requested path.
4. How to implement Spring Cloud Fuse
(I) Hystrix
Hystrix is a Netflix open source library for services such as circuit breaking, downgrading and isolation. It realizes the fuse function through circuit breaker mode. When a service fails, the circuit breaker will automatically turn on, preventing calls to the failed service until the service returns to normal.
The steps to using Hystrix are as follows:
Introduce Hystrix dependencies in the project.
<dependency> <groupId></groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
Add the @HystrixCommand annotation on the service method and specify the callback method after the circuit breaker.
import ; @Service public class MyService { @HystrixCommand(fallbackMethod = "fallbackMethod") public String myMethod() { // Call remote services or perform other operations that may failreturn "Normal Response"; } public String fallbackMethod() { // Callback method after circuit breakingreturn "Service is not available, please try again later"; } }
In the above code, we added the @HystrixCommand annotation to the myMethod method and specified the callback method after the circuit breakdown fallbackMethod. When the myMethod method fails to execute, the fallbackMethod method is automatically called to return the preset error response.
(II) Resilience4j
Resilience4j is a lightweight fault-tolerant library that provides functions such as fuse, retry, speed limit, etc. Its design philosophy is similar to Hystrix, but it is more concise and flexible.
The steps to use Resilience4j are as follows:
Introduce the Resilience4j dependency in the project.
<dependency> <groupId>.resilience4j</groupId> <artifactId>resilience4j-spring-boot2</artifactId> <version>1.7.1</version> </dependency>
Add the @CircuitBreaker annotation on the service method and specify the callback method after circuit breaking.
import .; @Service public class MyService { @CircuitBreaker(name = "myCircuitBreaker", fallbackMethod = "fallbackMethod") public String myMethod() { // Call remote services or perform other operations that may failreturn "Normal Response"; } public String fallbackMethod() { // Callback method after circuit breakingreturn "Service is not available, please try again later"; } }
In the above code, we added the @CircuitBreaker annotation to the myMethod method and specified the callback method after the circuit breakdown fallbackMethod. When the myMethod method fails to execute, the fallbackMethod method is automatically called to return the preset error response.
5. Implementation of Spring Cloud downgrade
(I) Hystrix downgrade
While using Hystrix to achieve fuse, you can also implement downgrades. In the callback method after circuit breaking, a preset downgrade response can be returned, or some downgrade logic can be performed, such as obtaining data from the cache, etc.
For example, in the above Hystrix example, the fallbackMethod method is a downgrade method, which returns a preset error response when a service fails.
(II) Spring Cloud custom downgrade logic
In addition to using the downgrade features provided by Hystrix, you can also customize the downgrade logic in Spring Cloud. You can create a custom downgrade method by implementing the FallbackFactory interface.
The steps to using custom downgrade logic are as follows:
Create a class that implements the FallbackFactory interface and implements the create method.
import ; import ; @Component public class MyServiceFallbackFactory implements FallbackFactory<MyService> { @Override public MyService create(Throwable cause) { return new MyService() { @Override public String myMethod() { // Custom downgrade logicreturn "Service is not available, please try again later"; } }; } }
Specify the downgrade factory class on the Feign client interface.
import ; import ; @FeignClient(name = "myService", fallbackFactory = ) public interface MyFeignClient { @GetMapping("/myapi") String myMethod(); }
In the above code, we created a class MyServiceFallbackFactory that implements the FallbackFactory interface, and implemented custom downgrade logic in the create method. Then, the downgrade factory class is specified on the Feign client interface. When a service fails, the create method in the downgrade factory class will be automatically called to create a downgrade object, and the myMethod method in the downgrade object will be executed to return a preset downgrade response.
6. Summary
Current limiting, fuse and downgrade are important means to ensure system stability and reliability in microservice architecture. They play different roles in different scenarios, but the purpose is to ensure that the system can operate normally under various circumstances.
In Spring Cloud, you can use Guava RateLimiter, Spring Cloud Gateway, etc. to implement current limiting functions; use Hystrix, Resilience4j, etc. to implement fuse functions; use the downgrade method provided by Hystrix or custom downgrade logic to implement downgrade functions. In practical applications, appropriate implementation methods can be selected according to specific needs, and combined with monitoring and alarm systems, problems in the system can be discovered and dealt with in a timely manner to ensure the stable operation of the system.
This is the article about the differences and implementation of SpringCloud current limit, circuit breaker, and downgrade. For more related SpringCloud current limit, circuit breaker, and downgrade, please search for my previous articles or continue to browse the related articles below. I hope everyone will support me in the future!