当前位置:网站首页>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: trueThe 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 .
边栏推荐
- (lightoj - 1410) consistent verbs (thinking)
- [CF Gym101196-I] Waif Until Dark 网络最大流
- Inspiration from the recruitment of bioinformatics analysts in the Department of laboratory medicine, Zhujiang Hospital, Southern Medical University
- MySQL view tablespace and create table statements
- Ble of Jerry [chapter]
- . Net 6 learning notes: what is NET Core
- CF1036C Classy Numbers 题解
- Key value judgment in the cycle of TS type gymnastics, as keyword use
- If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]
- How to estimate the number of threads
猜你喜欢
![DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist](/img/17/415e66d67afb055e94a966de25c2bc.png)
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist
![Ble of Jerry [chapter]](/img/00/27486ad68bf491997d10e387c32dd4.png)
Ble of Jerry [chapter]

Solution: intelligent site intelligent inspection scheme video monitoring system

珠海金山面试复盘
![datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在](/img/17/415e66d67afb055e94a966de25c2bc.png)
datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在

Risk planning and identification of Oracle project management system

Solution: système de surveillance vidéo intelligent de patrouille sur le chantier

MEX有关的学习

Fundamentals of C language 9: Functions

How to prevent Association in cross-border e-commerce multi account operations?
随机推荐
【Redis】NoSQL数据库和redis简介
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
智能终端设备加密防护的意义和措施
数据治理:微服务架构下的数据治理
07- [istio] istio destinationrule (purpose rule)
Opencv learning notes 8 -- answer sheet recognition
Description of octomap averagenodecolor function
Solution: système de surveillance vidéo intelligent de patrouille sur le chantier
js對象獲取屬性的方法(.和[]方式)
After the hot update of uniapp, "mismatched versions may cause application exceptions" causes and Solutions
Full Score composition generator: living on code
Wonderful use of TS type gymnastics string
二叉树创建 & 遍历
Summary of Digital IC design written examination questions (I)
Transformer principle and code elaboration
Launch APS system to break the problem of decoupling material procurement plan from production practice
Linked list interview questions (Graphic explanation)
Fundamentals of C language 9: Functions
Cf1036c class numbers solution
Common functions for PHP to process strings