This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-website.git
The following commit(s) were added to refs/heads/master by this push:
new 005649eec92 [majorization] 异步调用 (#1398)
005649eec92 is described below
commit 005649eec922bf190782ce707604427186cb7c1c
Author: JIAN ZHONG <[email protected]>
AuthorDate: Sun Aug 21 20:34:18 2022 +0800
[majorization] 异步调用 (#1398)
---
.../service/async-call.md | 73 +++++++++-------------
1 file changed, 29 insertions(+), 44 deletions(-)
diff --git
a/content/zh/docs3-v2/java-sdk/advanced-features-and-usage/service/async-call.md
b/content/zh/docs3-v2/java-sdk/advanced-features-and-usage/service/async-call.md
index 7821d27b950..0cdaaa19662 100644
---
a/content/zh/docs3-v2/java-sdk/advanced-features-and-usage/service/async-call.md
+++
b/content/zh/docs3-v2/java-sdk/advanced-features-and-usage/service/async-call.md
@@ -6,7 +6,8 @@ weight: 3
description: "在 Dubbo 中发起异步调用"
---
-## 背景
+## 特性说明
+#### 背景
从 2.7.0 开始,Dubbo 的所有异步编程接口开始以
[CompletableFuture](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html)
为基础
@@ -14,36 +15,33 @@ description: "在 Dubbo 中发起异步调用"

-## 使用 CompletableFuture 签名的接口
+## 参考用例
+
+[https://github.com/apache/dubbo-samples/tree/master/dubbo-samples-async](https://github.com/apache/dubbo-samples/tree/master/dubbo-samples-async)
+## 使用场景
+## 使用方式
+### 使用 CompletableFuture 签名的接口
需要服务提供者事先定义 CompletableFuture 签名的服务,接口定义指南如下:
Provider端异步执行将阻塞的业务从Dubbo内部线程池切换到业务自定义线程,避免Dubbo线程池的过度占用,有助于避免不同服务间的互相影响。异步执行无异于节省资源或提升RPC响应性能,因为如果业务执行需要阻塞,则始终还是要有线程来负责执行。
-{{% alert title="注意" color="warning" %}}
-Provider 端异步执行和 Consumer 端异步调用是相互独立的,你可以任意正交组合两端配置
-- Consumer同步 - Provider同步
-- Consumer异步 - Provider同步
-- Consumer同步 - Provider异步
-- Consumer异步 - Provider异步
- {{% /alert %}}
+> **Provider 端异步执行和 Consumer 端异步调用是相互独立的,任意正交组合两端配置**
+> - Consumer同步 - Provider同步
+> - Consumer异步 - Provider同步
+> - Consumer同步 - Provider异步
+> - Consumer异步 - Provider异步
-## 参考用例
-
-[https://github.com/apache/dubbo-samples/tree/master/dubbo-samples-async](https://github.com/apache/dubbo-samples/tree/master/dubbo-samples-async)
-
-## 定义 CompletableFuture 签名的接口
-
-服务接口定义:
+### 定义 CompletableFuture 签名的接口
+服务接口定义
```java
public interface AsyncService {
CompletableFuture<String> sayHello(String name);
}
```
-服务实现:
-
+服务实现
```java
public class AsyncServiceImpl implements AsyncService {
@Override
@@ -65,27 +63,24 @@ public class AsyncServiceImpl implements AsyncService {
-## 使用AsyncContext
+### 使用 AsyncContext
Dubbo 提供了一个类似 Servlet 3.0 的异步接口`AsyncContext`,在没有 CompletableFuture
签名接口的情况下,也可以实现 Provider 端的异步执行。
-服务接口定义:
-
+服务接口定义
```java
public interface AsyncService {
String sayHello(String name);
}
```
-服务暴露,和普通服务完全一致:
-
+服务暴露,和普通服务完全一致
```xml
<bean id="asyncService"
class="org.apache.dubbo.samples.governance.impl.AsyncServiceImpl"/>
<dubbo:service
interface="org.apache.dubbo.samples.governance.api.AsyncService"
ref="asyncService"/>
```
-服务实现:
-
+服务实现
```java
public class AsyncServiceImpl implements AsyncService {
public String sayHello(String name) {
@@ -108,14 +103,12 @@ public class AsyncServiceImpl implements AsyncService {
注意接口的返回类型是 `CompletableFuture<String>`。
-XML引用服务:
-
+XML 引用服务
```xml
<dubbo:reference id="asyncService" timeout="10000"
interface="com.alibaba.dubbo.samples.async.api.AsyncService"/>
```
-调用远程服务:
-
+调用远程服务
```java
// 调用直接返回CompletableFuture
CompletableFuture<String> future = asyncService.sayHello("async call request");
@@ -131,18 +124,15 @@ future.whenComplete((v, t) -> {
System.out.println("Executed before response return.");
```
-## 使用 RpcContext
-
-在 consumer.xml 中配置:
-
+### 使用 RpcContext
+在 consumer.xml 中配置
```xml
<dubbo:reference id="asyncService"
interface="org.apache.dubbo.samples.governance.api.AsyncService">
<dubbo:method name="sayHello" async="true" />
</dubbo:reference>
```
-调用代码:
-
+调用代码
```java
// 此调用会立即返回null
asyncService.sayHello("world");
@@ -158,8 +148,7 @@ helloFuture.whenComplete((retValue, exception) -> {
});
```
-或者,你也可以这样做异步调用:
-
+或者,也可以这样做异步调用
```java
CompletableFuture<String> future = RpcContext.getServiceContext().asyncCall(
() -> {
@@ -171,19 +160,15 @@ future.get();
```
-你也可以设置是否等待消息发出: [^1]
-
-- `sent="true"` 等待消息发出,消息发送失败将抛出异常。
+**异步总是不等待返回**,你也可以设置是否等待消息发出
+- `sent="true"` 等待消息发出,消息发送失败将抛出异常。
- `sent="false"` 不等待消息发出,将消息放入 IO 队列,即刻返回。
```xml
<dubbo:method name="findFoo" async="true" sent="true" />
```
-如果你只是想异步,完全忽略返回值,可以配置 `return="false"`,以减少 Future 对象的创建和管理成本:
-
+如果你只是想异步,完全忽略返回值,可以配置 `return="false"`,以减少 Future 对象的创建和管理成本
```xml
<dubbo:method name="findFoo" async="true" return="false" />
```
-
-[^1]: 异步总是不等待返回
\ No newline at end of file