Message idempotent method and device, computer device and storage medium
By obtaining specific expression combinations of message requests as idempotent keys, querying the idempotent information corresponding to the cache keys, and obtaining the processing mode, the problem of the single idempotent mode of messages is solved, realizing dynamic judgment and preventing duplicate transactions, and improving the flexibility and accuracy of message idempotency.
Patent Information
- Application Number
- CN202310439707.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-23
- Publication Date
- 2025-11-25
- Estimated Expiration
- 2043-04-23
AI Technical Summary
In existing technologies, the message idempotency method has a fixed pattern, resulting in insufficient flexibility and an inability to effectively prevent fund fluctuations caused by duplicate transactions.
By obtaining specific expressions of message requests, combining them into idempotent keys, querying the idempotent information corresponding to the cached keys, and obtaining the corresponding message processing mode based on the state of the idempotent information, dynamic message idempotency judgment is achieved. Locking is used to prevent duplicate requests, and multiple idempotent interceptors with various strategies are supported to be attached to the interface to achieve non-intrusive idempotency functionality.
It achieves accuracy and flexibility in message idempotency, effectively prevents duplicate transactions, expands application scenarios, and enhances the flexibility of message idempotency.
Smart Images

Figure CN116582606B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a message idempotency method and device, computer equipment and storage medium. BACKGROUND
[0002] Idempotency refers to the property that the result of a single request and multiple requests of the same operation for a user should remain consistent, for example, for the payment operation of the same order, even if multiple message requests are received, only one deduction can be made, and the actual result of the message request response transaction. For systems in the financial and payment fields, it is very important to do idempotency processing of transactions to prevent repeated transactions caused by multiple message submissions, otherwise it is easy to cause unexpected changes in customer funds, causing disputes. The current message idempotency method has the problem of fixed mode, which leads to insufficient flexibility of message idempotency. SUMMARY
[0003] Therefore, it is necessary to provide a message idempotency method, device, computer equipment, computer readable storage medium and computer program product capable of improving the flexibility of message idempotency in view of the above technical problems.
[0004] In a first aspect, the present application provides a message idempotency method. The method comprises:
[0005] When the idempotency interceptor receives a message request, a specific expression corresponding to the message request is obtained;
[0006] According to the specific expression, a specified field is obtained from the message request, and the specified field is combined into an idempotent key;
[0007] The idempotent group corresponding to the message request is spliced with the idempotent key to obtain a cache key, and idempotent information corresponding to the cache key is queried;
[0008] If the state of the idempotent information is complete, a corresponding message processing mode is obtained;
[0009] According to the message processing mode, the idempotent information is processed to obtain a response message.
[0010] In one embodiment, the method further comprises:
[0011] An idempotency method configuration annotation and an idempotency parameter specification annotation are obtained; the idempotency method configuration annotation comprises a configurable specific expression, a configurable idempotent group, and a configurable message processing mode; and the idempotency parameter specification annotation is used to mark the parameters in the idempotency method configuration annotation;
[0012] The idempotency interceptor is attached to an interface.
[0013] marking the idempotent method configuration annotation on the interface, and marking the idempotent parameter specifying annotation on a formal parameter of the interface.
[0014] In one of the embodiments, the method further comprises:
[0015] if the idempotent information corresponding to the cache key is not queried, then performing a lock processing on the message request based on the cache key;
[0016] if the lock is successful, then entering a business process corresponding to the message request;
[0017] after the business process is completed, generating the idempotent information according to a corresponding message processing mode, and setting a state of the idempotent information as completed.
[0018] In one of the embodiments, the idempotent information is first idempotent information or second idempotent information; and the generating the idempotent information according to the corresponding message processing mode and setting the state of the idempotent information as completed comprises:
[0019] if the message processing mode is a message snapshot mode, then generating the first idempotent information; the first idempotent information comprises a serialized response message;
[0020] if the message processing mode is a message memo mode, then generating the second idempotent information;
[0021] setting the state of the first idempotent information or the second idempotent information as completed, and establishing a first mapping relationship between the cache key and the first idempotent information or the second idempotent information;
[0022] the querying the idempotent information corresponding to the cache key comprises:
[0023] querying the first idempotent information or the second idempotent information corresponding to the cache key according to the first mapping relationship.
[0024] In one of the embodiments, the method further comprises:
[0025] if the lock fails, then entering a waiting polling process;
[0026] the querying the idempotent information corresponding to the cache key comprises:
[0027] if a current request waiting quantity is greater than a critical waiting threshold, then setting a maximum polling quantity as a first preset value; and querying the idempotent information according to the maximum polling quantity;
[0028] If the current number of requests waiting is not greater than the critical waiting threshold, then the maximum number of polling times is set to the second preset value, and the idempotency information is queried at intervals based on the sleep time.
[0029] In one embodiment, the idempotency information further includes abnormal idempotency information, and the method further includes:
[0030] If the business process is abnormally interrupted, obtain the abnormal information;
[0031] The abnormal idempotency information is generated based on the abnormal information, and the state of the abnormal idempotency information is set to interrupt.
[0032] Establish a second mapping relationship between the cache key and the abnormal idempotency information;
[0033] The idempotency information corresponding to the cache key includes:
[0034] Based on the second mapping relationship, query the abnormal idempotency information corresponding to the cache key.
[0035] In one embodiment, the method further includes:
[0036] If the state of the idempotency information is interrupted, an error message is generated based on the idempotency information, and the message idempotency process ends.
[0037] Secondly, this application also provides a message idempotency device. The device includes:
[0038] The first acquisition module is used to acquire a specific expression corresponding to the message request when the idempotent interceptor receives the message request;
[0039] The acquisition and combination module is used to acquire a specified field from the message request based on the specific expression, and combine the specified field into an idempotent key;
[0040] The concatenation and query module is used to concatenate the idempotency group corresponding to the message request with the idempotency key to obtain the cache key, and query the idempotency information corresponding to the cache key;
[0041] The second acquisition module is used to acquire the corresponding message processing mode if the state of the idempotent information is complete.
[0042] The information processing module is used to process the idempotent information according to the message processing mode to obtain a response message.
[0043] In one embodiment, the device further includes:
[0044] The configuration module is configured to acquire an idempotent method configuration annotation and an idempotent parameter designation annotation; the idempotent method configuration annotation comprises a configurable specific expression, a configurable idempotent group, and a configurable message processing mode; the idempotent parameter designation annotation is used for marking a parameter in the idempotent method configuration annotation; the idempotent interceptor is attached to an interface; the idempotent method configuration annotation is marked on the interface, and the idempotent parameter designation annotation is marked on a formal parameter of the interface.
[0045] In one of the embodiments, the apparatus further comprises:
[0046] The generation module is configured to, if the idempotent information corresponding to the cache key is not queried, perform a locking process on the message request based on the cache key; if the locking is successful, enter a business process corresponding to the message request; after the business process is completed, generate the idempotent information according to a corresponding message processing mode, and set a state of the idempotent information as completed.
[0047] In one of the embodiments, the idempotent information is first idempotent information or second idempotent information; the generation module is further configured to, if the message processing mode is a message snapshot mode, generate the first idempotent information; the first idempotent information comprises a serialized response message; if the message processing mode is a message memorandum mode, generate the second idempotent information; set the state of the first idempotent information or the second idempotent information as completed, and establish a first mapping relationship between the cache key and the first idempotent information or the second idempotent information.
[0048] In one of the embodiments, the splicing and querying module is further configured to query the first idempotent information or the second idempotent information corresponding to the cache key according to the first mapping relationship.
[0049] In one of the embodiments, the splicing and querying module is further configured to, if the locking fails, enter a waiting polling process; if a current request waiting quantity is greater than a critical waiting threshold, set a maximum polling quantity as a first preset value, and query the idempotent information according to the maximum polling quantity; if the current request waiting quantity is not greater than the critical waiting threshold, set the maximum polling quantity as a second preset value, and query the idempotent information at intervals according to a sleep time.
[0050] In one of the embodiments, the idempotent information further comprises abnormal idempotent information, and the apparatus further comprises:
[0051] The interrupt processing module is configured to, if the service flow is interrupted abnormally, acquire abnormal information; generate the abnormal idempotent information according to the abnormal information, and set a state of the abnormal idempotent information as interrupted to establish a second mapping relationship between the cache key and the abnormal idempotent information;
[0052] In one of the embodiments, the splicing and querying module is further configured to query the abnormal idempotent information corresponding to the cache key according to the second mapping relationship.
[0053] In one of the embodiments, the interrupt processing module is further configured to, if the state of the idempotent information is interrupted, generate abnormal prompt information according to the idempotent information, and end the message idempotent process.
[0054] In a third aspect, the present application further provides a computer device. The computer device comprises a memory and a processor, the memory stores a computer program, and the processor implements the steps of the above method when executing the computer program.
[0055] In a fourth aspect, the present application further provides a computer readable storage medium. The computer readable storage medium stores a computer program, and the computer program implements the steps of the above method when executed by a processor.
[0056] In a fifth aspect, the present application further provides a computer program product. The computer program product comprises a computer program, and the computer program implements the steps of the above method when executed by a processor.
[0057] The above message idempotent method, device, computer device, storage medium and computer program product, by acquiring a specific expression corresponding to a message request when an idempotent interceptor receives the message request; acquiring a specified field from the message request according to the specific expression, and combining the specified field into an idempotent key; splicing an idempotent group corresponding to the message request and the idempotent key to obtain a cache key, accurate query of the idempotent information corresponding to the cache key is realized, that is, the accuracy of message idempotence is ensured, if the state of the idempotent information is complete, the corresponding message processing mode is acquired; the idempotent information is processed according to the message processing mode to obtain a response message, the corresponding message processing mode is acquired according to the idempotent information, the problem of single mode of message idempotence is solved, dynamic message idempotent judgment is realized, the application scenario of message idempotence is effectively expanded, and the flexibility of message idempotence is improved. BRIEF DESCRIPTION OF DRAWINGS
[0058] Figure 1 An application environment diagram of the message idempotent method in one embodiment;
[0059] Figure 2 A flowchart of the message idempotent method in one embodiment;
[0060] Figure 3 A flowchart for generating the idempotent information step in one embodiment;
[0061] Figure 4a A flowchart for the message idempotent method in another embodiment;
[0062] Figure 4b A flowchart for the message idempotent method in yet another embodiment;
[0063] Figure 5 A block diagram of the structure of the message idempotent device in one embodiment;
[0064] Figure 6 A block diagram of the structure of the message idempotent device in one embodiment;
[0065] Figure 7 An internal structure diagram of the computer device in one embodiment. DETAILED DESCRIPTION
[0066] In order to make the purposes, technical solutions and advantages of the present application clearer, the present application is further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and do not limit the present application.
[0067] The message idempotent method provided by the embodiments of the present application can be applied in an application environment as shown in FIG. 1. Figure 1 As shown in FIG. 1, a terminal 102 communicates with a server 104 through a network. A data storage system can store data required to be processed by the server 104. The data storage system can be integrated on the server 104, or placed on a cloud or other network server. The present application can be executed by the server 104.
[0068] When the idempotent interceptor receives a message request, the server 104 obtains a specific expression corresponding to the message request; obtains a specified field from the message request according to the specific expression, and combines the specified field into an idempotent key; splices an idempotent group corresponding to the message request and the idempotent key to obtain a cache key, and queries idempotent information corresponding to the cache key; if the state of the idempotent information is complete, obtains a corresponding message processing mode; processes the idempotent information according to the message processing mode to obtain a response message. The server 104 returns the response message to the terminal 102.
[0069] The terminal 102 can be, but is not limited to, various personal computers, notebook computers, smart phones, tablet computers, Internet of Things devices, and portable wearable devices. The Internet of Things device can be a smart speaker, a smart television, a smart air conditioner, a smart vehicle-mounted device, etc. The portable wearable device can be a smart watch, a smart bracelet, a head-mounted device, etc. The server 104 can be implemented by a standalone server or a server cluster composed of multiple servers.
[0070] In one embodiment, as shown in FIG. 1, a message idempotency method is provided, which is applied to the server 104 in FIG. 1 for example, and includes the following steps. Figure 2 Figure 1 The server 104 in FIG. 1 is taken as an example for illustration, and includes the following steps.
[0071] S202, when the idempotent interceptor receives a message request, a specific expression corresponding to the message request is obtained.
[0072] The interceptor can refer to an object of a dynamic interception interface (Action) call in Java, which provides a mechanism to enable developers to execute a piece of code before and after the execution of an interface, or to prevent the execution of an interface before the execution. At the same time, it also provides a way to extract reusable parts of the code in the interface. In AOP (Aspect Oriented Programming, Aspect Oriented Programming), the interceptor is used before a method or field is accessed. The idempotent interceptor in the present application can refer to an interceptor for ensuring idempotency. The message request can refer to a message-related request sent by a terminal, for example, a transfer request, a batch transfer request, an account freezing request, an account unfreezing request, an account cancellation request, or an account opening request, etc. The specific expression can refer to an expression for obtaining a corresponding field from the message request. For example, the message request contains a message number field, an institution number field, and a transaction serial number field, and the specific expression can be "message number" or "institution number + transaction serial number" or "message number + institution number + transaction serial number", etc.
[0073] Specifically, the terminal sends a message request to the server, the server receives the message request through the idempotent interceptor, determines the interface corresponding to the message request, determines the idempotency method configuration annotation marked on the interface, and determines the specific expression corresponding to the message request according to the idempotency method configuration annotation.
[0074] The interface can refer to a window exposed by a project to receive a message processing service, which is generally distinguished by a URL (Universal Resource Locator). Each interface has its own specified message content, that is, an interface is usually responsible for receiving a type of message request. For example, a transfer request corresponds to a transfer interface, an account freezing request corresponds to an account freezing interface, an account unfreezing request corresponds to an account unfreezing interface, an account cancellation request corresponds to an account cancellation interface, and an account opening request corresponds to an account opening interface. The idempotent method configuration annotation can be used to label the interface to make the interface have an idempotent function.
[0075] In one embodiment, determining the specific expression corresponding to the message request according to the idempotent method configuration annotation includes determining the idempotent parameter specification annotation corresponding to the idempotent method configuration annotation, determining the key parameter name corresponding to the message request in the idempotent parameter specification annotation, and determining the specific expression corresponding to the message request according to the key parameter name.
[0076] The idempotent parameter specification annotation refers to an annotation used to describe the parameters in the idempotent method configuration annotation. The key parameter name refers to the parameter name corresponding to the message request specified by the idempotent parameter specification annotation. For example, the message request is a transfer request, the transfer request is a message 001, and the message 001 corresponds to the key parameter name “req”.
[0077] In one embodiment, after determining the interface corresponding to the message request, if the interface does not contain the idempotent method configuration annotation, the message request corresponding business process is directly entered, and after the business process is completed, a response message is generated and returned to the terminal.
[0078] The business process can refer to a message processing service related to the message request. For example, the message request is a transfer request, and the corresponding business process is a message processing service related to the transfer. The response message can refer to a response to the message request.
[0079] In one embodiment, after determining the idempotent method configuration annotation marked on the interface, if the interface does not contain a formal parameter, the message request corresponding business process is directly entered, and after the business process is completed, a response message is generated and returned to the terminal.
[0080] The formal parameter can be used to determine the field in the message request, and the formal parameter can be stored in a parameter list. For example, the formal parameter can be a name and a gender, and the corresponding field in the message request is “Zhang San” and “female”.
[0081] In one embodiment, before S202, the server can acquire an idempotent method configuration annotation and an idempotent parameter specification annotation; the idempotent method configuration annotation comprises a configurable specific expression, a configurable idempotent group, a configurable message processing mode, and a configurable message assembly method; the idempotent parameter specification annotation is used for marking parameters in the idempotent method configuration annotation; an idempotent interceptor is attached to the interface; the idempotent method configuration annotation is marked on the interface, and the idempotent parameter specification annotation is marked on the formal parameter of the interface.
[0082] The idempotent group can refer to a grouping of message idempotency. The message processing mode can refer to a way of message processing, and the message processing mode comprises a message snapshot mode or a message memo mode. The message snapshot mode and the message memo mode differ in response message generation manner. For example, the message snapshot mode can directly deserialization to obtain a response message. The message memo mode can obtain a response message by assembling relevant information after querying.
[0083] S204, acquiring specified fields from the message request according to the specific expression, and combining the specified fields into an idempotent key.
[0084] The specified field can refer to a field in the message request used for combination into an idempotent key. The idempotent key can refer to a field generated according to the specified field. For example, when the message request is a transfer request, the specified fields can be institution number: 1234 and transaction serial number: 999, and the idempotent key is 1234999.
[0085] Specifically, the server can first determine all fields in the message request, filter all fields in the message request according to the specific expression, obtain the specified fields, and combine the specified fields into the idempotent key.
[0086] S206, splicing the idempotent group corresponding to the message request and the idempotent key to obtain a cache key, and querying idempotent information corresponding to the cache key.
[0087] The cache key can refer to a field generated based on the idempotent group and the idempotent key. For example, when the message request is a transfer request, the interface corresponding to the transfer request is a transfer interface, the idempotent group of the transfer interface is transfer, and the idempotent key of the transfer request is 1234999, the cache key can be "idempotent group + idempotent key": transfer1234999. The idempotent information refers to information used to obtain a response message, and the idempotent information has idempotency.
[0088] Specifically, the server can first acquire the idempotent group corresponding to the message request, then splice the idempotent group and the idempotent key to obtain the cache key, and query the idempotent information corresponding to the cache key in the cache component.
[0089] S208, if the status of the idempotent information is complete, the corresponding message processing mode is acquired.
[0090] The status of the idempotent information includes interruption and completion. The interruption indicates that the business process corresponding to the idempotent information has an exception in the execution process. The completion indicates that the execution process of the business process corresponding to the idempotent information is normal.
[0091] For example, the business process is "transfer service", if the network is abnormal when the transfer is first performed, the transfer service is interrupted, and the status of the generated idempotent information is interruption, and the idempotent information contains the information of the network exception. When the transaction is idempotently requested, and the idempotent response needs to be consistent with the original request, the corresponding idempotent information (containing the network exception information) is acquired, so as to prompt the calling party that the transaction is not successful when the first transfer is performed, and the reason is network exception. If the balance is insufficient during the transfer process, the transfer fails and no exception occurs, or the transfer is successfully completed, the status of the generated idempotent information is completion.
[0092] Specifically, the server can judge the status of the idempotent information, if the status of the idempotent information is complete, the corresponding message processing mode is acquired.
[0093] In one embodiment, acquiring the corresponding message processing mode includes that the server can determine the interface corresponding to the message request, and acquire the message processing mode from the idempotent method configuration annotation of the interface.
[0094] In one embodiment, if the status of the idempotent information is interruption, an exception prompt information is generated according to the idempotent information, and the message idempotent process is ended.
[0095] The exception prompt information can be information for prompting an exception. The message idempotent process can be a message request corresponding idempotent process.
[0096] S210, the idempotent information is processed according to the message processing mode, and a response message is obtained.
[0097] Specifically, if the message processing mode is the message snapshot mode, the idempotent information is the serialized response message, the idempotent information is deserialized to obtain the response message; if the message processing mode is the message memo mode, the idempotent information is not the serialized response message, and a message assembly method in the idempotent method configuration annotation is called according to the idempotent information. The message assembly method has idempotency, and the response field is assembled by the message assembly method to obtain the response message. The response field can be a field used to generate a response message, which can be queried from a persistent data source (such as a database) via an idempotent key.
[0098] In the message idempotent method, when the idempotent interceptor receives a message request, a specific expression corresponding to the message request is obtained; a specified field is obtained from the message request according to the specific expression, and the specified field is combined into an idempotent key; an idempotent group corresponding to the message request is spliced with the idempotent key to obtain a cache key, which realizes accurate query of the idempotent information corresponding to the cache key, that is, the accuracy of the message idempotent is ensured, and if the state of the idempotent information is complete, a corresponding message processing mode is obtained; the idempotent information is processed according to the message processing mode to obtain a response message, the corresponding message processing mode is obtained according to the idempotent information, the problem of single mode of the message idempotent is solved, dynamic message idempotent judgment is realized, the application scene of the message idempotent is effectively expanded, and the flexibility of the message idempotent is improved.
[0099] In one embodiment, as shown in Figure 3 the generating idempotent information step includes:
[0100] S302, if the idempotent information corresponding to the cache key is not queried, the message request is locked based on the cache key.
[0101] The lock processing is used to prevent the same business process from being repeated, for example, the lock in the lock processing includes but is not limited to a distributed lock.
[0102] In one embodiment, the lock processing of the message request based on the cache key includes using the cache key as a lock flag bit, and performing distributed lock processing on the message request according to the lock flag bit. The lock flag bit is used to mark the locked message request.
[0103] In one embodiment, if the lock is abnormal, that is, the lock is not successful and not failed, the business process corresponding to the message request is directly entered, and after the business process is completed, a response message is obtained, and the response message is returned to the terminal.
[0104] S304, if the lock is successful, the business process corresponding to the message request is entered.
[0105] The business process can refer to a related process for responding to a message request.
[0106] In one embodiment, entering the business process corresponding to the message request includes entering a transfer account business process corresponding to a transfer account request; or entering a batch transfer account business process corresponding to a batch transfer account request; or entering an account freezing business process corresponding to an account freezing request; or entering an account unfreezing business process corresponding to an account unfreezing request; or entering an account cancellation business process corresponding to an account cancellation request; or entering an account opening business process corresponding to an account opening request.
[0107] In one embodiment, if the locking fails, a waiting polling process is entered; the idempotent information corresponding to the cache key is queried, including: if the current request waiting quantity is greater than a critical waiting threshold, setting the maximum polling number as a first preset value; and querying the idempotent information according to the maximum polling number; if the current request waiting quantity is not greater than the critical waiting threshold, setting the maximum polling number as a second preset value, and querying the idempotent information at intervals according to a sleep time.
[0108] The waiting polling process refers to a process for querying the idempotent information. The current request waiting quantity can refer to the number of currently waiting message requests. The critical waiting threshold can be used to measure the current request waiting quantity. For example, the critical waiting threshold can be 1, 2, 3, etc., which can be set as needed according to specific circumstances. The maximum polling number can refer to the upper limit number of querying the idempotent information. For example, the maximum polling number can be 1, 2, 3, etc., which can be set as needed according to specific circumstances. The first preset value can refer to the maximum polling number under the shortest waiting time. For example, the first preset value can be 1, 2, 3, etc., which can be set as needed according to specific circumstances. The second preset value can refer to a pre-set value. The first preset value and the second preset value are different preset values. For example, the second preset value can be 1, 2, 3, etc., which can be set as needed according to specific circumstances. The sleep time can refer to the time for pausing the querying of the idempotent information during the waiting polling process. For example, the sleep time can be 1ms, 1s, 3s, etc., which can be set as needed according to specific circumstances.
[0109] In one embodiment, the idempotent information further includes exception idempotent information. If the business process is interrupted abnormally, the exception information is obtained; the exception idempotent information is generated according to the exception information, and the state of the exception idempotent information is set to interrupted; and a second mapping relationship between the cache key and the exception idempotent information is established.
[0110] The exception idempotent information can refer to the idempotent information generated if the business process is interrupted abnormally. The exception information can refer to the information generated according to the interrupted business process. The second mapping relationship can refer to the mapping relationship established between the cache key and the exception idempotent information.
[0111] In one embodiment, querying the idempotent information corresponding to the cache key includes querying the exception idempotent information corresponding to the cache key according to the second mapping relationship.
[0112] S306, after the completion of the business process, the idempotent information is generated according to the corresponding message processing mode, and the state of the idempotent information is set to completed.
[0113] The idempotent information can be the first idempotent information or the second idempotent information.
[0114] In one embodiment, the idempotent information is generated according to the corresponding message processing mode, and a state of the idempotent information is set to complete, including: if the message processing mode is the message snapshot mode, generating first idempotent information; the first idempotent information includes a serialized response message; if the message processing mode is the message memo mode, generating second idempotent information; setting the state of the first idempotent information or the second idempotent information to complete, and establishing a first mapping relationship between the cache key and the first idempotent information or the second idempotent information.
[0115] The idempotent information can be the first idempotent information or the second idempotent information. The first idempotent information can refer to idempotent information generated under the message processing mode of the message snapshot mode. The second idempotent information can refer to idempotent information generated under the message processing mode of the message memo mode. The first mapping relationship can refer to a mapping relationship between the cache key and the first idempotent information or the second idempotent information.
[0116] In one embodiment, querying the idempotent information corresponding to the cache key includes querying the first idempotent information or the second idempotent information corresponding to the cache key according to the first mapping relationship.
[0117] In this embodiment, if the idempotent information corresponding to the cache key is not queried, the message request is locked based on the cache key, if the locking is successful, the business process corresponding to the message request is entered, and after the business process is completed, the idempotent information is generated according to the corresponding message processing mode, and the state of the idempotent information is set to complete. By using the lock to enter the corresponding business process, the repeated message processing caused by the repeated message arriving at the same time is effectively prevented.
[0118] As an example, the embodiment is as follows:
[0119] The idea of this embodiment is: a message idempotent solution supporting multiple strategies and being configurable, by using a specified annotation, an idempotent interceptor is attached to a specified interface, thereby realizing non-intrusive addition of idempotent function. By using a method-level annotation and a SPEL expression, when a specific transaction message is received in running, customized judgment standard and idempotent processing flow can be executed. By using a distributed lock on a transaction unique field, the concurrent scenario protection of repeated messages is realized. Figure 4a For another embodiment, a flowchart of a message idempotent method is shown in FIG. 2; Figure 4b For another embodiment, a flowchart of a message idempotent method is shown in FIG. 2; Figure 4a And as shown in FIG. 2, the specific implementation steps are as follows: Figure 4b
[0120] Step 1: Add idempotent related configuration file content, define idempotent interceptor and its related classes, define three idempotent related annotation classes, and add corresponding annotations on interfaces that need to add idempotency, and add message assembly methods as needed.
[0121] Step 2: Load idempotent configuration at project startup, and decide whether to initialize idempotent related instances and whether to load idempotent interceptor into the interceptor chain of the controller with the specified annotation according to the idempotent enable flag.
[0122] Step 3: At runtime, if a transaction with the specified annotation receives a transaction message in the first step, it will enter the idempotent interceptor we defined before processing the specific transaction. In this interceptor, the corresponding value of the idempotent key will be taken out from the request message according to the field configured by the annotation, and the cache component will be queried according to this value to see if there is an idempotent record: if there is an idempotent record, the transaction will be processed according to the idempotent policy configured by the annotation, and the idempotent response message will be sent; otherwise, a distributed lock will be added, and the business processing flow will be entered. After the business processing is completed, the idempotent key-idempotent information value key-value pair will be stored in the cache component according to the configured idempotent policy. In the case where the distributed lock is not acquired, it is considered that there is a transaction in progress, and the transaction result is polled before the timeout, and the transaction result is unknown if the timeout occurs.
[0123] Step 1 includes the following steps:
[0124] Step 1.1: In the project configuration file, add the flag configuration to enable idempotency and the configuration of the idempotent information cache duration, and add the idempotent interceptor, idempotent information model class, idempotent information processor, idempotent cache manager, and idempotent configuration class in the code. The idempotent configuration class is responsible for reading related configurations and initializing the idempotent interceptor, idempotent information processor, and idempotent cache manager. The internal processing flow of the idempotent interceptor and its related processing classes is described in detail in Step 3.
[0125] Step 1.2: Define three annotations: ① IdempProxy, idempotent proxy annotation; ② IdemApi, idempotent method configuration annotation; ③ IdemParam, idempotent parameter specification annotation. Among them: the idempotent proxy annotation is a class-level annotation without parameters, and is used to mark the controller of the transaction in this scenario. The class marked by this annotation will be attached with the idempotent interceptor through dynamic proxy when instantiated;
[0126] The idempotent method configuration annotation is a method-level annotation marked on an interface that needs to be added with idempotency. When the interface marked with this annotation receives a request, the request message will be processed by the idempotent interceptor first. The parameter list includes: ① Idempotent strategy (i.e. message processing mode): there are two modes, message snapshot mode and message memo mode. The message snapshot mode directly caches the complete transaction response message in the cache component, while the message memo mode only caches the key information, which needs to be specified in the subsequent parameters; ② Message assembly method: if the idempotent strategy is the message snapshot mode, it does not need to be defined, if it is the message memo mode, a class instance method needs to be specified as the idempotent response assembly method through a string. This method needs to have the same access permission modifier, parameter list, return value, and exception declaration as the actual business interface; ③ Idempotent group: different transactions should be grouped for idempotent judgment to prevent interference with each other. Only when the idempotent group and the idempotent key are the same, it will be identified as having an idempotent record; ④ Idempotent key: specify the basis for determining whether a transaction has an idempotent record in the idempotent group. It should have the ability to uniquely distinguish a transaction within the idempotent group. It supports SPEL expressions, and it is generally recommended to take a combination of several fields of the request as a unique key.
[0127] The idempotent parameter specification annotation is an annotation for method parameters. It is used in conjunction with the parameters (idempotent key) in the idempotent method configuration annotation to specify the mapping of the interface parameter name in the SPEL expression, preventing value offset problems in SPEL expressions in the case of multiple parameters and code confusion. The parameter has only one parameter name.
[0128] Step 1.3: According to the annotation rules defined in step 1.2, mark the corresponding annotations on the class, method, and parameter of the interface that needs to be added with idempotency. If the message assembly method is filled in the idempotent method configuration annotation, a new method needs to be added.
[0129] The configuration examples of the idempotent method configuration annotation and the idempotent parameter specification annotation are shown in the following figure. The idempotent strategy of the example is the message memo mode.
[0130] Step 3 includes the following steps:
[0131] Step 3.1: When the interceptor receives a request, it first judges whether the interface that receives the request has an idempotent method configuration annotation. If not, it means that the method does not need idempotency, and it directly enters the business process and responds, ending the process. If it does, it proceeds to the next step.
[0132] Step 3.2: Determine the parameter list of this method. If there is no parameter list, the idempotent function cannot be executed, and the business process is directly entered and responded to, ending this process. If there is a parameter list, read the parameter name of the idempotent parameter specified annotation, construct the container, use the SPEL expression (specific expression) to obtain the specified field of the request, and combine it into an idempotent key. The idempotent group in the idempotent method configuration annotation and the above idempotent key are spliced into a cache key, and the idempotent information processor is called to enter step 3.3.
[0133] Step 3.3: The idempotent information processor calls the idempotent cache manager to query whether the above cache key has corresponding idempotent information. If so, directly enter step 3.6, otherwise try to add a distributed lock to the cache key. If the lock is abnormal, directly enter the business process and respond, ending this process. If the lock is successful, enter step 3.4; if the lock fails, enter step 3.5.
[0134] Step 3.4: Enter the business process. If the business process is completed, according to the strategy of the idempotent method configuration annotation, if it is a message snapshot mode, create idempotent information containing the serialized string generated by the actual response message; if it is a message memo mode, generate idempotent information with only a flag bit. Then set the state in the idempotent information to complete, store it in the cache component through the idempotent cache manager with the cache key-idempotent information key-value pair created in step 3.2, and the effective period follows the idempotent duration configuration loaded in step 2, and end this process. If the business process is interrupted abnormally, construct the idempotent information according to the exception information, set the state in the idempotent information to interrupt, and also store it through the idempotent cache manager, with the cache key-idempotent information key-value pair created in step 3.2 stored in the cache component, the effective period follows the idempotent duration configuration loaded in step 2, and finally throw the exception of the business process, ending this process.
[0135] Step 3.5: Locking fails, indicating that the distributed lock already exists, i.e., the same message is already in the business process processing, at which point the waiting polling process is entered, the global waiting count attribute (i.e., the current request waiting number) is first atomically incremented by 1 and the value is obtained, then a polling count variable is initialized, if the global waiting count attribute is greater than the maximum waiting value (i.e., the key waiting threshold), the function is downgraded, the maximum polling number is set to 1, at this time the process will exit the polling after querying the idempotent information once (i.e., the first preset value is 1), if the idempotent information is not found, the transaction result is unknown and the process is ended, if the idempotent information is found, step 3.6 is entered; if the global waiting count attribute is not greater than the maximum waiting value, the process will be polled after a certain sleep time, until the idempotent information is found or the polling number reaches the maximum polling number, if the idempotent information is not found at this time, the transaction result is unknown and the process is ended, if the idempotent information is found, step 3.6 is entered. After the above steps are completed, the global waiting count attribute is decremented by 1.
[0136] Step 3.6: Check the state in the obtained idempotent information:
[0137] If the state is complete, distinguish the idempotent strategy processing, if it is the message snapshot mode, directly deserialize the message string to obtain the response message and return, ending the process; if it is the message memo mode, call the message assembly method specified in the idempotent method configuration annotation through reflection to obtain the response message and return, ending the process; if the state is interrupted, construct an exception to restore the business process according to the idempotent information and throw it, ending the process.
[0138] In this embodiment, the general idempotent processing rules are encapsulated by using the characteristics of AOP aspect-oriented programming, and non-invasive function addition to business code is realized; at the same time, by using annotations, interface-level idempotent configuration can be realized, the idempotent strategy of the current interface can be flexibly configured as the message memo mode or the message snapshot mode, the idempotent request judgment field, the idempotent grouping, and the message assembly method in the message memo mode, and dynamic transaction idempotent judgment at runtime is realized; the idempotent information is temporarily stored through the cache component, which ensures that the efficiency of the transaction is not greatly affected; finally, by using the distributed lock, the repeated message processing caused by the repeated message arriving at the same time in the extreme case is prevented.
[0139] It should be understood that although the steps in the flowcharts involved in the above embodiments are shown in sequence according to the arrows, the steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified herein, the execution of the steps is not strictly limited in sequence, and the steps can be executed in other orders. Moreover, at least some of the steps in the flowcharts involved in the above embodiments can include multiple steps or multiple stages, which are not necessarily executed at the same time, but can be executed at different times, and the execution order of the steps or stages is not necessarily sequential, but can be alternately executed with other steps or steps or stages in at least part of other steps.
[0140] Based on the same inventive concept, the embodiments of the present application also provide a message idempotent device for implementing the message idempotent method described above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme described in the above method, so the specific limitations in one or more message idempotent device embodiments provided below can refer to the limitations of the message idempotent method in the above, which will not be repeated here.
[0141] In one embodiment, as shown in Figure 5 A message idempotent device is provided, comprising: a first acquisition module 502, an acquisition and combination module 504, a splicing and query module 506, a second acquisition module 508, and an information processing module 510, wherein:
[0142] The first acquisition module 502 is configured to acquire a specific expression corresponding to a message request when an idempotent interceptor receives the message request.
[0143] The acquisition and combination module 504 is configured to acquire a specified field from the message request according to the specific expression, and combine the specified field into an idempotent key.
[0144] The splicing and query module 506 is configured to splice the idempotent group corresponding to the message request and the idempotent key to obtain a cache key, and query the idempotent information corresponding to the cache key.
[0145] The second acquisition module 508 is configured to acquire a corresponding message processing mode if the state of the idempotent information is complete.
[0146] The information processing module 510 is configured to process the idempotent information according to the message processing mode to obtain a response message.
[0147] In one embodiment, the splicing and query module 506 is further configured to query the first idempotent information or the second idempotent information corresponding to the cache key according to the first mapping relationship.
[0148] In an embodiment, the splicing and querying module 506 is further configured to enter a waiting polling process if the locking fails; set the maximum polling number to a first preset value if the current request waiting number is greater than a key waiting threshold; and query the idempotent information according to the maximum polling number; set the maximum polling number to a second preset value if the current request waiting number is not greater than the key waiting threshold, and query the idempotent information at intervals according to the sleep time.
[0149] In an embodiment, the splicing and querying module 506 is further configured to query the abnormal idempotent information corresponding to the cache key according to the second mapping relationship.
[0150] In an embodiment, as shown in FIG. 6, the message idempotent device further includes: Figure 6
[0151] The configuration module 512 is configured to obtain an idempotent method configuration annotation and an idempotent parameter specification annotation; the idempotent method configuration annotation includes a configurable specific expression, a configurable idempotent group, a configurable message processing mode, and a configurable message assembly method; the idempotent parameter specification annotation is used for marking the parameters in the idempotent method configuration annotation; the idempotent interceptor is attached to the interface; the idempotent method configuration annotation is marked on the interface, and the idempotent parameter specification annotation is marked on the formal parameter of the interface.
[0152] The generation module 514 is configured to perform a locking process on the message request based on the cache key if the idempotent information corresponding to the cache key is not queried; enter a business process corresponding to the message request if the locking is successful; generate idempotent information according to the corresponding message processing mode after the business process is completed, and set the state of the idempotent information to completed.
[0153] The interruption processing module 516 is configured to obtain abnormal information if the business process is interrupted abnormally; generate abnormal idempotent information according to the abnormal information, and set the state of the abnormal idempotent information to interrupted; and establish a second mapping relationship between the cache key and the abnormal idempotent information.
[0154] In an embodiment, the idempotent information is first idempotent information or second idempotent information; the generation module 514 is further configured to: generate the first idempotent information if the message processing mode is a message snapshot mode; the first idempotent information includes a serialized response message; generate the second idempotent information if the message processing mode is a message memorandum mode; set the state of the first idempotent information or the second idempotent information to completed, and establish a first mapping relationship between the cache key and the first idempotent information or the second idempotent information.
[0155] In an embodiment, the interruption processing module 516 is further configured to generate abnormal prompt information according to the idempotent information if the state of the idempotent information is interrupted, and end the message idempotent process.
[0156] The modules in the message idempotent device can be implemented by software, hardware, or a combination thereof. The modules can be embedded in or independent of a processor in a computer device in hardware form, or stored in a memory in a computer device in software form, so as to be called and executed by a processor to perform operations corresponding to the modules.
[0157] In one embodiment, a computer device is provided, which can be a server, and an internal structure diagram of the computer device can be as shown in FIG. 1. Figure 7 The computer device includes a processor, a memory, an input / output interface, and a communication interface. The processor, the memory, and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. The processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program, and a database. The internal memory provides an environment for running the operating system and the computer program in the non-volatile storage medium. The database of the computer device is configured to store message idempotent data. The input / output interface of the computer device is configured to exchange information between the processor and external devices. The communication interface of the computer device is configured to communicate with terminals outside through a network connection. The computer program is executed by the processor to implement a message idempotent method.
[0158] Those skilled in the art can understand that Figure 7 The structure shown in FIG. 1 is only a block diagram of part of the structure related to the scheme of the present application, and does not constitute a limitation on the computer device to which the scheme of the present application is applied. A specific computer device can include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0159] In one embodiment, a computer device is provided, which includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the above embodiments.
[0160] In one embodiment, a computer readable storage medium is provided, which stores a computer program. The computer program is executed by a processor to implement the above embodiments.
[0161] In one embodiment, a computer program product is provided, which includes a computer program. The computer program is executed by a processor to implement the above embodiments.
[0162] It should be noted that the user information (including but not limited to user equipment information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in the present application are all information and data authorized by the user or authorized by all parties, and the collection, use and processing of related data need to comply with relevant laws, regulations and standards of the country and region.
[0163] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing related hardware through a computer program. The computer program can be stored in a non-volatile computer readable storage medium. When the computer program is executed, it can include the processes of the above-mentioned embodiments of each method. Any reference to a memory, database or other medium used in the embodiments provided by the present application can include at least one of a non-volatile and volatile memory. The non-volatile memory can include a read-only memory (ROM), a magnetic tape, a floppy disk, a flash memory, an optical storage, a high-density embedded non-volatile memory, a resistive memory (ReRAM), a magnetoresistive random access memory (MRAM), a ferroelectric memory (FRAM), a phase change memory (PCM), a graphene memory, etc. The volatile memory can include a random access memory (RAM) or an external cache memory, etc. As an illustration but not limitation, the RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc. The database involved in the embodiments provided by the present application can include at least one of a relational database and a non-relational database. The non-relational database can include a distributed database based on a block chain, etc., without being limited thereto. The processor involved in the embodiments provided by the present application can be a general-purpose processor, a central processing unit, a graphics processing unit, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., without being limited thereto.
[0164] The technical features of the above embodiments can be combined arbitrarily. In order to make the description concise, all possible combinations of the technical features in the above embodiments are not described, however, as long as the combinations of the technical features do not exist contradictory, they should be considered as the scope of the present disclosure.
[0165] The above embodiments only express several implementation ways of the present application, and the description is specific and detailed, but it should not be understood as a limitation to the patent scope of the present application. It should be pointed out that for ordinary skilled in the art, without departing from the concept of the present application, several modifications and improvements can be made, which all belong to the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the appended claims.
Claims
1. A message idempotency method, characterized in that, The method includes: When the idempotent interceptor receives a message request, it obtains the specific expression corresponding to the message request; Obtain a specified field from the message request based on the specific expression, and combine the specified field into an idempotent key; The idempotency group corresponding to the message request is concatenated with the idempotency key to obtain the cache key, and the idempotency information corresponding to the cache key is queried. If idempotent information corresponding to the cache key is found, and the state of the idempotent information is complete, then the corresponding message processing mode is obtained; the idempotent information is processed according to the message processing mode to obtain a response message; the message processing mode is either message snapshot mode or message memo mode. If no idempotent information corresponding to the cache key is found, the message request is locked based on the cache key; if locking fails, a waiting polling process is initiated; wherein the waiting polling process includes: atomically incrementing the current request waiting count; if the current request waiting count is greater than a critical waiting threshold, a function degradation is performed, setting the maximum polling count to a first preset value, and querying the idempotent information based on the maximum polling count; if the current request waiting count is not greater than the critical waiting threshold, setting the maximum polling count to a second preset value, and polling the idempotent information based on the sleep time interval until idempotent information is found or the maximum polling count is reached; wherein the first preset value is the maximum polling count under the shortest waiting time, and the second preset value is a pre-set value; After completing the waiting polling process, the current number of requests waiting is decremented.
2. The method according to claim 1, characterized in that, The method further includes: Obtain idempotent method configuration annotations and idempotent parameter specification annotations; the idempotent method configuration annotations include configurable specific expressions, configurable idempotent groups, and configurable message processing modes; the idempotent parameter specification annotations are used to mark the parameters in the idempotent method configuration annotations; Attach the idempotent interceptor to the interface; Mark the interface with the idempotent method configuration annotation, and mark the formal parameters of the interface with the idempotent parameter specification annotation.
3. The method according to claim 1, characterized in that, The method further includes: If the locking is successful, the business process corresponding to the message request will proceed. After the business process is completed, the idempotency information is generated according to the corresponding message processing mode, and the status of the idempotency information is set to complete.
4. The method according to claim 3, characterized in that, The idempotency information is either first idempotency information or second idempotency information; the step of generating the idempotency information according to the corresponding message processing mode and setting the state of the idempotency information to complete includes: If the message processing mode is message snapshot mode, then the first idempotent information is generated; the first idempotent information includes a serialized response message; If the message processing mode is message memo mode, then the second idempotent information is generated; Set the state of the first idempotent information or the second idempotent information to complete, and establish a first mapping relationship between the cache key and the first idempotent information or the second idempotent information; The idempotency information corresponding to the cache key includes: Based on the first mapping relationship, query the first idempotent information or the second idempotent information corresponding to the cache key.
5. The method according to claim 3, characterized in that, The business process corresponding to the message request includes: Enter the transfer process corresponding to the transfer request; or, Proceed to the batch transfer business process corresponding to the batch transfer request; or... Enter the account freezing business process corresponding to the account freezing request; or, Proceed to the account unfreezing process corresponding to the account unfreezing request; or... Enter the account cancellation process corresponding to the account cancellation request; or, Enter the account opening process corresponding to the account opening request.
6. The method according to claim 3, characterized in that, The idempotency information also includes abnormal idempotency information, and the method further includes: If the business process is abnormally interrupted, obtain the abnormal information; The abnormal idempotency information is generated based on the abnormal information, and the state of the abnormal idempotency information is set to interrupt. Establish a second mapping relationship between the cache key and the abnormal idempotency information; The idempotency information corresponding to the cache key includes: Based on the second mapping relationship, query the abnormal idempotency information corresponding to the cache key.
7. The method according to claim 1, characterized in that, The method further includes: If the state of the idempotency information is interrupted, an error message is generated based on the idempotency information, and the message idempotency process ends.
8. A message idempotent device, characterized in that, The device includes: The first acquisition module is used to acquire a specific expression corresponding to the message request when the idempotent interceptor receives the message request; The acquisition and combination module is used to acquire a specified field from the message request based on the specific expression, and combine the specified field into an idempotent key; The concatenation and query module is used to concatenate the idempotency group corresponding to the message request with the idempotency key to obtain the cache key, and query the idempotency information corresponding to the cache key; The second acquisition module is used to acquire the corresponding message processing mode if idempotent information corresponding to the cache key is found and the state of the idempotent information is complete. The information processing module is used to process the idempotent information according to the message processing mode to obtain a response message; The generation module is used to lock the message request based on the cache key if no idempotent information corresponding to the cache key is found. The concatenation and query module is used to initiate a waiting and polling process if locking fails. This waiting and polling process includes: atomically incrementing the current request waiting count; if the current request waiting count exceeds a critical waiting threshold, performing a function degradation by setting the maximum polling count to a first preset value and querying the idempotency information based on the maximum polling count; if the current request waiting count is not greater than the critical waiting threshold, setting the maximum polling count to a second preset value and polling the idempotency information based on the sleep time interval until idempotency information is found or the maximum polling count is reached; wherein the first preset value is the maximum polling count with the shortest possible waiting time, and the second preset value is a pre-set value; after completing the waiting and polling process, the current request waiting count is decremented.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.
11. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Idempotent implementation method, device, equipment and medium
CN113064584A