当前位置:网站首页>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 .
边栏推荐
- Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
- Key value judgment in the cycle of TS type gymnastics, as keyword use
- JMeter performance test steps practical tutorial
- [KMP] template
- [nonlinear control theory]9_ A series of lectures on nonlinear control theory
- Comparison of usage scenarios and implementations of extensions, equal, and like in TS type Gymnastics
- [MySQL learning notes 30] lock (non tutorial)
- Wonderful use of TS type gymnastics string
- Jerry's general penetration test - do data transmission with app Communication [article]
- Data governance: 3 characteristics, 4 transcendence and 3 28 principles of master data
猜你喜欢
21. Delete data
Simulation of Michelson interferometer based on MATLAB
Pangolin Library: control panel, control components, shortcut key settings
Mise en œuvre du langage leecode - C - 15. Somme des trois chiffres - - - - - idées à améliorer
Games101 Lesson 7 shading 1 Notes
If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]
数据治理:主数据的3特征、4超越和3二八原则
Fundamentals of C language 9: Functions
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
[redis] Introduction to NoSQL database and redis
随机推荐
Luogu p1836 number page solution
Sharing of source code anti disclosure scheme under burning scenario
How to delete all the words before or after a symbol in word
[count] [combined number] value series
数字经济时代,如何保障安全?
解决方案:智慧工地智能巡檢方案視頻監控系統
Summary of Digital IC design written examination questions (I)
[非线性控制理论]9_非线性控制理论串讲
shu mei pai
CF1036C Classy Numbers 题解
Cf1036c class numbers solution
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
合规、高效,加快药企数字化转型,全新打造药企文档资源中心
Vit (vision transformer) principle and code elaboration
Force buckle day31
Methods for JS object to obtain attributes (. And [] methods)
Typescript interface and the use of generics
数据治理:微服务架构下的数据治理
Simulation of Michelson interferometer based on MATLAB