简体   繁体   English

如何优化 REST API 响应时间

[英]How to Optimize REST API Response time

am working on a E-commerce project which has internally four projects(parts).我正在做一个电子商务项目,内部有四个项目(部分)。
1.) core -has users info 1.)核心- 有用户信息
2.) cms -contents info 2.) cms -内容信息
3.) oms -orders info 3.) oms -订单信息
4.) wallet -payment info 4.)钱包-支付信息

Used:-使用:-
UI -AngularJs用户界面-AngularJs
Backend -java后端-java

  • No third paty api calls.没有第三个 paty api 调用。
  • caching already used缓存已使用
  • only http calls among above four projects.以上四个项目中只有http调用。

we have multiple REST API calls one after other to same domain(with different path and query parameters).我们有多个 REST API 一个接一个地调用到同一个域(具有不同的路径和查询参数)。

What are the ways that I can optimize api response time.我可以优化api响应时间的方法有哪些。

Suggestions for java and AngularJs is welcome,rather than caching give me more suggestions that i can use to optimize response time of api's.欢迎对 java 和 AngularJs 提出建议,而不是缓存给我更多的建议,我可以用它来优化 api 的响应时间。

Appreciated!!!赞赏!!!

I have found same question on stackoverflow How to Optimize REST API calls ,answer is helpul but found no best solution for me.我在 stackoverflow 如何优化 REST API 调用上发现了同样的问题,答案很有帮助,但没有找到适合我的最佳解决方案。

Try using the below configurations for Java-spring projects :尝试对 Java-spring 项目使用以下配置:

# Enable response compression
server.compression.enabled=true

# The comma-separated list of mime types that should be compressed
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json

# Compress the response only if the response size is at least 1KB
server.compression.min-response-size=1024

# Enable HTTP/2 support, if the current environment supports it
server.http2.enabled=true

What is the current response time you have in ms and what you are aiming to?您当前的响应时间(以毫秒为单位)是多少?您的目标是什么?

A good cached Api should work at the java level around 1-30ms depends on the amount of content and the type of cachong you are using.一个好的缓存 Api 应该在 1-30 毫秒左右的 Java 级别工作,具体取决于您使用的内容量和缓存类型。

Working with partial resources使用部分资源

A simple way to improve the performance of your API calls is by sending and receiving only the portion of the data that you're interested in. This lets your application avoid transferring, parsing, and storing unneeded fields, so it can use resources including network, CPU, and memory more efficiently.提高 API 调用性能的一种简单方法是仅发送和接收您感兴趣的数据部分。这让您的应用程序避免传输、解析和存储不需要的字段,因此它可以使用资源,包括网络、CPU 和内存更高效。

There are two types of partial requests:有两种类型的部分请求:

Partial response:部分回应:

A request where you specify which fields to include in the response (use the fields request parameter).您指定要在响应中包含哪些字段的请求(使用 fields 请求参数)。 Patch: An update request where you send only the fields you want to change (use the PATCH HTTP verb).补丁:一个更新请求,您只发送要更改的字段(使用 PATCH HTTP 动词)。 More details on making partial requests are provided in the following sections.以下部分提供了有关发出部分请求的更多详细信息。 Partial response By default, the server sends back the full representation of a resource after processing requests.部分响应 默认情况下,服务器在处理请求后发回资源的完整表示。 For better performance, you can ask the server to send only the fields you really need and get a partial response instead.为了获得更好的性能,您可以要求服务器仅发送您真正需要的字段并获得部分响应。 To request a partial response, use the fields request parameter to specify the fields you want returned.要请求部分响应,请使用 fields 请求参数指定要返回的字段。 You can use this parameter with any request that returns response data.您可以将此参数用于任何返回响应数据的请求。

Note that the fields parameter only affects the response data;注意 fields 参数只影响响应数据; it does not affect the data that you need to send, if any.它不会影响您需要发送的数据(如果有)。 To reduce the amount of data you send when modifying resources, use a patch request.要减少修改资源时发送的数据量,请使用补丁请求。

Example
The following example shows the use of the fields parameter with a generic 
(fictional) "Demo" API.

Simple request: This HTTP GET request omits the fields parameter and returns 
the full resource.

https://www.googleapis.com/demo/v1?key=YOUR-API-KEY
Full resource response: The full resource data includes the following 
fields, along with many others that have been omitted for brevity.

{
  "kind": "demo",
  ...
  "items": [
  {
    "title": "First title",
    "comment": "First comment.",
    "characteristics": {
      "length": "short",
      "accuracy": "high",
      "followers": ["Jo", "Will"],
    },
    "status": "active",
    ...
  },
  {
    "title": "Second title",
    "comment": "Second comment.",
    "characteristics": {
      "length": "long",
      "accuracy": "medium"
      "followers": [ ],
    },
    "status": "pending",
    ...
  },
  ...
  ]
}

Request for a partial response: The following request for this same resource uses the fields parameter to significantly reduce the amount of data returned.请求部分响应:以下对同一资源的请求使用 fields 参数来显着减少返回的数据量。

Partial response: In response to the request above, the server sends back a response that contains only the kind information along with a pared-down items array that includes only HTML title and length characteristic information in each item.部分响应:作为对上述请求的响应,服务器发回一个仅包含种类信息的响应以及一个精简的项目数组,该数组仅包含每个项目中的 HTML 标题和长度特征信息。

Response status : 200 OK响应状态:200 OK

It seems that almost all the necessary steps you have taken to improve your performance and to reduce the response time.似乎您已经采取了几乎所有必要的步骤来提高性能和减少响应时间。

To make it even quicker, make sure you are collecting only adequate data from user requests.为了使其更快,请确保仅从用户请求中收集足够的数据。 Not to forget to respond with the only data which is really required as in if there is a response entity that returns ten data fields but only two of them are required.不要忘记使用真正需要的唯一数据进行响应,就像有一个响应实体返回十个数据字段但只需要其中两个字段一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM