springboot + redis + 注解 + 攔截器 實(shí)現(xiàn)接口冪等性校驗(yàn)

一沛慢、概念

冪等性, 通俗的說就是一個(gè)接口, 多次發(fā)起同一個(gè)請(qǐng)求, 必須保證操作只能執(zhí)行一次
比如:

  • 訂單接口, 不能多次創(chuàng)建訂單
  • 支付接口, 重復(fù)支付同一筆訂單只能扣一次錢
  • 支付寶回調(diào)接口, 可能會(huì)多次回調(diào), 必須處理重復(fù)回調(diào)
  • 普通表單提交接口, 因?yàn)榫W(wǎng)絡(luò)超時(shí)等原因多次點(diǎn)擊提交, 只能成功一次
    等等

二豫柬、常見解決方案

  1. 唯一索引 -- 防止新增臟數(shù)據(jù)
  2. token機(jī)制 -- 防止頁(yè)面重復(fù)提交
  3. 悲觀鎖 -- 獲取數(shù)據(jù)的時(shí)候加鎖(鎖表或鎖行)
  4. 樂觀鎖 -- 基于版本號(hào)version實(shí)現(xiàn), 在更新數(shù)據(jù)那一刻校驗(yàn)數(shù)據(jù)
  5. 分布式鎖 -- redis(jedis万栅、redisson)或zookeeper實(shí)現(xiàn)
  6. 狀態(tài)機(jī) -- 狀態(tài)變更, 更新數(shù)據(jù)時(shí)判斷狀態(tài)

三、本文實(shí)現(xiàn)

本文采用第2種方式實(shí)現(xiàn), 即通過redis + token機(jī)制實(shí)現(xiàn)接口冪等性校驗(yàn)

四劲室、實(shí)現(xiàn)思路

為需要保證冪等性的每一次請(qǐng)求創(chuàng)建一個(gè)唯一標(biāo)識(shí)token, 先獲取token, 并將此token存入redis, 請(qǐng)求接口時(shí), 將此token放到header或者作為請(qǐng)求參數(shù)請(qǐng)求接口, 后端接口判斷redis中是否存在此token:

  • 如果存在, 正常處理業(yè)務(wù)邏輯, 并從redis中刪除此token, 那么, 如果是重復(fù)請(qǐng)求, 由于token已被刪除, 則不能通過校驗(yàn), 返回請(qǐng)勿重復(fù)操作提示
  • 如果不存在, 說明參數(shù)不合法或者是重復(fù)請(qǐng)求, 返回提示即可

五幕帆、項(xiàng)目簡(jiǎn)介

  • springboot
  • redis
  • @ApiIdempotent注解 + 攔截器對(duì)請(qǐng)求進(jìn)行攔截
  • @ControllerAdvice全局異常處理
  • 壓測(cè)工具: jmeter

說明:

  • 本文重點(diǎn)介紹冪等性核心實(shí)現(xiàn), 關(guān)于springboot如何集成redisServerResponse虑粥、ResponseCode等細(xì)枝末節(jié)不在本文討論范圍之內(nèi), 有興趣的小伙伴可以查看我的Github項(xiàng)目: https://github.com/wangzaiplus/springboot/tree/wxw

六如孝、代碼實(shí)現(xiàn)

  1. pom
        <!-- Redis-Jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

        <!--lombok 本文用到@Slf4j注解, 也可不引用, 自定義log即可-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
  1. JedisUtil
package com.wangzaiplus.test.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
@Slf4j
public class JedisUtil {

    @Autowired
    private JedisPool jedisPool;

    private Jedis getJedis() {
        return jedisPool.getResource();
    }

    /**
     * 設(shè)值
     *
     * @param key
     * @param value
     * @return
     */
    public String set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.set(key, value);
        } catch (Exception e) {
            log.error("set key:{} value:{} error", key, value, e);
            return null;
        } finally {
            close(jedis);
        }
    }

    /**
     * 設(shè)值
     *
     * @param key
     * @param value
     * @param expireTime 過期時(shí)間, 單位: s
     * @return
     */
    public String set(String key, String value, int expireTime) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.setex(key, expireTime, value);
        } catch (Exception e) {
            log.error("set key:{} value:{} expireTime:{} error", key, value, expireTime, e);
            return null;
        } finally {
            close(jedis);
        }
    }

    /**
     * 取值
     *
     * @param key
     * @return
     */
    public String get(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.get(key);
        } catch (Exception e) {
            log.error("get key:{} error", key, e);
            return null;
        } finally {
            close(jedis);
        }
    }

    /**
     * 刪除key
     *
     * @param key
     * @return
     */
    public Long del(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.del(key.getBytes());
        } catch (Exception e) {
            log.error("del key:{} error", key, e);
            return null;
        } finally {
            close(jedis);
        }
    }

    /**
     * 判斷key是否存在
     *
     * @param key
     * @return
     */
    public Boolean exists(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.exists(key.getBytes());
        } catch (Exception e) {
            log.error("exists key:{} error", key, e);
            return null;
        } finally {
            close(jedis);
        }
    }

    /**
     * 設(shè)值key過期時(shí)間
     *
     * @param key
     * @param expireTime 過期時(shí)間, 單位: s
     * @return
     */
    public Long expire(String key, int expireTime) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.expire(key.getBytes(), expireTime);
        } catch (Exception e) {
            log.error("expire key:{} error", key, e);
            return null;
        } finally {
            close(jedis);
        }
    }

    /**
     * 獲取剩余時(shí)間
     *
     * @param key
     * @return
     */
    public Long ttl(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.ttl(key);
        } catch (Exception e) {
            log.error("ttl key:{} error", key, e);
            return null;
        } finally {
            close(jedis);
        }
    }

    private void close(Jedis jedis) {
        if (null != jedis) {
            jedis.close();
        }
    }

}

  1. 自定義注解@ApiIdempotent
package com.wangzaiplus.test.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 在需要保證 接口冪等性 的Controller的方法上使用此注解
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiIdempotent {
}

  1. ApiIdempotentInterceptor攔截器
package com.wangzaiplus.test.interceptor;

import com.wangzaiplus.test.annotation.ApiIdempotent;
import com.wangzaiplus.test.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * 接口冪等性攔截器
 */
public class ApiIdempotentInterceptor implements HandlerInterceptor {

    @Autowired
    private TokenService tokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();

        ApiIdempotent methodAnnotation = method.getAnnotation(ApiIdempotent.class);
        if (methodAnnotation != null) {
            check(request);// 冪等性校驗(yàn), 校驗(yàn)通過則放行, 校驗(yàn)失敗則拋出異常, 并通過統(tǒng)一異常處理返回友好提示
        }

        return true;
    }

    private void check(HttpServletRequest request) {
        tokenService.checkToken(request);
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}

  1. TokenServiceImpl
package com.wangzaiplus.test.service.impl;

import com.wangzaiplus.test.common.Constant;
import com.wangzaiplus.test.common.ResponseCode;
import com.wangzaiplus.test.common.ServerResponse;
import com.wangzaiplus.test.exception.ServiceException;
import com.wangzaiplus.test.service.TokenService;
import com.wangzaiplus.test.util.JedisUtil;
import com.wangzaiplus.test.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.StrBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class TokenServiceImpl implements TokenService {

    private static final String TOKEN_NAME = "token";

    @Autowired
    private JedisUtil jedisUtil;

    @Override
    public ServerResponse createToken() {
        String str = RandomUtil.UUID32();
        StrBuilder token = new StrBuilder();
        token.append(Constant.Redis.TOKEN_PREFIX).append(str);

        jedisUtil.set(token.toString(), token.toString(), Constant.Redis.EXPIRE_TIME_MINUTE);

        return ServerResponse.success(token.toString());
    }

    @Override
    public void checkToken(HttpServletRequest request) {
        String token = request.getHeader(TOKEN_NAME);
        if (StringUtils.isBlank(token)) {// header中不存在token
            token = request.getParameter(TOKEN_NAME);
            if (StringUtils.isBlank(token)) {// parameter中也不存在token
                throw new ServiceException(ResponseCode.ILLEGAL_ARGUMENT.getMsg());
            }
        }

        if (!jedisUtil.exists(token)) {
            throw new ServiceException(ResponseCode.REPETITIVE_OPERATION.getMsg());
        }

        Long del = jedisUtil.del(token);
        if (del <= 0) {
            throw new ServiceException(ResponseCode.REPETITIVE_OPERATION.getMsg());
        }
    }

}

  1. TestApplication
package com.wangzaiplus.test;

import com.wangzaiplus.test.interceptor.ApiIdempotentInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@MapperScan("com.wangzaiplus.test.mapper")
public class TestApplication  extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    /**
     * 跨域
     * @return
     */
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 接口冪等性攔截器
        registry.addInterceptor(apiIdempotentInterceptor());
        super.addInterceptors(registry);
    }

    @Bean
    public ApiIdempotentInterceptor apiIdempotentInterceptor() {
        return new ApiIdempotentInterceptor();
    }

}

OK, 目前為止, 校驗(yàn)代碼準(zhǔn)備就緒, 接下來測(cè)試驗(yàn)證

七、測(cè)試驗(yàn)證

  1. 獲取token的控制器TokenController
package com.wangzaiplus.test.controller;

import com.wangzaiplus.test.common.ServerResponse;
import com.wangzaiplus.test.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/token")
public class TokenController {

    @Autowired
    private TokenService tokenService;

    @GetMapping
    public ServerResponse token() {
        return tokenService.createToken();
    }

}

  1. TestController, 注意@ApiIdempotent注解, 在需要冪等性校驗(yàn)的方法上聲明此注解即可, 不需要校驗(yàn)的無影響
package com.wangzaiplus.test.controller;

import com.wangzaiplus.test.annotation.ApiIdempotent;
import com.wangzaiplus.test.common.ServerResponse;
import com.wangzaiplus.test.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Autowired
    private TestService testService;

    @ApiIdempotent
    @PostMapping("testIdempotence")
    public ServerResponse testIdempotence() {
        return testService.testIdempotence();
    }

}

  1. 獲取token
    image.png

查看redis


image.png
  1. 測(cè)試接口安全性: 利用jmeter測(cè)試工具模擬50個(gè)并發(fā)請(qǐng)求, 將上一步獲取到的token作為參數(shù)


    image.png
image.png
  1. header或參數(shù)均不傳token, 或者token值為空, 或者token值亂填, 均無法通過校驗(yàn), 如token值為"abcd"


    image.png

八娩贷、注意點(diǎn)(非常重要)

image.png

上圖中, 不能單純的直接刪除token而不校驗(yàn)是否刪除成功, 會(huì)出現(xiàn)并發(fā)安全性問題, 因?yàn)? 有可能多個(gè)線程同時(shí)走到第46行, 此時(shí)token還未被刪除, 所以繼續(xù)往下執(zhí)行, 如果不校驗(yàn)jedisUtil.del(token)的刪除結(jié)果而直接放行, 那么還是會(huì)出現(xiàn)重復(fù)提交問題, 即使實(shí)際上只有一次真正的刪除操作, 下面重現(xiàn)一下

稍微修改一下代碼:


image.png

再次請(qǐng)求


image.png

再看看控制臺(tái)


image.png

雖然只有一個(gè)真正刪除掉token, 但由于沒有對(duì)刪除結(jié)果進(jìn)行校驗(yàn), 所以還是有并發(fā)問題, 因此, 必須校驗(yàn)

九第晰、總結(jié)

其實(shí)思路很簡(jiǎn)單, 就是每次請(qǐng)求保證唯一性, 從而保證冪等性, 通過攔截器+注解, 就不用每次請(qǐng)求都寫重復(fù)代碼, 其實(shí)也可以利用spring aop實(shí)現(xiàn), 無所謂

如果小伙伴有什么疑問或者建議歡迎提出

Github
https://github.com/wangzaiplus/springboot/tree/wxw

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子茁瘦,更是在濱河造成了極大的恐慌品抽,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,682評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件甜熔,死亡現(xiàn)場(chǎng)離奇詭異圆恤,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)腔稀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門盆昙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人焊虏,你說我怎么就攤上這事淡喜。” “怎么了诵闭?”我有些...
    開封第一講書人閱讀 165,083評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵炼团,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我疏尿,道長(zhǎng)们镜,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,763評(píng)論 1 295
  • 正文 為了忘掉前任润歉,我火速辦了婚禮模狭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘踩衩。我一直安慰自己嚼鹉,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評(píng)論 6 392
  • 文/花漫 我一把揭開白布驱富。 她就那樣靜靜地躺著锚赤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪褐鸥。 梳的紋絲不亂的頭發(fā)上线脚,一...
    開封第一講書人閱讀 51,624評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音叫榕,去河邊找鬼浑侥。 笑死,一個(gè)胖子當(dāng)著我的面吹牛晰绎,可吹牛的內(nèi)容都是我干的寓落。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼荞下,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼伶选!你這毒婦竟也來了史飞?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,261評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤仰税,失蹤者是張志新(化名)和其女友劉穎构资,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體陨簇,經(jīng)...
    沈念sama閱讀 45,722評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蚯窥,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了塞帐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,030評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡巍沙,死狀恐怖葵姥,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情句携,我是刑警寧澤榔幸,帶...
    沈念sama閱讀 35,737評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站矮嫉,受9級(jí)特大地震影響削咆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蠢笋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評(píng)論 3 330
  • 文/蒙蒙 一拨齐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧昨寞,春花似錦瞻惋、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至享怀,卻和暖如春羽峰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背添瓷。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工梅屉, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鳞贷。 一個(gè)月前我還...
    沈念sama閱讀 48,237評(píng)論 3 371
  • 正文 我出身青樓履植,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親悄晃。 傳聞我的和親對(duì)象是個(gè)殘疾皇子玫霎,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 接口調(diào)用存在的問題 現(xiàn)如今我們的系統(tǒng)大多拆分為分布式SOA凿滤,或者微服務(wù),一套系統(tǒng)中包含了多個(gè)子系統(tǒng)服務(wù)庶近,而一個(gè)子系...
    哦00閱讀 5,255評(píng)論 0 3
  • 實(shí)際系統(tǒng)中有很多操作翁脆,是不管做多少次,都應(yīng)該產(chǎn)生一樣的效果或返回一樣的結(jié)果鼻种。 例如: 1. 前端重復(fù)提交選中的數(shù)據(jù)...
    值得一看的喵閱讀 6,796評(píng)論 1 6
  • 高并發(fā)下接口冪等性解決方案 一反番、冪等性概念在編程中.一個(gè)冪等操作的特點(diǎn)是其任意多次執(zhí)行所產(chǎn)生的影響均與一次執(zhí)行的影...
    ongahong閱讀 600評(píng)論 0 2
  • 一、冪等性概念 在編程中.一個(gè)冪等操作的特點(diǎn)是其任意多次執(zhí)行所產(chǎn)生的影響均與一次執(zhí)行的影響相同叉钥。冪等函數(shù)罢缸,或冪等方...
    匆匆歲月閱讀 1,141評(píng)論 0 31
  • 說不出眼前的你 是美麗多于叛逆 還是叛逆多于美麗 只覺得你的雙眸間 有絲深埋的憂郁 憂郁著這寂寥的雨季 難道是愛情...
    澎湃簡(jiǎn)報(bào)閱讀 540評(píng)論 1 5