溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

springboot如何實(shí)現(xiàn)接口自動(dòng)冪等

發(fā)布時(shí)間:2022-03-29 14:00:22 來源:億速云 閱讀:241 作者:iii 欄目:大數(shù)據(jù)

這篇“springboot如何實(shí)現(xiàn)接口自動(dòng)冪等”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“springboot如何實(shí)現(xiàn)接口自動(dòng)冪等”文章吧。

冪等

1.概念: 任意多次執(zhí)行所產(chǎn)生的影響均與一次執(zhí)行的影響相同。
按照這個(gè)含義,最終的含義就是 對(duì)數(shù)據(jù)庫(kù)的影響只能是一次性的,不能重復(fù)處理。如何保證其冪等性,通常有以下手段:

1: 數(shù)據(jù)庫(kù)建立唯一性索引,可以保證最終插入數(shù)據(jù)庫(kù)的只有一條數(shù)據(jù)

2: token 機(jī)制,每次接口請(qǐng)求前先獲取一個(gè) token,然后再下次請(qǐng)求的時(shí)候在請(qǐng)求的 header 體中加上這個(gè) token,后臺(tái)進(jìn)行驗(yàn)證,如果驗(yàn)證通過刪除 token,下次請(qǐng)求再次判斷 token

3: 悲觀鎖或者樂觀鎖,悲觀鎖可以保證每次 for update 的時(shí)候其他 sql 無法 update 數(shù)據(jù) (在數(shù)據(jù)庫(kù)引擎是 innodb 的時(shí)候, select 的條件必須是唯一索引, 防止鎖全表)

4: 先查詢后判斷,首先通過查詢數(shù)據(jù)庫(kù)是否存在數(shù)據(jù),如果存在證明已經(jīng)請(qǐng)求過了,直接拒絕該請(qǐng)求,如果沒有存在,就證明是第一次進(jìn)來,直接放行。

redis 實(shí)現(xiàn)自動(dòng)冪等的原理圖:

springboot如何實(shí)現(xiàn)接口自動(dòng)冪等

一. 搭建redis的服務(wù)Api

1: 首先是搭建 redis 服務(wù)器。

2: 引入 springboot 中到的 redis 的 stater,或者 Spring 封裝的 jedis 也可以,后面主要用到的 api 就是它的 set 方法和 exists 方法, 這里我們使用 springboot 的封裝好的 redisTemplate
代碼如下:

/*
redis工具類
*/
@Component
public class RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 寫入緩存
     * @param key
     * @param value
     * @return
     */
    public  boolean set(final String key,Object value){
        boolean result = false;
        try {
            ValueOperations<Serializable,Object> operations = redisTemplate.opsForValue();
            operations.set(key,value);
            result = true;
        }catch (Exception e){
            result = false;
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 寫入緩存有效期
     * @return
     */
    public boolean setEx(final String key ,Object value,Long expireTime){
        boolean result = false;
        try {
            ValueOperations<Serializable,Object> operations = redisTemplate.opsForValue();
            operations.set(key,value);
            redisTemplate.expire(key,expireTime, TimeUnit.SECONDS);//有效期
            result = true;
        }catch (Exception e){
            result = false;
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 判斷緩存中是否有對(duì)應(yīng)的value
     * @param key
     * @return
     */
    public boolean exists(final String key){
       return redisTemplate.hasKey(key);
    }

    /**
     * 讀取緩存
     * @param key
     * @return
     */
    public Object get(final String key){
        Object obj = null;
        ValueOperations<Serializable,Object> operations= redisTemplate.opsForValue();
        obj =  operations.get(key);
        return obj;
    }

    /**
     * 刪除對(duì)應(yīng)的value
     * @param key
     * @return
     */
    public boolean remvoe(final String key){
        if(exists(key)){
            Boolean delete = redisTemplate.delete(key);
            return delete;
        }
        return false;
    }


}

二.自定義 AutoIdempotent

自定義一個(gè)注解,定義此注解的主要目的是把它添加在需要實(shí)現(xiàn)冪等的方法上,凡是某個(gè)方法注解了它,都會(huì)實(shí)現(xiàn)自動(dòng)冪等。后臺(tái)利用反射如果掃描到這個(gè)注解,就會(huì)處理這個(gè)方法實(shí)現(xiàn)自動(dòng)冪等,使用元注解 ElementType.METHOD 表示它只能放在方法上,etentionPolicy.RUNTIME 表示它在運(yùn)行時(shí)

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 創(chuàng)建和檢驗(yàn)

1.token服務(wù)接口

我們新建一個(gè)接口,創(chuàng)建 token 服務(wù),里面主要是兩個(gè)方法,一個(gè)用來創(chuàng)建 token,一個(gè)用來驗(yàn)證 token。創(chuàng)建 token 主要產(chǎn)生的是一個(gè)字符串,檢驗(yàn) token 的話主要是傳達(dá) request 對(duì)象,為什么要傳 request 對(duì)象呢?主要作用就是獲取 header 里面的 token, 然后檢驗(yàn),通過拋出的 Exception 來獲取具體的報(bào)錯(cuò)信息返回給前端

public interface TokenService {

    /**
     * 創(chuàng)建token
     * @return
     */
    String createToken();

    /**
     * 檢驗(yàn)token的合法性
     * @param request
     * @return
     * @throws Exception
     */
    boolean checkToken(HttpServletRequest request) throws Exception;
}

2.token 的服務(wù)實(shí)現(xiàn)類

token 引用了 redis 服務(wù),創(chuàng)建 token 采用隨機(jī)算法工具類生成隨機(jī) uuid 字符串, 然后放入到 redis 中 (為了防止數(shù)據(jù)的冗余保留, 這里設(shè)置過期時(shí)間為 10000 秒, 具體可視業(yè)務(wù)而定),如果放入成功,最后返回這個(gè) token 值。checkToken 方法就是從 header 中獲取 token 到值 (如果 header 中拿不到,就從 paramter 中獲取),如若不存在, 直接拋出異常。這個(gè)異常信息可以被攔截器捕捉到,然后返回給前端。

以上就是關(guān)于“springboot如何實(shí)現(xiàn)接口自動(dòng)冪等”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI