当前位置:网站首页>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
边栏推荐
- Say goodbye to 996. What are the necessary plug-ins in idea?
- 微服务实战|Eureka注册中心及集群搭建
- Redis installation and deployment (windows/linux)
- Actual combat of microservices | discovery and invocation of original ecosystem implementation services
- oracle删除表空间及用户
- View the port of the application published by was
- 自定义Redis连接池
- [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
- Oracle delete tablespace and user
- Microservice practice | fuse hytrix initial experience
猜你喜欢
深入剖析JVM是如何执行Hello World的
告别996,IDEA中必装插件有哪些?
《统计学习方法》——第五章、决策树模型与学习(上)
十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
Chrome browser tag management plug-in – onetab
Redis安装部署(Windows/Linux)
2022/2/13 summary
概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`
随机推荐
京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”
【Go实战基础】gin 如何自定义和使用一个中间件
Chrome video download Plug-in – video downloader for Chrome
Demand delineation executive summary
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
C4D quick start tutorial - Chamfer
MySql报错:unblock with mysqladmin flush-hosts
Long summary (code with comments) number structure (C language) -- Chapter 4, string (Part 1)
ORA-12514问题解决方法
A detailed explanation takes you to reproduce the statistical learning method again -- Chapter 2, perceptron model
Mirror protocol of synthetic asset track
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
微服务实战|手把手教你开发负载均衡组件
Flink - use the streaming batch API to count the number of words
[go practical basis] how can gin get the request parameters of get and post
聊聊消息队列高性能的秘密——零拷贝技术
idea查看字节码配置
以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
Sentinel reports failed to fetch metric connection timeout and connection rejection
Oracle modifies tablespace names and data files