In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "springboot how to achieve interface auto-idempotency", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "springboot how to achieve interface auto-idempotent" article.
Idempotent
1. Concept: the impact of any number of execution is the same as that of a single execution.
According to this meaning, the ultimate meaning is that the impact on the database can only be one-off, can not be repeated processing. How to ensure its idempotency, there are usually the following means:
1: the database establishes a unique index to ensure that only one piece of data is eventually inserted into the database.
2: token mechanism: get a token before each API request, and then add the token to the header body of the request next time. The background verifies the token. If the verification passes the deletion of the token, the token is judged again in the next request.
3: pessimistic lock or optimistic lock, pessimistic lock can ensure that other sql cannot update data every time for update (when the database engine is innodb, the condition of select must be a unique index to prevent locking the whole table)
4: first query and then judge, first by querying whether there is data in the database, if the existence proof has been requested, directly reject the request, if it does not exist, it is proved to be the first time to come in and let it go directly.
Schematic diagram of redis to achieve automatic idempotency:
one。 Build the service Api of redis
1: the first step is to build a redis server.
2: introduce the stater of redis in springboot, or the jedis encapsulated by Spring. The api mainly used later is its set method and exists method. Here we use the encapsulated redisTemplate of springboot.
The code is as follows:
/ * redis utility class * / @ Componentpublic class RedisService {@ Autowired private RedisTemplate redisTemplate; / * write cache * @ param key * @ param value * @ return * / public boolean set (final String key,Object value) {boolean result = false; try {ValueOperations operations = redisTemplate.opsForValue (); operations.set (key,value); result = true } catch (Exception e) {result = false; e.printStackTrace ();} return result;} / * write cache validity period * @ return * / public boolean setEx (final String key, Object value,Long expireTime) {boolean result = false; try {ValueOperations operations = redisTemplate.opsForValue () Operations.set (key,value); redisTemplate.expire (key,expireTime, TimeUnit.SECONDS); / / validity period result = true;} catch (Exception e) {result = false; e.printStackTrace ();} return result } / * determine whether there is a corresponding value * @ param key * @ return * / public boolean exists (final String key) {return redisTemplate.hasKey (key);} / * read cache * @ param key * @ return * / public Object get (final String key) {Object obj = null; ValueOperations operations= redisTemplate.opsForValue () Obj = operations.get (key); return obj;} / * Delete the corresponding value * @ param key * @ return * / public boolean remvoe (final String key) {if (exists (key)) {Boolean delete = redisTemplate.delete (key); return delete;} return false;}} 2. Custom AutoIdempotent
Customize an annotation. The main purpose of defining this annotation is to add it to a method that needs to achieve idempotency. Whenever a method annotated it, it will be automatically idempotent. If the background uses reflection to scan this annotation, it will process the method to achieve automatic idempotency, using meta-annotation ElementType.METHOD to indicate that it can only be placed on the method, and etentionPolicy.RUNTIME to indicate that it is at run time.
Package com.yxkj.springboot_redis_interceptor.annotion;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target (ElementType.METHOD) @ Retention (RetentionPolicy.RUNTIME) public @ interface AutoIdempotent {}. Token creates and verifies 1.token service interfaces
Let's create a new interface and create a token service with two main methods, one for creating token and one for validating token. The creation of token mainly produces a string, and the main purpose of verifying token is to convey the request object, so why pass the request object? The main function is to get the token in the header, then verify it, and get the specific error information back to the front end through the thrown Exception.
Public interface TokenService {/ * create token * @ return * / String createToken (); / * verify the validity of token * @ param request * @ return * @ throws Exception * / boolean checkToken (HttpServletRequest request) throws Exception;} 2.token 's service implementation class
Token refers to the redis service. Create token to generate random uuid strings using a random algorithm tool class, and then put them into redis (to prevent redundant retention of data, the expiration time is set to 10000 seconds, depending on the business). If the placement is successful, the token value is returned. The checkToken method is to get the token to the value from header (or from paramter if you can't get it from header), and throw an exception if it doesn't exist. This exception information can be captured by the interceptor and returned to the front end.
The above is the content of this article on "how to achieve interface auto-idempotency in springboot". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.