Dubbo異步調用

異步調用,

對於 Provider 端不需要做特別的配置。

Consumer 配置:

<dubbo:reference id="asyncService" interface="com.x.x.AsyncService">
    <dubbo:method name="testAsync" async="true"/>
</dubbo:reference>

Consumer 端發起調用:

String result = asyncService.testAsync("samples");// 這裏的返回值爲空,請不要使用
Future<String> future = RpcContext.getContext().getFuture();
... // 業務線程可以開始做其他事情
result = future.get(); // 阻塞需要獲取異步結果時,也可以使用 get(timeout, unit) 設置超時時間


Dubbo Consumer端發起調用後,同時通過RpcContext.getContext().getFuture()獲取跟返回結果關聯的Future對象,然後就可以開始處理其他任務;當需要這次異步調用的結果時,可以在任意時刻通過future.get(timeout)來獲取。

一些特殊場景下,爲了儘快調用返回,可以設置是否等待消息發出:

sent="true" 等待消息發出,消息發送失敗將拋出異常;
sent="false" 不等待消息發出,將消息放入 IO 隊列,即刻返回。
return="false" 完全忽略是否返回

<dubbo:method name="testAsync" async="true" sent="true" />
或者
<dubbo:method name="testAsync" async="true" return="false"/>

默認是sent="false"。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章