簡體   English   中英

如何提高內部其他REST調用的REST調用的性能

[英]How to improve the performance of a REST call which internally other REST calls

我正在創建一個端點,該端點檢索我一些數據,並且在此調用中它調用了3個不同的REST調用,因此,這會影響應用程序的性能。

My Endpoint Code:

 1. REST call to get the userApps()
 2. iterate over userAPPs
    2.1 make REST call to get the appDetails
    2.2 make use of above response to call the 3rd REST call which returns list.
    2.3 iterate over the above list and filter out the required fields and put it in main response object.
 3.return response

因此,這種復雜性會影響性能。

我嘗試添加多線程概念,但是普通代碼和多線程所花費的時間幾乎相同。

條件就像,我們不能修改3個外部REST調用來支持分頁。

我們無法添加分頁,因為我們沒有任何數據庫。 有什么解決辦法嗎?

您不應該添加線程,而應該完全刪除線程。 也就是說,您應該使所有代碼都無阻塞。 這只是意味着所有工作基本上都將在http客戶端的線程池中完成,而所有等待都可以在操作系統的選擇器中完成(我們想要的)。

這是一些代碼,假設您的http調用返回CompletableFuture ,那么此核心邏輯將如何工作。

public CompletableFuture<List<Something>> retrieveSomethings() {
    return retrieveUserApps().thenCompose(this::retriveAllAppsSomethings);
}

public CompletableFuture<List<Something>> retrieveAllAppsSomethings(List<UserApp> apps) {
    return CompletableFuture.allOf(
        apps.stream().map(this::retriveAppSomethings).toArray(CompletableFuture[]::new))
    .apply(listOfLists -> listOfLists.stream().flatMap(List::stream).collect(Collectors.toList()))
    .apply(this::filterSomethings);
}

public CompletableFuture<List<Something>> retreiveAppSomethings(UserApp app) {
    return retrieveAppDetails(app).thenCompose(this::retreiveAppDetailSomethings);
}

這一切呢,是使一切無阻塞,這樣就可以並行運行的一切都並行運行。 無需限制任何內容,因為所有內容都將在http客戶端的線程池中運行,而這很有可能受到限制。 沒關系,因為等待不會占用線程。

上面的代碼要做的只是實現retrieveUserApps()retrieveAppDetails(app)retrieveAppDetailSometings(appDetail) 所有這些都應返回CompletableFuture<>並使用HTTP客戶端的啟用異步功能的版本實現。

這將使檢索1個應用程序或100個應用程序的數據相同,因為所有這些應用程序將並行運行(假設它們全部花費相同的時間,並且下游系統可以處理這么多並行請求)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM