当前位置:网站首页>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 .
边栏推荐
- js對象獲取屬性的方法(.和[]方式)
- Data governance: misunderstanding sorting
- QT color is converted to string and uint
- 软件测试界的三无简历,企业拿什么来招聘你,石沉大海的简历
- jmeter性能测试步骤实战教程
- MySQL view tablespace and create table statements
- 解决方案:智慧工地智能巡檢方案視頻監控系統
- Three treasures of leeks and Chinese men's football team
- Pangolin Library: control panel, control components, shortcut key settings
- 2022年Instagram运营小技巧简单讲解
猜你喜欢

Related operations of Excel

Opencv learning notes 8 -- answer sheet recognition

861. Score after flipping the matrix

24. Query table data (basic)

Do you really think binary search is easy

Codeforces Global Round 19(A~D)
![[redis] Introduction to NoSQL database and redis](/img/95/229d7a08e94245f2733b8c59201cff.png)
[redis] Introduction to NoSQL database and redis
![If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]](/img/d6/92ad1c6f84415de6ab0dfd16cd6073.png)
If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]

In the era of digital economy, how to ensure security?

Document 2 Feb 12 16:54
随机推荐
数字经济时代,如何保障安全?
Parameter self-tuning of relay feedback PID controller
http缓存,强制缓存,协商缓存
MES, APS and ERP are essential to realize fine production
软件开发的一点随记
Relevant introduction of clip image
Generator Foundation
数据治理:微服务架构下的数据治理
C # create database connection object SQLite database
Apache middleware vulnerability recurrence
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
Transformer principle and code elaboration
Google可能在春节后回归中国市场。
datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在
CF1036C Classy Numbers 题解
Esrally domestic installation and use pit avoidance Guide - the latest in the whole network
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
861. Score after flipping the matrix
[computer skills]
esRally国内安装使用避坑指南-全网最新