The most popular use of springbootreisilien

2022-07-31
  • Detail

Use spring boot + resilience 4J to realize the following functions of circuit breaker

resilience 4J

circuit breaker

ratelimiter

bulkhead

retry

cache

timelimiter

if you plan to use it in spring boot, you can use starter. Please note that the new graphene reinforced prepreg of Spring B is under evaluation OOT 1 X and 2 Artifactid seems to be different between X series. In addition, the above only includes circuitbreaker and ratelimiter. When using other functions, you need to add dependencies separately. (since autoconfigure is not ready, you also need to define beans yourself.)

this time, I will summarize how to use spring boot 2 Circuitbreaker and ratelimiter are used in the X series

environment

jdk 8

spring boot lease

resilience 4J 0.13.2

circuit breaker

when some services with micro services fail, it can temporarily block access to the failed services and prevent failure propagation

circuitbreaker has three states: closed, open and halfopen. If it is normal, it is closed. If the processing fails more than a certain number, it will become open and access is blocked. After a period of time in the open state, it enters the halfopen state. If the processing fails for more than a certain amount in the halfopen state, it will return to the closed state

in resilience 4J, the success and failure of processing are managed by the ring bit buffer, and the state transitions when the number of faults in the buffer exceeds the set rate

under the condition of small deformation, the ring bit buffer in the circuit breaker use state is closed to store the success or failure state of calls that play the supporting and leading role of key new material enterprises. The successful call store is 0 bits and the failed call store is 1 bits. The ring bit buffer has a (configurable) fixed size. The ring buffer internally uses a BitSet similar to the data structure to store memory saving bits compared with Boolean arrays. BitSet uses the long [] array to store these bits. This means that BitSet only needs an array of 16 long (64 bit) values to store the state of 1024 calls

for example, if the size of the ring buffer is 10, at least 10 calls must be evaluated before the failure rate can be calculated. If only 9 calls are evaluated, the circuit breaker will not open even if all 9 calls fail

for closed ->; Open and halfopen ->; The closed ring buffer is different. The size can be defined, but the same judgment condition (error rate) is used

after the continuous closing time, the circuit breaker status changes from open to half_ Open and allow calls to see if the back end is still unavailable or is available again

the circuit breaker uses another (configurable) ring buffer to evaluate half_ The failure rate in the open state. If the failure rate is higher than the configured threshold, the status will change back to open. If the failure rate is below or equal to the threshold, the status changes back to closed

in addition, the success and failure of processing are judged by exceptions. By default, if any exception throws an exception, it will be treated as a processing failure, but you can also specify a condition to treat it as a failure

setting

l you can set and define multiple circuit breakers

if you want to use recordexceptions only considering a specific exception and fault, use ignoreexceptions when you don't want to ignore a specific exception

there are two ways to use spring AOP and implement it in functions. Either way, if the circuit is in the open state, it will generate a circuitbreakeropenexception

in the following implementation example, it is not a microservice for simplicity. At first, I thought this would be the process of service class calling other service APIs

spring AOP implementation

the circuit breaker can be enabled by annotating @circuitbreaker (name = "hogehoge") on a class or method. If you specify this annotation in a class, the circuit breaker is enabled for all public methods

how to write a business function

the caller modifies the method to be called with the modify~ method of the circuit breaker

backup processing

next, what should I do if a failure occurs and the rollback process is executed? In the case of hystrix, by specifying @hystrixcommand ("hogemethod"), because resilience4j does not have such a function set, it must be implemented by itself

ratelimiter

you can limit the number of executions per unit time

unit time is a cycle, and the number of executions that can be performed in a cycle is limited. If it exceeds the upper limit that can be executed in a loop, let it wait. If the waiting time exceeds the timeout, requestnotpermitted occurs

multiple rateelimiters can be defined in L

it is implemented in the same way as the circuit breaker. There are two ways to use spring AOP and write it in business functions. The implementation method is also similar to the circuit breaker

the caller doesn't have to think about anything, just execute the method

function method:

backup processing. Like the circuit breaker, there is no mechanism to automatically execute fallback processing, so you need to implement it yourself

the unit time is 5 seconds, the timeout is 1 second, and the execution times per unit time is 1. If multiple requests are sent at the same time, a failed request is issued. (if you request three at the same time, at least one will always fail.)

write at the end:

welcome to leave a message for discussion. For example, I happen to have a copy here. How can I get →→→ follow + forward and then I can get

points of attention by sending a private message to the "architecture data". Don't lose your way and keep updating

Copyright © 2011 JIN SHI