自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

這篇寫的太好了!Spring Boot + Redis 實現(xiàn)接口冪等性

存儲 存儲軟件 Redis
冪等性的概念是,任意多次執(zhí)行所產(chǎn)生的影響都與一次執(zhí)行產(chǎn)生的影響相同,按照這個含義,最終的解釋是對數(shù)據(jù)庫的影響只能是一次性的,不能重復處理。

[[351749]]

本文轉(zhuǎn)載自微信公眾號「小明菜市場」,可以通過以下二維碼關注。轉(zhuǎn)載本文請聯(lián)系小明菜市場公眾號。

介紹

冪等性的概念是,任意多次執(zhí)行所產(chǎn)生的影響都與一次執(zhí)行產(chǎn)生的影響相同,按照這個含義,最終的解釋是對數(shù)據(jù)庫的影響只能是一次性的,不能重復處理。手段如下

  • 數(shù)據(jù)庫建立唯一索引
  • token機制
  • 悲觀鎖或者是樂觀鎖
  • 先查詢后判斷

小小主要帶你們介紹Redis實現(xiàn)自動冪等性。其原理如下圖所示。

實現(xiàn)過程

引入 maven 依賴

  1. <dependency> 
  2.            <groupId>org.springframework.boot</groupId> 
  3.            <artifactId>spring-boot-starter-data-redis</artifactId> 
  4.        </dependency> 

spring 配置文件寫入

  1. server.port=8080 
  2. core.datasource.druid.enabled=true 
  3. core.datasource.druid.url=jdbc:mysql://192.168.1.225:3306/?useUnicode=true&characterEncoding=UTF-8 
  4. core.datasource.druid.username=root 
  5. core.datasource.druid.password
  6. core.redis.enabled=true 
  7. spring.redis.host=192.168.1.225 #本機的redis地址 
  8. spring.redis.port=16379 
  9. spring.redis.database=3 
  10. spring.redis.jedis.pool.max-active=10 
  11. spring.redis.jedis.pool.max-idle=10 
  12. spring.redis.jedis.pool.max-wait=5s 
  13. spring.redis.jedis.pool.min-idle=10 

引入 Redis

引入 Spring boot 中的redis相關的stater,后面需要用到 Spring Boot 封裝好的 RedisTemplate

  1. package cn.smallmartial.demo.utils; 
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired; 
  4. import org.springframework.data.redis.core.RedisTemplate; 
  5. import org.springframework.data.redis.core.ValueOperations; 
  6. import org.springframework.stereotype.Component; 
  7.  
  8. import java.io.Serializable
  9. import java.util.Objects; 
  10. import java.util.concurrent.TimeUnit; 
  11.  
  12. /** 
  13.  * @Author smallmartial 
  14.  * @Date 2020/4/16 
  15.  * @Email smallmarital@qq.com 
  16.  */ 
  17. @Component 
  18. public class RedisUtil { 
  19.  
  20.     @Autowired 
  21.     private RedisTemplate redisTemplate; 
  22.  
  23.     /** 
  24.      * 寫入緩存 
  25.      * 
  26.      * @param key 
  27.      * @param value 
  28.      * @return 
  29.      */ 
  30.     public boolean set(final String key, Object value) { 
  31.         boolean result = false
  32.         try { 
  33.             ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 
  34.             operations.set(key, value); 
  35.             result = true
  36.         } catch (Exception e) { 
  37.             e.printStackTrace(); 
  38.         } 
  39.         return result; 
  40.     } 
  41.  
  42.     /** 
  43.      * 寫入緩存設置時間 
  44.      * 
  45.      * @param key 
  46.      * @param value 
  47.      * @param expireTime 
  48.      * @return 
  49.      */ 
  50.     public boolean setEx(final String key, Object value, long expireTime) { 
  51.         boolean result = false
  52.         try { 
  53.             ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 
  54.             operations.set(key, value); 
  55.             redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); 
  56.             result = true
  57.         } catch (Exception e) { 
  58.             e.printStackTrace(); 
  59.         } 
  60.         return result; 
  61.     } 
  62.  
  63.     /** 
  64.      * 讀取緩存 
  65.      * 
  66.      * @param key 
  67.      * @return 
  68.      */ 
  69.     public Object get(final String key) { 
  70.         Object result = null
  71.         ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 
  72.         result = operations.get(key); 
  73.         return result; 
  74.     } 
  75.  
  76.     /** 
  77.      * 刪除對應的value 
  78.      * 
  79.      * @param key 
  80.      */ 
  81.     public boolean remove(final String key) { 
  82.         if (exists(key)) { 
  83.             Boolean delete = redisTemplate.delete(key); 
  84.             return delete
  85.         } 
  86.         return false
  87.  
  88.     } 
  89.  
  90.     /** 
  91.      * 判斷key是否存在 
  92.      * 
  93.      * @param key 
  94.      * @return 
  95.      */ 
  96.     public boolean exists(final String key) { 
  97.         boolean result = false
  98.         ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); 
  99.         if (Objects.nonNull(operations.get(key))) { 
  100.             result = true
  101.         } 
  102.         return result; 
  103.     } 
  104.  
  105.  

自定義注解

自定義一個注解,定義此注解的目的是把它添加到需要實現(xiàn)冪等的方法上,只要某個方法注解了其,都會自動實現(xiàn)冪等操作。其代碼如下

  1. @Target({ElementType.METHOD}) 
  2. @Retention(RetentionPolicy.RUNTIME) 
  3. public @interface AutoIdempotent { 
  4.    

token 的創(chuàng)建和實現(xiàn)

token 服務接口,我們新建一個接口,創(chuàng)建token服務,里面主要是有兩個方法,一個用來創(chuàng)建 token,一個用來驗證token

  1. public interface TokenService { 
  2.  
  3.     /** 
  4.      * 創(chuàng)建token 
  5.      * @return 
  6.      */ 
  7.     public  String createToken(); 
  8.  
  9.     /** 
  10.      * 檢驗token 
  11.      * @param request 
  12.      * @return 
  13.      */ 
  14.     public boolean checkToken(HttpServletRequest request) throws Exception; 
  15.  

token 的實現(xiàn)類,token中引用了服務的實現(xiàn)類,token引用了 redis 服務,創(chuàng)建token采用隨機算法工具類生成隨機 uuid 字符串,然后放入 redis 中,如果放入成功,返回token,校驗方法就是從 header 中獲取 token 的值,如果不存在,直接跑出異常,這個異常信息可以被直接攔截到,返回給前端。

  1. package cn.smallmartial.demo.service.impl; 
  2.  
  3. import cn.smallmartial.demo.bean.RedisKeyPrefix; 
  4. import cn.smallmartial.demo.bean.ResponseCode; 
  5. import cn.smallmartial.demo.exception.ApiResult; 
  6. import cn.smallmartial.demo.exception.BusinessException; 
  7. import cn.smallmartial.demo.service.TokenService; 
  8. import cn.smallmartial.demo.utils.RedisUtil; 
  9. import io.netty.util.internal.StringUtil; 
  10. import org.springframework.beans.factory.annotation.Autowired; 
  11. import org.springframework.stereotype.Service; 
  12. import org.springframework.util.StringUtils; 
  13.  
  14. import javax.servlet.http.HttpServletRequest; 
  15. import java.util.Random; 
  16. import java.util.UUID; 
  17.  
  18. /** 
  19.  * @Author smallmartial 
  20.  * @Date 2020/4/16 
  21.  * @Email smallmarital@qq.com 
  22.  */ 
  23. @Service 
  24. public class TokenServiceImpl implements TokenService { 
  25.     @Autowired 
  26.     private RedisUtil redisService; 
  27.  
  28.     /** 
  29.      * 創(chuàng)建token 
  30.      * 
  31.      * @return 
  32.      */ 
  33.     @Override 
  34.     public String createToken() { 
  35.         String str = UUID.randomUUID().toString().replace("-"""); 
  36.         StringBuilder token = new StringBuilder(); 
  37.         try { 
  38.             token.append(RedisKeyPrefix.TOKEN_PREFIX).append(str); 
  39.             redisService.setEx(token.toString(), token.toString(), 10000L); 
  40.             boolean empty = StringUtils.isEmpty(token.toString()); 
  41.             if (!empty) { 
  42.                 return token.toString(); 
  43.             } 
  44.         } catch (Exception ex) { 
  45.             ex.printStackTrace(); 
  46.         } 
  47.         return null
  48.     } 
  49.  
  50.     /** 
  51.      * 檢驗token 
  52.      * 
  53.      * @param request 
  54.      * @return 
  55.      */ 
  56.     @Override 
  57.     public boolean checkToken(HttpServletRequest request) throws Exception { 
  58.  
  59.         String token = request.getHeader(RedisKeyPrefix.TOKEN_NAME); 
  60.         if (StringUtils.isEmpty(token)) {// header中不存在token 
  61.             token = request.getParameter(RedisKeyPrefix.TOKEN_NAME); 
  62.             if (StringUtils.isEmpty(token)) {// parameter中也不存在token 
  63.                 throw new BusinessException(ApiResult.BADARGUMENT); 
  64.             } 
  65.         } 
  66.  
  67.         if (!redisService.exists(token)) { 
  68.             throw new BusinessException(ApiResult.REPETITIVE_OPERATION); 
  69.         } 
  70.  
  71.         boolean remove = redisService.remove(token); 
  72.         if (!remove) { 
  73.             throw new BusinessException(ApiResult.REPETITIVE_OPERATION); 
  74.         } 
  75.         return true
  76.     } 

攔截器的配置

用于攔截前端的 token,判斷前端的 token 是否有效

  1. @Configuration 
  2. public class WebMvcConfiguration extends WebMvcConfigurationSupport { 
  3.  
  4.     @Bean 
  5.     public AuthInterceptor authInterceptor() { 
  6.         return new AuthInterceptor(); 
  7.     } 
  8.  
  9.     /** 
  10.      * 攔截器配置 
  11.      * 
  12.      * @param registry 
  13.      */ 
  14.     @Override 
  15.     public void addInterceptors(InterceptorRegistry registry) { 
  16.         registry.addInterceptor(authInterceptor()); 
  17. //                .addPathPatterns("/ksb/**"
  18. //                .excludePathPatterns("/ksb/auth/**""/api/common/**""/error""/api/*"); 
  19.         super.addInterceptors(registry); 
  20.     } 
  21.  
  22.     @Override 
  23.     public void addResourceHandlers(ResourceHandlerRegistry registry) { 
  24.         registry.addResourceHandler("/**").addResourceLocations( 
  25.                 "classpath:/static/"); 
  26.         registry.addResourceHandler("swagger-ui.html").addResourceLocations( 
  27.                 "classpath:/META-INF/resources/"); 
  28.         registry.addResourceHandler("/webjars/**").addResourceLocations( 
  29.                 "classpath:/META-INF/resources/webjars/"); 
  30.         super.addResourceHandlers(registry); 
  31.     } 
  32.  
  33.  

攔截處理器:主要用于攔截掃描到 Autoldempotent 到注解方法,然后調(diào)用 tokenService 的 checkToken 方法校驗 token 是否正確,如果捕捉到異常就把異常信息渲染成 json 返回給前端。這部分代碼主要和自定義注解部分掛鉤。其主要代碼如下所示

  1. @Slf4j 
  2. public class AuthInterceptor extends HandlerInterceptorAdapter { 
  3.  
  4.     @Autowired 
  5.     private TokenService tokenService; 
  6.  
  7.     @Override 
  8.     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 
  9.  
  10.         if (!(handler instanceof HandlerMethod)) { 
  11.             return true
  12.         } 
  13.         HandlerMethod handlerMethod = (HandlerMethod) handler; 
  14.         Method method = handlerMethod.getMethod(); 
  15.         //被ApiIdempotment標記的掃描 
  16.         AutoIdempotent methodAnnotation = method.getAnnotation(AutoIdempotent.class); 
  17.         if (methodAnnotation != null) { 
  18.             try { 
  19.                 return tokenService.checkToken(request);// 冪等性校驗, 校驗通過則放行, 校驗失敗則拋出異常, 并通過統(tǒng)一異常處理返回友好提示 
  20.             } catch (Exception ex) { 
  21.                 throw new BusinessException(ApiResult.REPETITIVE_OPERATION); 
  22.             } 
  23.         } 
  24.         return true
  25.     } 
  26.  

測試用例

這里進行相關的測試用例 模擬業(yè)務請求類,通過相關的路徑獲得相關的token,然后調(diào)用 testidempotence 方法,這個方法注解了 @Autoldempotent,攔截器會攔截所有的請求,當判斷到處理的方法上面有該注解的時候,就會調(diào)用 TokenService 中的 checkToken() 方法,如果有異常會跑出,代碼如下所示

  1. /** 
  2.  * @Author smallmartial 
  3.  * @Date 2020/4/16 
  4.  * @Email smallmarital@qq.com 
  5.  */ 
  6. @RestController 
  7. public class BusinessController { 
  8.  
  9.  
  10.     @Autowired 
  11.     private TokenService tokenService; 
  12.  
  13.     @GetMapping("/get/token"
  14.     public Object  getToken(){ 
  15.         String token = tokenService.createToken(); 
  16.         return ResponseUtil.ok(token) ; 
  17.     } 
  18.  
  19.  
  20.     @AutoIdempotent 
  21.     @GetMapping("/test/Idempotence"
  22.     public Object testIdempotence() { 
  23.         String token = "接口冪等性測試"
  24.         return ResponseUtil.ok(token) ; 
  25.     } 

用瀏覽器進行訪問

用獲取到的token第一次訪問

用獲取到的token再次訪問可以看到,第二次訪問失敗,即,冪等性驗證通過。

關于作者

我是小小,雙魚座的程序猿,活在一線城市,我們下期再見。

 

責任編輯:武曉燕 來源: 小明菜市場
相關推薦

2021-10-22 14:50:23

Spring BootJava

2020-09-09 09:55:36

JavaNIOBIO

2020-07-29 10:40:21

Spring循環(huán)依賴Java

2021-06-25 09:47:59

Spring循環(huán)依賴Java

2020-05-07 10:05:58

Spring循環(huán)依賴Java

2024-08-29 09:01:39

2018-09-21 15:50:58

數(shù)據(jù)庫MySQL分庫分表

2020-06-18 11:58:13

蘋果MacOS保密

2019-10-17 09:26:05

MySQL數(shù)據(jù)庫InnoDB

2020-10-18 07:24:16

數(shù)字證書簽名網(wǎng)絡協(xié)議

2019-09-29 10:04:26

技術編程開發(fā)

2024-03-13 15:18:00

接口冪等性高并發(fā)

2021-10-27 09:55:55

Sharding-Jd分庫分表Java

2025-02-23 08:00:00

冪等性Java開發(fā)

2021-01-18 14:34:59

冪等性接口客戶端

2024-06-24 01:00:00

2021-06-21 15:57:08

微服務架構數(shù)據(jù)

2024-11-01 09:28:02

2021-01-13 11:23:59

分布式冪等性支付

2020-07-15 08:14:12

高并發(fā)
點贊
收藏

51CTO技術棧公眾號