简体   繁体   English

REST API 设计 - 异步 REST 客户端与异步 REST API

[英]REST API Design - Async REST Client Vs Async REST API

I already have REST API (for System-to-System communication) which takes lot of time to process.我已经有 REST API(用于系统到系统通信),这需要很多时间来处理。

I want to have asynchronous processing.我想要异步处理。 I see two options here:我在这里看到两个选项:

  1. To make the API itself as asynchronous, where it returns a LOCATION header giving another URI to fetch result.使 API 本身成为异步的,它返回一个 LOCATION header 给出另一个 URI 来获取结果。

  2. To make the client asynchronous - using asynchronous HTTP Client or AsyncRestTemplate etc.使客户端异步 - 使用异步 HTTP 客户端或 AsyncRestTemplate 等。

I was wondering what is better way in such scenarios, as both seems to solve the issue.我想知道在这种情况下有什么更好的方法,因为两者似乎都可以解决问题。

There are some pros and cons to each of the options you mentioned that you may want to consider when making a decision.您提到的每个选项都有一些优点和缺点,您在做出决定时可能需要考虑这些选项。

Asynchronous API异步 API

This approach has a lot of benefits, such as allowing the API to process requests in parallel and improving the overall performance and scalability of the system.这种方式有很多好处,比如可以让API并行处理请求,提高系统的整体性能和可扩展性。 However, this approach can also add some complexity to the API, as it requires the API to implement asynchronous processing and provide a mechanism for clients to fetch the result of a request.但是,这种方法也会给 API 增加一些复杂性,因为它需要 API 实现异步处理并为客户端提供获取请求结果的机制。

Asynchronous client异步客户端

This approach can provide a simpler and more straightforward solution, as it does not require any changes to the API itself.这种方法可以提供更简单、更直接的解决方案,因为它不需要对 API 本身进行任何更改。 This approach can also improve the performance and scalability of the system, as it allows the client to process multiple requests in parallel and handle responses asynchronously.这种方法还可以提高系统的性能和可扩展性,因为它允许客户端并行处理多个请求并异步处理响应。 However, this approach may require the client to implement additional logic to handle asynchronous processing and fetching results, which can add some complexity to the client.但是,这种方法可能需要客户端实现额外的逻辑来处理异步处理和获取结果,这会给客户端增加一些复杂性。

Summary概括

Making the API asynchronous can provide better performance and scalability, but may require more complex implementation, while making the client asynchronous can provide a simpler solution, but may not provide the same level of performance and scalability.使 API 异步可以提供更好的性能和可扩展性,但可能需要更复杂的实现,而使客户端异步可以提供更简单的解决方案,但可能无法提供相同级别的性能和可扩展性。 You will need to weigh the pros and cons of each approach and decide which is best for your specific system based on your requirements and constraints.您需要权衡每种方法的优缺点,并根据您的要求和限制决定哪种方法最适合您的特定系统。

It depends on your specific use case and requirements.这取决于您的具体用例和要求。

If you have a lot of requests coming in from multiple clients, making the API asynchronous may be the best option as it allows you to scale better and process requests in parallel.如果您有来自多个客户端的大量请求,使 API 异步可能是最佳选择,因为它允许您更好地扩展并并行处理请求。

On the other hand, if your API is already built and you are just looking to improve performance of the requests, using an asynchronous client may be the best option as it will allow the requests to be sent in parallel and processed faster.另一方面,如果您的 API 已经构建,并且您只是想提高请求的性能,那么使用异步客户端可能是最佳选择,因为它允许并行发送请求并更快地处理。

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

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