The asynchronous implementation of Dubbo 2.7 #14195
-
Pre-check
Apache Dubbo ComponentJava SDK (apache/dubbo) Detailsdubbo 2.7的异步化实现:用户发起远程调用时,客户端通过Netty发送消息给服务端。服务端Netty收到请求后将任务提交到线程池。当服务端线程池里的任务执行完成时,服务端通过Netty发送Response消息给客户端,客户端将处理Response的任务提交到线程池。当客户端线程池里的任务执行完成时,触发CompletableFuture完成的动作,此时客户端可以拿到同步结果。
(For English speaker)The asynchronous implementation of Dubbo 2.7:When a user initiates a remote call, the client sends a message to the server through Netty. Upon receiving the request, the server's Netty submits the task to a thread pool. When the task in the server's thread pool is completed, the server sends a Response message to the client via Netty, and the client submits the task of processing the Response to a thread pool. When the task in the client's thread pool is completed, it triggers the completion action of CompletableFuture. At this point, the client can obtain the synchronous result.
问题dubbo 2.7以上有异步化特性,在客户端实现异步转同步,那么生产中用户在调用的时候还有必要再包一层CompletableFuture提交到线程池吗?这样可以避免流量直接打到下游,因为流量过高上游线程池会拒绝? (For English speaker)Question:In Dubbo 2.7 and above, there are asynchronous features. When implementing asynchronous to synchronous conversion on the client side, is it necessary for users in production to wrap another layer of CompletableFuture and submit it to the thread pool when making a call? This can help avoid high traffic directly hitting the downstream, as the upstream thread pool may reject the traffic. The main purpose is to explore the advantages and disadvantages of these two approaches. Below is a load testing result for a demo interface corresponding to the two approaches mentioned above: Test code public interface DemoService {
String sayHello(String var1);
default CompletableFuture<String> sayHiAsync(String name) {
return CompletableFuture.completedFuture(this.sayHello(name));
}
}
@RestController
public class DemoController {
@Reference
DemoService demoService;
@Resource(name = "common-thread-pool")
ThreadPoolExecutor commonThreadPool;
@PostMapping("/demo")
public String demoService() throws ExecutionException, InterruptedException, TimeoutException {
return CompletableFuture
.supplyAsync(() -> demoService.sayHello("Hello"),
commonThreadPool).get(10, TimeUnit.SECONDS);
}
@PostMapping("/demoAsync")
public String demoServiceAsync() throws ExecutionException, InterruptedException {
CompletableFuture<String> res = demoService.sayHiAsync("Hello");
return res.get();
}
} 会有什么坑不?敬请大佬解答 Code of Conduct
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This is depends on your coding style. Dubbo make it all possible. In most scenarios, performance is not important enough or users cannot feel the difference of performance. So, choose the one you like. |
Beta Was this translation helpful? Give feedback.
This is depends on your coding style. Dubbo make it all possible. In most scenarios, performance is not important enough or users cannot feel the difference of performance. So, choose the one you like.