Java 如何优化 REST API 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19193985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:56:13  来源:igfitidea点击:

How to Optimize REST API calls

javajavascriptrestmobile

提问by randomness

I am in process of building Mash up mobile application. I need to call my API Provider and integrate with Facebook , twitter etc. During this process, I have to make multiple REST API calls one after other to same domain(with different path and query parameters ofcourse). Also the API calls has to be sequential as in result of one is required to call the next. What are the ways I can optimize these http calls to avoid the round trip. Suggestions for java and js is welcome

我正在构建 Mash up 移动应用程序。我需要调用我的 API Provider 并与 Facebook、twitter 等集成。在此过程中,我必须对同一个域一个接一个地进行多个 REST API 调用(当然具有不同的路径和查询参数)。此外,API 调用必须是顺序的,因为需要一个调用下一​​个。我可以通过哪些方式优化这些 http 调用以避免往返。欢迎对 java 和 js 提出建议

回答by jfriend00

If you need the results of the first API call in order to make the next API call, then there is nothing you can do client-side to avoid a sequential round-trip for each one. There would either need to be an existing API call that can be used to combine multiple calls into one or you would need to add such an API call to the server or you would need to use a proxy server that does that on your behalf.

如果您需要第一个 API 调用的结果来进行下一个 API 调用,那么您在客户端无法做任何事情来避免每个 API 的顺序往返。要么需要一个现有的 API 调用,可用于将多个调用合并为一个调用,或者您需要将此类 API 调用添加到服务器,或者您需要使用代表您执行此操作的代理服务器。

If you don't need the results of the first API call in order to make the next API call, but you just want to process the results in sequential order, then you can make all the API calls at once and structure your response code to process them in order (saving any results that arrive out of order for later processing).

如果您不需要第一个 API 调用的结果来进行下一个 API 调用,而只想按顺序处理结果,那么您可以一次进行所有 API 调用并将响应代码构建为按顺序处理它们(保存任何乱序到达的结果以供以后处理)。

回答by Santosh

The requirements stated are broad. Since you are using the public third party API, that somewhat limits the scope of possible optimizations. There is absolutely nothing that you can do to speed up the APIs as they do not belong to you.

规定的要求很广泛。由于您使用的是公共第三方 API,这在一定程度上限制了可能优化的范围。您绝对无法加速 API,因为它们不属于您。

In general I suggest following guidelines which will help you come up with better application.

一般来说,我建议遵循以下指南,这将有助于您提出更好的应用程序。

  1. Thumb rule is make as few API calls as possible.
  2. Making fewer network calls (resulting from API calls) will imply that the device radio (which is used to communicate with the server over the air) will be less frequently used and hence less power required.
  3. Now, making fewer number of calls may not be an option always. So study the public API documentationcarefully and make sure you know the all the details of the APIs you need.
  4. For example, there could be an API which gives details of an entity upon passing its identifier. If you need the details of 5 such entities then there should be an API which takes a bunch of IDs and returns all the details at once. This way you save multiple calls.
  5. Cachingis very effective way of reducing the number of API call. So look for any data received from server which can be cached for certain duration.
  6. Note that caching would need more memory and hence make sure you are aware of its implication and accordingly decide the amount of data that needs to cached.
  7. Apart from memory, the data can be cached on the disc as well, which will help reduce the memory usage. Not sure if you are using Android, please refer to this related post.
  1. Thumb 规则是尽可能少地调用 API
  2. 更少的网络调用(由 API 调用引起)将意味着设备无线电(用于通过空中与服务器进行通信)将不那么频繁地使用,因此需要更少的功率。
  3. 现在,减少通话次数可能并不总是一种选择。因此,请仔细研究公共 API 文档,并确保您了解所需 API 的所有详细信息。
  4. 例如,可能有一个 API,它在传递实体标识符时提供实体的详细信息。如果您需要 5 个这样的实体的详细信息,那么应该有一个 API,它接受一堆 ID 并一次返回所有详细信息。这样您就可以保存多个呼叫
  5. 缓存是减少 API 调用次数的非常有效的方法。因此,查找从服务器收到的任何可以缓存一段时间的数据。
  6. 请注意,缓存需要更多内存,因此请确保您了解其含义并相应地决定需要缓存的数据量。
  7. 除了内存,数据也可以缓存在磁盘上,这将有助于减少内存使用。不确定您是否使用 Android,请参阅此相关帖子

On the side note, you can refer to following links which offer general guidelines for developing a mobile App.

在旁注中,您可以参考以下链接,这些链接提供了开发移动应用程序的一般指南。

  1. Make your application blazing fast.
  2. Optimizing a Mobile App for Mobile network.
  1. 使您的应用程序快速运行
  2. 针对移动网络优化移动应用程序

Please note that these are general guidelines. Some may not apply to your particular use case.

请注意,这些是一般准则。有些可能不适用于您的特定用例。

回答by Lifecube

If you could use jQuery, I suggest you have a look my plugin jQuery Chainwhich could help make async call sequential.

如果您可以使用 jQuery,我建议您查看我的插件jQuery Chain,它可以帮助进行异步调用顺序。

回答by AL-Tamimi

REST is unified and therefore, filters and round trips are necessary to accomplish a certain functionality. When it comes to optimization and imitating multiple clients request into ONE, you will slowly start facing challenges of a unified paradigm since it client can be a lot more specific than the contract can do in ONE UNIFIED call. While REST can be heavily optimized to drop the number of API calls to two or even one call, sometimes, your client request will require complex filtering that will require a feedback from the server prior to applying additional filtering logic.

REST 是统一的,因此,过滤器和往返行程是完成特定功能所必需的。当谈到优化和将多个客户端请求模拟到 ONE 中时,您将慢慢开始面临统一范式的挑战,因为它的客户端可以比 ONE UNIFIED 调用中的合约更具体。虽然可以对 REST 进行大量优化,将 API 调用的数量减少到两次甚至一次调用,但有时,您的客户端请求将需要复杂的过滤,在应用额外的过滤逻辑之前,需要来自服务器的反馈。

If your requirements that you absolutely have to have ONE API call to handle specific client request from the server instead of the client while all filters is not applicable, then RPC will be your way to go, which will take care of specific business logic in a restful manner from the server to other micro-services (SOA.)

如果您的要求绝对必须有一个 API 调用来处理来自服务器而不是客户端的特定客户端请求,而所有过滤器都不适用,那么 RPC 将是您要走的路,它将处理特定的业务逻辑从服务器到其他微服务(SOA)的宁静方式

The real question is why you would have the server to handle such request if you want to keep your REST API unified and consistent and have the clients implement their own unique logic. The REST API server should provide the client with hyperlinks/hypermedia to guide the client of best way to reduce the number of API calls whether by filtering or any other entity/resource.

真正的问题是,如果您想让 REST API 保持统一和一致并让客户端实现自己独特的逻辑,为什么要让服务器来处理此类请求。REST API 服务器应为客户端提供超链接/超媒体,以引导客户端以最佳方式减少 API 调用次数,无论是通过过滤还是任何其他实体/资源。

回答by Joe

Programming level- use code optimization/threading/parallel programming.

编程级别——使用代码优化/线程/并行编程。

API level- use pagination/filters/ranges to generate fine-grained data that is tailored to the need of the client.

API 级别- 使用分页/过滤器/范围来生成适合客户需求的细粒度数据。

Cache- Have heard somewhere that the "fastest HTTP call is the one that you don't make" that is, use caching(Memcache/Redis), most of the requests are read operations, wise use of cache control headers are also useful. That spares the database from serving repeated read requests which are expensive. (Not sure if Etags can be of any use ?)

缓存- 在某处听说过“最快的 HTTP 调用是你不做的那个”,即使用缓存(Memcache/Redis),大多数请求是读取操作,明智地使用缓存控制标头也很有用。这使数据库免于服务于昂贵的重复读取请求。(不确定 Etags 是否有任何用处?)

Request/Response- Use JSON serializer and gZip compress techniques for the data transmitted over the web.

请求/响应- 对通过网络传输的数据使用 JSON 序列化程序和 gZip 压缩技术。

Geography- Account for the distance between end users and origin servers, we can use CDNs for storing the static contents of the response, so that it can be served real fast.

地理- 考虑到最终用户和源服务器之间的距离,我们可以使用 CDN 来存储响应的静态内容,以便可以真正快速地提供服务。

(Have heard about asynchronous methods/reactive programming which improves the performance of APIs, but not so sure)

(听说过异步方法/反应式编程可以提高 API 的性能,但不太确定)