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

Summary of HytrixCommand practice

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

one。 Use HystrixCommand encoding

/ / construct setter

HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey (group)

HystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKey.Factory.asKey (group)

HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey (service)

HystrixCommandProperties.Setter commandPropertiesDefaults = HystrixCommandProperties.Setter ()

.withExecutionTimeoutInMilliseconds (100)

.withacquiitBreakerForceOpen (true)

HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults = HystrixThreadPoolProperties.Setter ()

.withCoreSize (10)

.withQueueSizeRejectionThreshold (10)

HytrixCommand.Setter setter = HytrixCommand.Setter.withGroupKey (groupKey)

.andCommandKey (commandKey)

.andThreadPoolKey (threadPoolKey)

.andCommandPropertiesDefaults (commandPropertiesDefaults)

.andThreadPoolPropertiesDefaults (threadPoolPropertiesDefaults)

/ / construct command

HystrixCommand command = new HystrixCommand (setter) {

Protected String run () throws Exception {

Logger.info ("# in hystrix thread")

Thread.sleep (time)

If (isException)

Throw new RuntimeException ("exception in run")

Return service+ ": return"

}

@ Override

Protected String getFallback () {

Logger.info ("# in request thread")

Return service+ ": fallback"

}

}

1 HystrixCommandKey

Hystrix uses singleton mode to store HystrixCommand, and the circuit breaker mechanism is implemented according to the call statistics on a single instance, so each HystrixCommand should have its own name for distinction and isolation of dependent calls. HystrixCommandKey is used to define this name, if the name is not defined, Hystrix will use its class name as its name, you can use the HystrixCommandKey.Factory.asKey (String name) method to define a name.

2 HystrixThreadPoolKey

HystrixThreadPoolKey is the thread pool where HystrixCommand resides. If this parameter is not set, HystrixCommandGroupKey is used as HystrixThreadPoolKey. In this case, dependent calls under the same HystrixCommandGroupKey share the same thread pool. If you do not want to share the same thread pool, you need to set this parameter. You can set it using the HystrixThreadPoolKey.Factory.asKey (String name) method.

3 HystrixCommandGroupKey

Hystrix needs to group HystrixCommand for easy statistics and management, so you need a grouping name. HystrixCommandGroupKey is used to define a grouping name. You can use the HystrixCommandGroupKey.Factory.asKey (String name) method to define a grouping name. Each HystrixCommand must be configured with a grouping name, one for grouping, and if HystrixThreadPoolKey is not configured, the grouping name will be used for the thread pool name.

4 HystrixThreadPoolProperties

From the name, you can see that this is the property configuration of the thread pool, through which you can set the core thread size, the maximum number of threads, the task queue size, and so on. Of course, it also has some default configuration parameters.

5 HystrixCommandProperties

This is the property configuration of HystrixCommand, which can set whether the fuse is available, the error percentage of fuse fuse, dependent call timeout, and so on. It has some default configuration parameters, such as the default value of 50% error percentage for fuse fuse and 1000 milliseconds for dependent call timeout.

two。 Use @ HystrixCommand mode

Here we only talk about the use of annotations and the more important parts. If you need to know all of them, check out: https://github.com/Netflix/Hystrix/wiki/How-To-Use

2.1 Hystrix command2.1.1 synchronous execution public class UserService {. @ HystrixCommand public User getUserById (String id) {return userResource.getUserById (id);} 2.1.2 Asynchronous execution public class UserService {

. @ HystrixCommand public Future getUserByIdAsync (final String id) {return new AsyncResult () {@ Override public User invoke () {return userResource.getUserById (id);}};}} 2.1.3 reaction execution () public class UserService {

... @ HystrixCommand public Observable getUserById (final String id) {return Observable.create (new Observable.OnSubscribe () {@ Override public void call (Subscriber)

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: 276

*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

Internet Technology

Wechat

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

12
Report