面试碰到一个问题,搞不定

2020-12-16 22:00:43 +08:00
 luxinfl
比如说要调用 abcde 这五个微服务,调用 b 的前提是先调用 a,cde 没有这种顺序要求。最终数据要求整合。怎么才能让时延变小??
6509 次点击
所在节点    程序员
44 条回复
Cbdy
2020-12-17 10:35:42 +08:00
我是 4L,我把原先的 JS 版改成 Java 版
var resultList = Stream.of(CompletableFuture.supplyAsync(() -> a()).thenApply(ra -> Stream.of(ra, b())),
CompletableFuture.supplyAsync(() -> c()),
CompletableFuture.supplyAsync(() -> d()),
CompletableFuture.supplyAsync(() -> e()))
.map(CompletableFuture::join)
.flatMap(it -> it instanceof Stream ? ((Stream<?>) it) : Stream.of(it))
.collect(Collectors.toList());
gmywq0392
2020-12-17 10:37:30 +08:00
回调
dddz97
2020-12-17 10:40:13 +08:00
感觉想问的点是 MQ 之类的
shyrock
2020-12-17 10:54:30 +08:00
插眼等有效回答。
qiaobeier
2020-12-17 10:58:19 +08:00
无非轮询事件回调。
fantastM
2020-12-17 11:39:18 +08:00
先同步调用 a,再用线程池异步并行地调用 bcde 。

因为整合数据时,需要保证 bcde 请求调用完成,所以就用一下 CountDownLatch 来实现这个等待机制。
XDJI
2020-12-17 14:36:03 +08:00
就是 20 21 解法 全链路异步
kkkkkrua
2020-12-17 14:40:27 +08:00
这不就是考察 CompletableFuture ?
surfire91
2020-12-17 17:17:04 +08:00
@fantastM 为什么不先调用 acde,然后等 a 返回了再调 b
wangritian
2020-12-17 17:31:01 +08:00
a+b 一个 goroutine,cde 各一个 goroutine,主线程 waitgroup
zy445566
2020-12-17 17:41:49 +08:00
如果是 js 的话,这样就行
```js
const data = await a();
const dataList = await Promise.all([c(),d(),d()]);
// 整合数据
```
Jooooooooo
2020-12-17 18:46:30 +08:00
20l 答案差不多了
liian2019
2020-12-17 19:01:54 +08:00
public static void test() throws Exception{
Map<String,String> resultMap = new HashMap<>();
CompletableFuture.allOf(CompletableFuture.supplyAsync(() -> {
// 调用 A
String aResult = "Hello";
resultMap.put("A",aResult);
return aResult;
}, executor).thenAcceptAsync(aResult -> {
// 调用 B
String bResult = aResult + " World";
resultMap.put("B",bResult);
}), CompletableFuture.runAsync(() -> {
// 调用 C
String cResult = "CValue";
resultMap.put("C",cResult);
},executor), CompletableFuture.runAsync(() -> {
// 调用 D
String dResult = "DValue";
resultMap.put("D",dResult);
},executor),
CompletableFuture.runAsync(() -> {
// 调用 E
String eResult = "EValue";
resultMap.put("E",eResult);
},executor)).join();
System.out.println(JSON.toJSONString(resultMap));
}
luvroot
2020-12-17 19:14:30 +08:00
ab 打包成一个微服务,ecd 做成 3 个微服务,即可
gengzi
2020-12-17 19:24:22 +08:00
多线程异步( Callable ),异步调用 a,调用 cde,等待 a 返回结果,再调用 b 。 最终的时延 acde 服务最长的一个耗时+b 服务耗时。
crclz
2020-12-17 19:30:22 +08:00
System.ReactiveX rxjs rxjava
mxT52CRuqR6o5
2020-12-17 19:30:37 +08:00
就是在满足调用条件时就马上去调用,把这个逻辑写出来就行
Achieve7
2020-12-17 19:30:50 +08:00
CompletableFuture 或者 ForkJoinPool 都可以 如果是微服务 就异步呗
IssacTomatoTan
2020-12-17 19:42:57 +08:00
Js:
const f = new Promise(async (r)=>{
await a();
await b();
r();
})
promise.all([c,d,e,f]).then
laminux29
2020-12-18 00:57:43 +08:00
微服务这功能,从本质上来说,首先是软件工程与管理上的问题,目的是为多人协同的大中型软件项目服务,是用增加组件间延时的代价来换取更快的平均运行性能与开发协作效率,所以时延高是必须,是代价,没办法从原理上降低。否则,如果低时延是刚需,那就不应该用微服务,而是单机 + 进程内部调用,这样子延时才会最小化。

其他细节上的问题,楼上的老哥们已经说了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/736181

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX