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

Example Analysis of Hystrix attribute configuration and fallback in Spring Cloud

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article shares with you the content of a sample analysis of Hystrix property configuration and fallback in Spring Cloud. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Hystrix property configuration and fallback property configuration

When using Hystrix, you can set properties for a command, and the following code snippet sets the timeout for execution of a command:

Public MyCommand (boolean isTimeout) {super (Setter.withGroupKey (HystrixCommandGroupKey.Factory.asKey ("ExampleGroup")) .andCommandPropertiesDefaults (HystrixCommandProperties.Setter () .withexecution TimeoutInMilliseconds (500));}

The above configuration only takes effect on this command. The timeout of the command is set to 500 milliseconds, and the default value of this configuration item is 1 second. If you want to take effect globally, you can use the following code snippet:

ConfigurationManager .getConfigInstance () .setProperty ("hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds", 500)

The above code snippet also sets the command timeout to 500 milliseconds, but it works globally. In addition to the timeout configuration, you also need to know the relevant names of the command, and you can set the following names for the command:

Command group name (GroupKey): you must provide a command group name. By default, the globally maintained thread pool Map takes this value as the key, and the value of this Map is the thread pool where the command is executed.

Command name (CommandKey): optional parameter.

Thread pool name (ThreadPoolKey): when the thread's key is specified, the globally maintained thread pool Map will use this value as the key.

The following code snippet sets the above three Key respectively:

Public RunCommand (String msg) {super (Setter.withGroupKey (HystrixCommandGroupKey.Factory.asKey ("group-key")) .andCommandKey (HystrixCommandKey.Factory.asKey ("command-key")) .andThreadPoolKey (HystrixThreadPoolKey.Factory.asKey ("pool-key"));}

There are many configurations of Hystrix. The cases in later chapters will cover some of the configurations. If you want to know more about the configuration, you can check it at the following address: https://github.com/Netflix/Hystrix/wiki/Configuration

Fallback

According to the flowchart in the previous section, there are at least three situations that trigger a fallback:

The circuit breaker is turned on.

Thread pools, queues, semaphores are fully loaded.

The actual execution of the command failed.

In the command, you can implement the fallback by implementing the getFallback () method of the parent class (HystrixCommand). When this happens, the fallback method will be executed. In the previous example, the fallback of "failed to execute the command" has been shown. Let's test the fallback when the circuit breaker is turned on, as detailed in listing 6-7.

Listing 6-7:

06\ 6.2\ first-hnystrix-client\ src\ main\ java\ org\ crazyit\ cloud\ fallback\ FallbackTest.java

Public class FallbackTest {public static void main (String [] args) {/ / Circuit Breaker is forced to open ConfigurationManager.getConfigInstance () .setProperty ("hystrix.command.default.circuitBreaker.forceOpen", "true"); FallbackCommand c = new FallbackCommand (); c.execute () / / create a second command to shut down ConfigurationManager.getConfigInstance (). SetProperty ("hystrix.command.default.circuitBreaker.forceOpen", "false"); FallbackCommand c2 = new FallbackCommand (); c2.execute ();} static class FallbackCommand extends HystrixCommand {public FallbackCommand () {super (HystrixCommandGroupKey.Factory.asKey ("ExampleGroup")) } / * Circuit Breaker is forced to open, this method will not execute * / protected String run () throws Exception {System.out.println ("Command execution"); return "" } / * fallback method. Fallback will be performed after the circuit breaker is opened * / protected String getFallback () {System.out.println ("execute fallback method"); return "fallback";}

If you let the circuit breaker open, you need to meet certain conditions. In this example, for the sake of simplicity, in the code listing, the configuration management class (ConfigurationManager) is used to force the circuit breaker on and off. After opening the circuit breaker, FallbackCommand always executes the getFallback method to close the circuit breaker, and the command executes normally. If the circuit breaker is turned on and no fallback method is provided in the command, the following exception is thrown:

Com.netflix.hystrix.exception.HystrixRuntimeException: FallbackCommand short-circuited and no fallback available.

In addition, it should be noted that after the command is executed, no matter whether the fallback will be triggered or not, it will calculate the health of the entire link and judge whether to open the circuit breaker according to the health status. If the command fails only once, it is not enough to open the circuit breaker. The logic about the circuit breaker will be described in later chapters.

Fallback mode

The fallback mechanism of Hystrix is relatively flexible. You can execute the B command in the fallback method of the A command. If the B command also fails, it will also trigger the fallback of the B command, thus forming a chain of command execution, such as the following code snippet:

Static class CommandA extends HystrixCommand {... Omit the other code protected String run () throws Exception {throw new RuntimeException ();} protected String getFallback () {return new CommandB () .execute ();}}

There are other more complex examples, such as bank transfer. Suppose a transfer order contains two orders: bank A deduction and Bank B increase. After one of the orders fails, the fallback of the transfer order is executed, as shown in figure 6-4.

Figure 6-4 Multi-command fallback

To achieve the effect of multiple commands in figure 6-4 executing only one fallback, CommandA and CommandB cannot have fallback methods. If the CommandA command fails and the command has a fallback method, the "MainCommand" fallback method will not be executed at this time. In addition to the chained fallback and multi-command fallback mentioned above, the reader can also design the fallback according to the actual situation.

Thank you for reading! This is the end of this article on "sample analysis of Hystrix attribute configuration and fallback in Spring Cloud". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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