当前位置:网站首页>Hystrix implements request consolidation
Hystrix implements request consolidation
2022-07-02 09:22:00 【Protect our fat tiger】
Add
@EnableHystrix
@EnableHystrixDashboard
Add dependencies on services that need to be merged
<!--hystrix rely on -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
Request interface
package com.leilei.controller;
import com.leilei.entity.AlarmDictionary;
import com.leilei.service.AlarmService;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.Future;
/** * @author lei * @create 2022-03-30 22:45 * @desc **/
@RequestMapping("/alarm")
@RestController
public class AlarmController {
private final AlarmService alarmService;
public AlarmController(AlarmService alarmService) {
this.alarmService = alarmService;
}
/** * External single request interface * @param id * @return */
@GetMapping(value = "/dictionary/{id}")
public AlarmDictionary requestCollapseAnnotation(@PathVariable Integer id) {
// Note that it needs to be turned on Hystrix Environmental Science
try (HystrixRequestContext context = HystrixRequestContext.initializeContext()) {
Future<AlarmDictionary> f1 = alarmService.find(id);
return f1.get();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Aggregate batch processing inside the service
package com.leilei.service;
import com.leilei.entity.AlarmDictionary;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.Future;
/** * @author lei * @create 2022-03-30 22:38 * @desc **/
@Service
public class AlarmService {
private final AlarmFeign alarmFeign;
public AlarmService(AlarmFeign alarmFeign) {
this.alarmFeign = alarmFeign;
}
/** * batchMethod Set the interface name of the batch method * HystrixProperty name Set to time delay timerDelayInMilliseconds That is, how long the request is submitted as a batch ;value Is the time threshold , In milliseconds * @param id * @return */
@HystrixCollapser(batchMethod = "findBatch",
collapserProperties = {
@HystrixProperty(name = "timerDelayInMilliseconds", value = "1000")})
public Future<AlarmDictionary> find(Integer id) {
throw new RuntimeException("This method body should not be executed");
}
@HystrixCommand
public List<AlarmDictionary> findBatch(List<Integer> alarms) {
List<AlarmDictionary> alarmDictionaries = alarmFeign.getBatch(alarms);
System.out.println(Thread.currentThread().getName() + ":" + alarmDictionaries);
return alarmDictionaries;
}
}
Service providers ( Of course, you can't use remote here , Using the internal batch logic of the service )
@RestController
@RequestMapping("/alarm/dictionary")
public class AlarmDictionaryController {
static List<AlarmDictionary> alarms = new ArrayList<>();
static {
alarms.add(new AlarmDictionary(1,"808 First level alarm "));
alarms.add(new AlarmDictionary(2,"808 Level two alarm "));
alarms.add(new AlarmDictionary(3,"808 Three alarm "));
alarms.add(new AlarmDictionary(4," Fatigue driving level 1 alarm "));
}
@PostMapping("/batch")
public List<AlarmDictionary> getBatchAlarmById(@RequestBody List<Integer> alarmIds) {
return alarms.stream().filter(x -> alarmIds.contains(x.getId())).collect(Collectors.toList());
}
}
test result

problem :
Hystrix The request must be in the request response mode , That is to say , A request must have a corresponding result , If the response is empty , An exception will be thrown
Caused by: java.lang.RuntimeException: Failed to map all collapsed requests to response. The expected contract has not been respected. Collapser key: 'find', requests size: '1', response size: '0'
at com.netflix.hystrix.contrib.javanica.collapser.CommandCollapser.mapResponseToRequests(CommandCollapser.java:76)
at com.netflix.hystrix.contrib.javanica.collapser.CommandCollapser.mapResponseToRequests(CommandCollapser.java:33)
at com.netflix.hystrix.HystrixCollapser$1$1.call(HystrixCollapser.java:171)
at rx.internal.util.ActionObserver.onNext(ActionObserver.java:39)
at rx.internal.operators.OnSubscribeDoOnEach$DoOnEachSubscriber.onNext(OnSubscribeDoOnEach.java:96)
... 68 more

At present, we can try-catch Handle , Better solutions need to be updated …
TODO…
Attach project address :hystrix-batch-collapser
边栏推荐
- Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
- Knife4j 2.X版本文件上传无选择文件控件问题解决
- 知识点很细(代码有注释)数构(C语言)——第三章、栈和队列
- Flink - use the streaming batch API to count the number of words
- 破茧|一文说透什么是真正的云原生
- Redis installation and deployment (windows/linux)
- [staff] the lines and spaces of the staff (the nth line and the nth space in the staff | the plus N line and the plus N space on the staff | the plus N line and the plus N space below the staff | the
- What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
- 数构(C语言)——第四章、矩阵的压缩存储(下)
- 西瓜书--第六章.支持向量机(SVM)
猜你喜欢

破茧|一文说透什么是真正的云原生

Ora-12514 problem solving method

告别996,IDEA中必装插件有哪些?

Hengyuan cloud_ Can aiphacode replace programmers?

洞见云原生|微服务及微服务架构浅析

「Redis源码系列」关于源码阅读的学习与思考

Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method

cmd窗口中中文呈现乱码解决方法

Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain

Chrome用户脚本管理器-Tampermonkey 油猴
随机推荐
Chrome浏览器插件-Fatkun安装和介绍
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
Oracle delete tablespace and user
View the port of the application published by was
机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
Microservice practice | declarative service invocation openfeign practice
Leetcode sword finger offer brush questions - day 23
微服务实战|负载均衡组件及源码分析
hystrix 实现请求合并
Matplotlib剑客行——没有工具用代码也能画图的造型师
Oracle修改表空间名称以及数据文件
【Go实战基础】gin 如何自定义和使用一个中间件
2022/2/14 summary
[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)
Oracle modify database character set
Microservice practice | load balancing component and source code analysis
JVM instruction mnemonic
Knife4j 2.X版本文件上传无选择文件控件问题解决
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
oracle修改数据库字符集