Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to turn on and off Hystrix Circuit Breaker in spring cloud

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you how to turn on and off Hystrix circuit breakers in spring cloud. I hope you will get something after reading this article. Let's discuss it together.

18 opening and closing of circuit breaker

Once the circuit breaker is turned on, the fallback method is called directly, the command is no longer executed, and the health of the link is not updated. The opening of the circuit breaker must meet two conditions:

1. The whole link reaches a certain threshold. By default, if more than 20 requests are generated within 10 seconds, the first condition is met.

2. If the first condition is met, if the error percentage of the request is greater than the threshold, the circuit breaker will be opened, which is 50% by default.

Hystrix logic, first determine whether the first condition is satisfied, and then determine the second condition, if both conditions are met, the circuit breaker will be turned on. The test code for opening the circuit breaker is shown in listing 6-8.

Listing 6-8:

Codes\ 06\ 6.2\ first-hnystrix-client\ src\ main\ java\ org\ crazyit\ cloud\ breaker\ OpenTest.java

If public class OpenTest {public static void main (String [] args) throws Exception {/ / 10 seconds has 10 requests, the first condition ConfigurationManager.getConfigInstance () .setProperty ("hystrix.command.default.metrics.rollingStats.timeInMilliseconds", 10000); ConfigurationManager.getConfigInstance () .setProperty ("hystrix.command.default.circuitBreaker.requestVolumeThreshold", 10) ConfigurationManager.getConfigInstance () .setProperty ("hystrix.command.default.circuitBreaker.errorThresholdPercentage", 50); for (int I = 0; I < 15; iSum +) {/ / all commands MyCommand c = new MyCommand (); c.execute () / / output information if (c.isCircuitBreakerOpen ()) {System.out.println ("circuit breaker is opened, execute" + (I + 1) + "command") after the circuit breaker is turned on. } / * Command to simulate timeout * @ author Yang Enxiong * * / static class MyCommand extends HystrixCommand {/ / set timeout to 500ms public MyCommand () {super (Setter.withGroupKey (HystrixCommandGroupKey.Factory.asKey ("ExampleGroup")) .andCommandPropertiesDefaults (HystrixCommandProperties.Setter () .withExecutionTimeoutInMilliseconds (500)) } protected String run () throws Exception {/ / Simulation processing timeout Thread.sleep; return ";} @ Override protected String getFallback () {return";}

Notice the three configurations in listing 6-8, the first with the time of data statistics, the second with the requested threshold, and the third with the percentage of error. If more than 10 requests occur within 10 seconds, and the error rate of the request exceeds 50%, turn on the circuit breaker.

In the command class MyCommand, the timeout for command execution is set to 500ms, and it takes 800ms for command execution. In other words, the command always times out, and the command simulates the situation of service paralysis (timeout response) that depends on in the real environment.

In the running class, the command is executed 15 times, the isCircuitBreakerOpen method is called, and the information is output if the circuit breaker is open. Run the OpenTest class in listing 6-8 with the following output:

The circuit breaker is opened, the 11th command circuit breaker is opened, the 12th command circuit breaker is opened, the 13th command circuit breaker is opened, the 14th command circuit breaker is opened, and the 15th command is executed.

According to the results, the previous 10 commands did not turn on the circuit breaker, but to the 11th command, the circuit breaker was turned on and the command was no longer executed.

Circuit breaker off

After the circuit breaker is turned on, the command will not be executed for a period of time (always triggering a fallback), which we call the "dormant period". The default value of the dormancy period is 5 seconds. After the dormancy period is over, Hystrix will attempt to execute a command. At this time, the state of the circuit breaker is not on or off, but a half-open state. If the command is executed successfully, it will close the circuit breaker and clear the health information of the link. If the execution fails, the circuit breaker will continue to be open. For the open and close test of the circuit breaker, see listing 6-9.

Listing 6-9:

Codes\ 06\ 6.2\ first-hnystrix-client\ src\ main\ java\ org\ crazyit\ cloud\ breaker\ CloseTest.java

Public class CloseTest {public static void main (String [] args) throws Exception {/ / 10 seconds satisfies the condition for the first circuit breaker to be turned on ConfigurationManager.getConfigInstance () .setProperty ("hystrix.command.default.metrics.rollingStats.timeInMilliseconds", 10000); ConfigurationManager.getConfigInstance () .setProperty ("hystrix.command.default.circuitBreaker.requestVolumeThreshold", 3) / / the failure rate of the request. The default is 50% ConfigurationManager.getConfigInstance () .setProperty ("hystrix.command.default.circuitBreaker.errorThresholdPercentage", 50) / / sets the dormant period, during which no command will be executed after the circuit breaker is turned on. The default value is 5 seconds, and here it is set to 3 seconds ConfigurationManager.getConfigInstance () .setProperty ("hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds", 3000); / / this value determines whether to execute the timeout boolean isTimeout = true; for (int I = 0; I < 10). MyCommand c = new MyCommand (isTimeout); c.execute (); / / output health status and other information HealthCounts hc = c.getMetrics () .getHealthCounts () System.out.println ("circuit breaker status:" + c.isCircuitBreakerOpen () + ", total number of requests:" + hc.getTotalRequests ()); if (c.isCircuitBreakerOpen ()) {/ / circuit breaker is opened to allow the next loop to successfully execute the command isTimeout = false System.out.println ("= circuit breaker on, waiting for dormancy to end ="); / / dormancy will end in 3 seconds, wait here for 4 seconds to ensure that dormancy is over Thread.sleep (4000) }} / * simulated timeout command * @ author Yang Enxiong * * / static class MyCommand extends HystrixCommand {private boolean isTimeout / / set the timeout time to 500ms public MyCommand (boolean isTimeout) {super (Setter.withGroupKey (HystrixCommandGroupKey.Factory.asKey ("ExampleGroup")) .andCommandPropertiesDefaults (HystrixCommandProperties.Setter (). WithExecutionTimeoutInMilliseconds (500)); this.isTimeout = isTimeout } protected String run () throws Exception {/ / Let the outside decide whether to time out if (isTimeout) {/ / Analog processing timeout Thread.sleep;} else {Thread.sleep (200);} return "" } @ Override protected String getFallback () {return ";}

In the code listing, you configure a dormant period of 3 seconds, cycle 10 times, create 10 commands and execute them. After executing the fourth command, the circuit breaker is turned on, and we wait for the dormancy period to end so that the command of the next cycle can be executed successfully.

A Boolean value is used in the code listing to determine whether the execution is successful, the fifth command will be executed successfully, the circuit breaker will be turned off, and all the remaining commands can be executed normally. In the body of the loop, the HealthCounts object is used to record the health information of the link, and if the circuit breaker is closed (the link is healthy), the health information in the HealthCounts will be reset. Run listing 6-9 and the effect is as follows:

Circuit breaker status: false, total requests: 0 circuit breaker status: false, total requests: 1 circuit breaker status: false, total requests: 2 circuit breaker status: true, total number of requests: 3 = circuit breaker is open Waiting for dormancy period to end = circuit breaker status: false, total number of requests: 0 circuit breaker status: false, total number of requests: 1 circuit breaker status: false, total number of requests: 1 circuit breaker status: false, total number of requests: 3 circuit breaker status: false, total number of requests: 3 circuit breaker status: false, total number of requests: 5 finished reading this article I believe you have a certain understanding of "how to turn on and off Hystrix circuit breakers in spring cloud". If you 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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report