Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to handle asynchronous requests of architecture design synchronously

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to handle asynchronous requests of architecture design synchronously". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "how to handle asynchronous requests of architecture design synchronously".

Full text summary:

Problems caused by asynchronism to existing architectur

The solution of changing Dubbo from Asynchronous to synchronous

Design Scheme of Asynchronous to synchronous Architecture

0x00. Preface

There is a system, and the overall architecture is as follows:

This is a common synchronous design, and the upstream system needs to wait for the downstream system interface to return the call result.

Now you need to access another third-party service B, which is most different from Service An in that it is an asynchronous API. After the call, only the acceptance is returned, and the processing result is subsequently returned through asynchronous notification.

After the connection, the overall architecture is as follows:

By default, Dubbo supports synchronous invocation, where a DefaultFuture object is created.

The business thread invokes the DefaultFuture#get method to enter blocking. This code is relatively simple, blocking the line layer by calling Condition#await.

0x02. Forwarding scheme design

According to the Dubbo solution, the solution to problem 1 is relatively simple. The specific process is as follows:

Communication service B generates a unique request ID internally, which is sent to a third party service.

If the request is successful, the internal version uses Map to store the correspondence and causes the business thread to block waiting

Communication service B receives the result of asynchronous notification, finds the corresponding business thread through ID, and wakes up the corresponding thread.

In this design process, you need to set a reasonable timeout, which takes into account the time spent on remote service invocation. You can refer to the following formula:

Business thread wait time = timeout of communication service B interface-time consumed by invoking third party service B interface

The specific code is not posted here, the detailed code is referred to Dubbo DefaultFuture.

Next, let's focus on how the notification service forwards the results to the nodes of the correct communication service B. Here are two ideas:

SocketServer scheme

MQ scheme

2.1 SocketServer

Communication service B uses SocketServer to build a service receiver program. When the notification receiver receives a third-party service B notification, it forwards the result to communication service B through Socket.

The overall system architecture is as follows:

After receiving the asynchronous notification, the notification receiver sends the result directly to MQ.

Communication service B starts the broadcast consumption mode and pulls MQ messages.

The communication service Broug1 pulls the message and does not find the internal waiting thread by requesting the ID mapping relationship, knowing that this is not its own waiting message, so it can be discarded directly.

The communication service Baub2 pulls the message, successfully finds the waiting thread by requesting the ID mapping relationship, and then wakes up the waiting thread to return the final result.

Compared with the SocketServer scheme, the overall process of the MQ scheme is relatively simple, the programming difficulty is low, and there is no special configuration.

However, this scheme relies heavily on the real-time performance of MQ messages. if the delivery delay of MQ messages is very high, it will lead to the overtime awakening of communication service B business threads and the abnormal return of business.

Here we choose to use RocketMQ, long polling Pull, to ensure that the message is very real-time.

Thank you for your reading. the above is the content of "how to handle asynchronous requests of architecture design synchronously". After the study of this article, I believe you have a deeper understanding of the problem of how to handle asynchronous requests of architecture design synchronously. the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report