当前位置:网站首页>Circuit breaker: use of hystrix
Circuit breaker: use of hystrix
2022-07-06 07:49:00 【Lean on the building and listen to the wind】
Introduce dependencies
<dependency> <!-- introduce eureka client -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency> <!-- hystrix -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency> <!-- openfeign -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Open fuse
Annotate the startup class
@EnableCircuitBreaker // Open the circuit breaker
@EnableFeignClients // Turn on Feign
Add the configuration
Turn on Fegin Of hystrix
OpenFeign Is bringing Hystrix Of , But it's not on by default , stay application.properties Add the following configuration to enable Hystrix
# Turn on feign Below hystrix function
feign.hystrix.enabled: true
The source code of the corresponding open configuration is FeignClientsConfiguration.java in
// org.springframework.cloud.openfeign.FeignClientsConfiguration.HystrixFeignConfiguration
@Configuration
@ConditionalOnClass({ HystrixCommand.class, HystrixFeign.class })
protected static class HystrixFeignConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "feign.hystrix.enabled")
public Feign.Builder feignHystrixBuilder() {
return HystrixFeign.builder();
}
}
To configure Hystrix Parameters
# Enable global timeout
hystrix.command.default.execution.timeout.enabled=true
# Enable access cache function
hystrix.command.default.requestCache.enabled=true
# Isolation strategy , Default thread isolation
hystrix.command.default.execution.isolation.strategy=THREAD
########################### Thread isolation ##############################
# Timeout of thread isolation
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
# Abort thread after timeout
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=true
# Terminate the thread when canceling
hystrix.command.default.execution.isolation.thread.interruptOnFutureCancel=true
####################################################################
########################### Information isolation ( Thread isolation cannot exist at the same time ) ###
# Maximum concurrent requests
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=100
####################################################################
# Turn on the function of fusing
hystrix.command.default.circuitBreaker.enabled=true
# In a certain time window , Ask for instructions 5 After more than one , Before starting to make circuit breaker judgment
hystrix.command.default.circuitBreaker.requestVolumeThreshold=5
# When the fuse is turned on , How many seconds will it take to enter the half open state
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=15000
# exceed 50% Failed requests for , Then the fuse switch is turned on
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50
# Forcibly open the fuse switch
hystrix.command.default.circuitBreaker.forceOpen=false
hystrix.command.default.circuitBreaker.forceClosed=false
# Set up a rolling window Divided time
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=20000
# Set up a rolling window The number divided
hystrix.command.default.metrics.rollingStats.numBuckets=10
These settings are global by default , If you want to set a single method, set default Switch to HystrixCommand.commandKey() , If the annotation does not specify commandKey, The default is Class name # Method name ( Parameter type ) for example :
MyService#retry(Integer)
Circuit breaker call
Three states of the circuit breaker :
- The closed position
The circuit breaker handles the closed state , Count the number of call failures , After reaching a certain threshold within a period of time, the circuit breaker opens .
- open
The method call directly returns a failure error , No real method calls occur , Set a reset time , After the reset time , The circuit breaker is half open .
- Half open state
Method calls are allowed at this time , When the call succeeds ( Or achieve a certain proportion of success ), Close the circuit breaker , Otherwise, the service is not restored , Reopen the circuit breaker .
To write Feign Call interface FeignService.java
@FeignClient(name = "feign-client", fallback = Fallback.class)
public interface FeignService {
@GetMapping("/fallback")
String fallback();
@GetMapping("/continueFallback")
String continueFallback();
}
Write degraded classes Fallback.java
@Slf4j
@Component
public class Fallback implements FeignService {
@Override
public String fallback() {
log.info(" Downgrade succeeded ");
return " Downgrade succeeded ";
}
SecureRandom random = new SecureRandom();
@Override
// Continuous degradation
@HystrixCommand(fallbackMethod = "fallback2")
public String continueFallback() {
if (random.nextBoolean()) {
return " The result returned by the first demotion :success-1";
}
String s = " First demotion ";
log.error(s);
throw new RuntimeException("first fallback");
}
@HystrixCommand(fallbackMethod = "fallback3")
public String fallback2() {
if (random.nextBoolean()) {
return " The result returned by the second degradation :success-2";
}
log.error(" Second demotion ");
throw new RuntimeException("fallback again");
}
public String fallback3() {
if (random.nextBoolean()) {
log.error(" Third demotion ");
return " The result returned by the third degradation :success-3";
}
log.error(" Third demotion Failure ");
throw new RuntimeException(" Third demotion Failure ");
}
}
Provide external access interface Controller.java
@Slf4j
@RestController
public class Controller {
@Autowired
private FeignService feignService;
@GetMapping("/fallback")
public String randomError() {
return feignService.fallback();
}
@GetMapping("/continueFallback")
public String continueFallback() {
return feignService.continueFallback();
}
}
Request merge
Hystrix It also provides the function of requesting merging . Multiple requests are combined into one request for one processing , It can effectively reduce network communication and thread pool resources .
It provides two ways to merge requests :request-scoped Collect one HystrixRequestContext The requests in are aggregated into a batch ; and globally-scoped Will be multiple HystrixRequestContext The requests in are aggregated into a batch , This requires that the downstream dependency of the application can support processing multiple in one command call HystrixRequestContext.
1. Request merging through annotation
A single request requires @HystrixCollapser To modify , And specify batchMethod, You can set some parameters of the merge request , Here we set the period of requesting consolidation to 100 second . Because waiting for results cannot be synchronized during request consolidation , So the result returned by a single request is Future, That is, you need to wait for the result asynchronously .
batchMethod Method parameters can only be List, It can't be Set, because Set Is chaotic , When merging requests, it cannot correspond .
@HystrixCollapser(batchMethod = "getInstanceByIds",
// Set the period for requesting consolidation to 100 second
collapserProperties = { @HystrixProperty(name = "timerDelayInMilliseconds", value = "10000") })
public Future<String> getInstanceById(String id) {
return null;
}
@HystrixCommand
public List<String> getInstanceByIds(List<String> ids) {
log.info("start batch");
List<String> list = new ArrayList<>();
for (String id : ids) {
list.add(id);
}
return list;
}
call
@GetMapping("/batch")
public List<String> batch() throws InterruptedException, ExecutionException {
// You need to initialize the request before the request starts HystrixRequestContext, Used to share data between threads in the same request , Encapsulation means request , You need to close before the request ends
HystrixRequestContext context = HystrixRequestContext.initializeContext();
Future<String> fc = collapserService.getInstanceById("c");
Future<String> fa = collapserService.getInstanceById("a");
Future<String> fd = collapserService.getInstanceById("d");
TimeUnit.MILLISECONDS.sleep(1000);
Future<String> fb = collapserService.getInstanceById("b");
List<String> list = Arrays.asList(fa.get(), fb.get(), fc.get(), fd.get());
context.close();
return list;
}
If it is called directly within the request merge cycle Future#get Method blocks waiting for synchronization results , Will force merge requests .
2. Inherit HystrixCollapser
The request merge command can also be implemented in a customized way , Just inherit HystrixCollapser abstract class .
public class MyHystrixCollapserCommand extends HystrixCollapser<List<String>, String, Integer> {
Integer requestId;
public MyHystrixCollapserCommand(Integer requestId) {
// You need to specify the CollapserKey, Used to mark the key value of the merged request
super(Setter.withCollapserKey(HystrixCollapserKey.Factory.asKey("myHystrixCollapserCommand")));
this.requestId = requestId;
}
@Override
public Integer getRequestArgument() {
return this.requestId;
}
@Override
protected com.netflix.hystrix.HystrixCommand<List<String>> createCommand(Collection<CollapsedRequest<String, Integer>> collapsedRequests) {
// Merge request parameters
List<Integer> ids = collapsedRequests.stream().map(CollapsedRequest::getArgument).collect(Collectors.toList());
HystrixCommandGroupKey batchGroup = HystrixCommandGroupKey.Factory.asKey("requestIdBatchGroup");
return new HystrixCommand<List<String>>(batchGroup) {
@Override
protected List<String> run() throws Exception {
// Initiate remote batch request , This is not the point , Ignore
List<String> list = ids.stream().map(String::valueOf).collect(Collectors.toList());
return list;
}
};
}
@Override
protected void mapResponseToRequests(List<String> batchResponse,
Collection<CollapsedRequest<String, Integer>> collapsedRequests) {
int count = 0;
for (CollapsedRequest<String, Integer> request : collapsedRequests) {
// Split the response result of the corresponding request
request.setResponse(batchResponse.get(count++));
}
}
}
stay Controller Use in
@GetMapping("/myBatch")
public List<String> myBatch() throws InterruptedException, ExecutionException {
// You need to initialize the request before the request starts HystrixRequestContext, Used to share data between threads in the same request , Encapsulation means request , You need to close before the request ends
HystrixRequestContext context = HystrixRequestContext.initializeContext();
MyHystrixCollapserCommand c1 = new MyHystrixCollapserCommand(1);
MyHystrixCollapserCommand c2 = new MyHystrixCollapserCommand(2);
MyHystrixCollapserCommand c3 = new MyHystrixCollapserCommand(3);
MyHystrixCollapserCommand c4 = new MyHystrixCollapserCommand(4);
Future<String> f1 = c1.queue();
Future<String> f2 = c2.queue();
Future<String> f3 = c3.queue();
TimeUnit.MILLISECONDS.sleep(1000);
Future<String> f4 = c4.queue();
List<String> list = Arrays.asList(f1.get(), f2.get(), f3.get(), f4.get());
context.close();
return list;
}
Effect and @HystrixCollapser equally .
边栏推荐
- [MySQL learning notes 29] trigger
- opencv学习笔记八--答题卡识别
- Opencv learning notes 8 -- answer sheet recognition
- ROS learning (IX): referencing custom message types in header files
- Google可能在春节后回归中国市场。
- Do you really think binary search is easy
- Brief explanation of instagram operation tips in 2022
- 数据治理:误区梳理篇
- Ble of Jerry [chapter]
- Description of octomap averagenodecolor function
猜你喜欢
[CF Gym101196-I] Waif Until Dark 网络最大流
[MySQL learning notes 30] lock (non tutorial)
Inspiration from the recruitment of bioinformatics analysts in the Department of laboratory medicine, Zhujiang Hospital, Southern Medical University
Redis builds clusters
leecode-C語言實現-15. 三數之和------思路待改進版
Relevant introduction of clip image
Document 2 Feb 12 16:54
数字经济时代,如何保障安全?
Simulation of Michelson interferometer based on MATLAB
Force buckle day31
随机推荐
Ble of Jerry [chapter]
Wonderful use of TS type gymnastics string
Cf1036c class numbers solution
珠海金山面试复盘
P3047 [usaco12feb]nearby cows g (tree DP)
http缓存,强制缓存,协商缓存
Nc204382 medium sequence
esRally国内安装使用避坑指南-全网最新
合规、高效,加快药企数字化转型,全新打造药企文档资源中心
Summary of Digital IC design written examination questions (I)
Scala language learning-08-abstract classes
Is the super browser a fingerprint browser? How to choose a good super browser?
数字经济时代,如何保障安全?
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
How to prevent Association in cross-border e-commerce multi account operations?
Description of octomap averagenodecolor function
实现精细化生产, MES、APS、ERP必不可少
If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]
Opencv learning notes 9 -- background modeling + optical flow estimation
opencv学习笔记八--答题卡识别