Disclosure of Invention
The invention aims to provide a method and a system for realizing cross-library operation consistency based on redis.
The technical solution for realizing the purpose of the invention is as follows: a method for realizing cross-library operation consistency based on redis comprises the following steps:
generating a global unique feature code;
the calling party calls an interface, and the calling content comprises service parameters and feature codes;
if the server side does not finish execution within the set time, the server side is in an overtime state, and the Fang Huigun business of this time is called;
if the execution of the server is finished and a return result is prepared, performing first state judgment on the calling party, and executing corresponding operation according to a judgment result, wherein the judgment result comprises a first critical state and a first non-overtime state;
the first critical state means that the time for the server to finish execution and prepare for returning a result is exactly the same as the deadline time set by the caller;
the first non-overtime state means that the time for the server to finish execution and prepare to return a result is less than the deadline set by the caller.
Preferably, when the determination result is the first critical state, the operations executed are:
step A1, judging whether the Setnx operation performed by the server side by using redis is successful: if successful, executing step A2, and if failed, executing step A3;
step A2, the server side normally executes the return operation, the caller judges whether the service succeeds or fails by acquiring the value of the redis key as the feature code, if the service succeeds, the code is not rolled back, and if the service fails, the code is rolled back; then deleting key value pairs with keys as feature codes in the redis;
and step A3, the server side directly rolls back the code, deletes the key value pair with the key as the feature code in the redis, and calls the server side directly to roll back the code.
When the judgment result is the first non-overtime state, the executed operation is as follows:
the calling party uses redis to carry out Setnx operation, key is set as a feature code, the state of value is judged, a corresponding interface calling state code is returned according to the state of value, then the calling party is subjected to second state judgment, corresponding operation is carried out according to the judgment result, the judgment result comprises a second critical state and a second non-overtime state, and the state of value is a success state or a failure state. The second critical state means that the time for the server to finish execution and prepare for returning a result during the calling is exactly the same as the deadline time set by the calling party; the second non-overtime state is that the time for the server to finish execution and prepare to return a result in the calling does not reach the deadline set by the calling party.
Preferably, when the value state is a success state, the caller performs the second state judgment and performs the following operations according to the judgment result:
if the calling party is in the second non-overtime state, the calling party obtains the success of the interface return state, the service is completed, the code is not rolled back, and the key value pair with the key as the feature code is deleted in the redis;
if the key value pair is in the second critical state, the calling party uses redis to perform Setnx operation, sets the key as the feature code, acquires the state value in the redis, and at the moment, the state is a successful state, completes the service, does not rollback the code, and deletes the key as the key value pair of the feature code in the redis.
Preferably, when the value state is a failure state, the caller performs the second state judgment and performs the following operations according to the judgment result:
if the calling party is in the second non-overtime state, the calling party fails to obtain the interface return state, the service is rolled back, and the key value pair with the key as the feature code is deleted in the redis;
if the key value is in the second critical state, the calling party uses redis to carry out Setnx operation, the key is set as the feature code, the state value in the redis is obtained, the state value is in a failure state, namely, the code is rolled back, and the key value pair with the key as the feature code is deleted in the redis.
Compared with the prior art, the invention has the following remarkable advantages: 1) The invention can ensure the data consistency in the cross-library interface calling process (non-query operation such as adding, modifying or deleting and the like for cross-library interface calling); 2) According to the invention, under the condition that the Redis is adopted to store the cross-library operation, the caller does not need to completely rely on the return value of the server, the service of the server can be ensured to be consistent with the caller by setting the Redis state, even if the time-out condition occurs, the consistency of the service of the caller and the service of the server can be ensured according to the final state in the Redis, and thus the final consistency of the data after the cross-library operation is ensured; 3) The method of the invention can greatly reduce the complexity of the calling party service.
The present invention is described in further detail below with reference to the attached drawing figures.
Detailed Description
With reference to the accompanying drawings, the method for realizing cross-library operation consistency based on redis comprises the following steps:
generating a global unique feature code, wherein the global unique feature code comprises a system parameter, a time parameter and a random number;
the calling party calls an interface, and the calling content comprises service parameters and feature codes;
if the execution of the server is not completed within the set time, the server is in an overtime state, and the Fang Huigun business is called;
if the execution of the service party is finished and a return result is prepared, performing first state judgment on the calling party, and executing corresponding operation according to a judgment result, wherein the judgment result comprises a first critical state and a first non-overtime state;
the first critical state means that the time for the server to finish execution and prepare for returning a result is exactly the same as the deadline time set by the caller;
the first non-overtime state means that the time for the server to finish execution and prepare to return a result is less than the deadline set by the caller.
When the judgment result is the first critical state, the executed operations are as follows:
step A1, judging whether the Setnx operation performed by the server side by using redis is successful: if the result is successful, returning the value to be 1 and executing the step A2, and if the result is failed, returning the value to be 0 and executing the step A3;
step A2, the server side normally executes the return operation, the state code is determined according to the success or failure of the service, setnx of the caller is 0, the caller judges whether the service finally succeeds or fails by acquiring the value of the rediskey as the feature code, if so, the code is not rolled back, and if not, the code is rolled back; then deleting key value pairs with keys as feature codes in the redis;
and step A3, the server side directly rolls back the code, deletes the key value pair with the key as the feature code in the redis, and directly rolls back the code when the setnx of the calling side is 1.
When the judgment result is the first non-overtime state, the executed operation is as follows:
the caller uses redis to carry out Setnx operation, sets key as a feature code, and judges the state of value:
if the value state is the 'success' state, the command is returned to 1, and then the interface calling state code is returned to 'success'; then executing a second critical state judgment step; the critical state judgment specifically judges the critical state of the interface at a set calling time point:
in the second non-overtime state, the calling party successfully obtains the interface return state, completes the service, does not rollback the operation code, and deletes the key value pair with the key as the feature code in the redis;
in the second critical state, the interface just reaches the overtime, the calling party Fang Weijie receives the returned 'success' status code, the calling party uses redis to perform Setnx operation, the key is set as the feature code, the value is the termination state, the command is returned to 0, the status value in the redis obtained, the status code is 'success', the service is completed, the operation code is not rolled back, and the key value pair with the key as the feature code is deleted in the redis. When the value status is a success status, the content in the status code may also be set to "Yes", "Y", or "1", etc.
If the value state is the 'failure' state, the command is returned to 1, and then the interface calling state code is returned to indicate 'failure'; and then executing a second critical state judgment step. The critical state judgment specifically judges the return state of the interface at the set calling time point:
in the second non-overtime state, the calling party fails to obtain the interface return state, the service is rolled back, and the key value pair with the key as the feature code is deleted in the redis;
in the second critical state, the interface just reaches the timeout time, the calling party Fang Weijie receives the status code returning 'failure', the calling party uses redis to perform Setnx operation, key is the feature code, value is the termination state, the command returns to 0, the status value in redis obtained, at this time, the status value is 'failure', namely, the service is rolled back, and the key value pair with key as the feature code is deleted in the redis. When the value state is a fail state, the contents in the state code may also be set to "No", "N", or "0", etc.
The invention discloses a system for realizing cross-library operation consistency based on redis, which comprises the following modules:
a global unique feature code generation module;
a calling party interface calling module;
the service overtime judging module is used for judging whether the execution of the service party is finished within the set time;
and the calling critical state judgment module is used for judging the calling critical state of the calling party when the execution of the service party is finished and a return result is prepared, and executing corresponding steps according to the judgment result.
A computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, the processor implementing the following steps when executing the computer program:
generating a global unique feature code;
the calling party calls an interface;
if the server side does not finish execution within the set time, the server side is in an overtime state, and the Fang Huigun business of this time is called;
if the execution of the server is finished and a return result is prepared, carrying out state judgment on the calling party, and executing corresponding operation according to a judgment result, wherein the judgment result comprises a critical state and a non-overtime state;
the critical state means that the time for the server to finish execution and prepare for returning the result is exactly the same as the deadline time set by the caller;
the non-overtime state refers to that the time for the server to finish execution and prepare to return a result is less than the deadline set by the calling party.
A computer-readable storage medium, on which a computer program is stored which, when executed by a processor, carries out the steps of:
generating a global unique feature code;
the calling party calls an interface;
if the server side does not finish execution within the set time, the server side is in an overtime state, and the Fang Huigun business of this time is called;
if the execution of the server is finished and a return result is prepared, carrying out state judgment on the calling party, and executing corresponding operation according to a judgment result, wherein the judgment result comprises a critical state and a non-overtime state;
the critical state means that the time for the server to finish execution and prepare for returning the result is exactly the same as the deadline time set by the caller;
the non-overtime state refers to that the time for the server to finish execution and prepare to return a result is less than the deadline set by the caller.
The present invention is described in further detail below with reference to examples:
the cross-library calling of the invention mainly utilizes an HTTP interface to access the scene, does not care about the specific database types of a calling party and a service party, and takes a transaction type database as a main part.
Example 1
The service side successfully returns to the scene by cross-library calling:
s1: the globally unique feature code is generated through a tool class, and is generally composed of system parameters, time parameters and random number encryption.
S2: when the calling party calls the direct interface, the feature code is added besides the service parameter.
S3: when the server normally returns successfully, redis Setnx is used, key is used as a feature code, value is a success state, the command returns to 1, and then the interface calling state code is returned to be successful.
S4: if the current redis operation is abnormal, the codes are not rolled back, and the codes are recorded in a maintenance thread and cleaned regularly.
Critical scenario: if the interface just times out and no return success status code is received in s4, s4 will execute the following steps:
(New) S4: when the interface of the calling party is overtime, redis Setnx is used, key is used as a feature code, value is in a termination state, a command returns to 0, the result is stored in redis by the service party, a state value in the redis is obtained, the state value is successful, the service can be completed, the operation code is not rolled back, del key in the redis is a key value pair of the feature code, if the operation of the redis is abnormal, the code is not rolled, the code is recorded in a maintenance thread, and the operation is cleared regularly.
Example 2
And (3) returning a scene by the service side in cross-library calling failure:
s1: the globally unique feature code is generated through a tool class, and is generally composed of system parameters, time parameters and random number encryption.
S2: when the calling party calls the direct interface, the feature code is added besides the service parameter.
S3: when the server normally and successfully returns, redis Setnx is used, key is used as a feature code, value is a failure state, the command returns to 1, and then an interface calling state code is returned, wherein the interface calling state code is failed.
S4: and if the operation of the redis abnormal, the codes are not rolled back, and are recorded in the maintenance thread, and the regular cleaning is carried out.
Critical scenario: if the interface just times out and no return failure status code is received in s4, s4 will execute the following steps:
(New) S4: when the interface of the calling party is overtime, redis Setnx is used, key is used as a feature code, value is a termination state, a command is returned to 0, the result is stored in redis by the service party, a state value in the redis is obtained, the state value is failed at the moment, the service is rolled back, del key in the redis is a key value pair of the feature code, if the operation of the redis is abnormal, the code cannot be rolled, the code is recorded in a maintenance thread, and the operation is cleared at regular time.
Example 3
The cross-library call of the server is not returned to the exception scene (when the server completes the service operation redis Setnx, the calling party is overtime and also operates redis Setnx):
s1: the globally unique feature code is generated through a tool class, and is generally composed of system parameters, time parameters and random number encryption.
S2: when the calling party calls the direct interface, the feature code is added besides the service parameter.
S3: when the server side executes the completion return result, the calling side just times out, so that a successful side result can be set as a final service completion state.
The success of the redis setnx operation of the server side is 1, the server side normally executes a return operation, the state code is determined according to the success or failure of the service, the setnx of the caller is 0, the caller judges whether the service finally succeeds or fails by acquiring the value of the redis key as the feature code, if the result is successful, the code is not rolled back, if the result is failed, the code is rolled back, the del key in the redis is the key value pair of the feature code, if the redis operation is abnormal, the code is not rolled back, the code is recorded in a maintenance thread, and the regular cleaning is carried out.
And if the current redis operation is abnormal, the codes cannot be rolled, the codes are recorded in a maintenance thread, and the codes are cleared regularly, wherein the setnx of the calling party is 1, and the codes are directly rolled back.
According to the invention, under the condition that the Redis is adopted to store the cross-library operation, the caller does not need to completely rely on the return value of the server, the service of the server can be ensured to be consistent with the caller by setting the Redis state, and even if the time-out condition occurs, the consistency of the service of the caller and the service of the server can be ensured according to the final state in the Redis, so that the final consistency of the data after the cross-library operation is ensured.
It will be understood by those skilled in the art that all or part of the processes of the methods of the embodiments described above can be implemented by hardware instructions of a computer program, which can be stored in a non-volatile computer-readable storage medium, and when executed, can include the processes of the embodiments of the methods described above. Any reference to memory, storage, database, or other medium used in the embodiments provided herein may include non-volatile and/or volatile memory, among others. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically Programmable ROM (EPROM), electrically Erasable Programmable ROM (EEPROM), or flash memory. Volatile memory can include Random Access Memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms such as Static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double Data Rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous Link DRAM (SLDRAM), rambus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
The technical features of the above embodiments can be arbitrarily combined, and for the sake of brevity, all possible combinations of the technical features in the above embodiments are not described, but should be considered as the scope of the present specification as long as there is no contradiction between the combinations of the technical features.
The above-mentioned embodiments only express several embodiments of the present application, and the description thereof is more specific and detailed, but not construed as limiting the scope of the invention. It should be noted that, for a person skilled in the art, several variations and modifications can be made without departing from the concept of the present application, which falls within the scope of protection of the present application. Therefore, the protection scope of the present patent shall be subject to the appended claims.