網(wǎng)站首頁 編程語言 正文
說明:實(shí)際開發(fā)中,我們?cè)谇岸隧撁嫔宵c(diǎn)擊了一個(gè)按鈕,訪問了一個(gè)接口,這時(shí)因?yàn)榫W(wǎng)絡(luò)波動(dòng)或者其他原因,頁面上沒有反應(yīng),用戶可能會(huì)在短時(shí)間內(nèi)再次點(diǎn)擊一次或者用戶以為沒有點(diǎn)到,很快的又點(diǎn)了一次。導(dǎo)致短時(shí)間內(nèi)發(fā)送了兩個(gè)請(qǐng)求到后臺(tái),可能會(huì)導(dǎo)致數(shù)據(jù)重復(fù)添加。
為了避免短時(shí)間內(nèi)對(duì)一個(gè)接口訪問,我們可以通過AOP+自定義注解+Redis的方式,在接口上加一個(gè)自定義注解,然后通過AOP的前置通知,在Redis中存入一個(gè)有效期的值,當(dāng)訪問接口時(shí)這個(gè)值還未過期,則拋出異常,以此來避免短時(shí)間內(nèi)對(duì)接口的方法。
實(shí)現(xiàn)
第一步:創(chuàng)建一個(gè)自定義注解,設(shè)置兩個(gè)屬性,一個(gè)是key,一個(gè)是這個(gè)key在Redis中的有效時(shí)間;
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 LimitAccess {
/**
* 限制訪問的key
* @return
*/
String key();
/**
* 限制訪問時(shí)間
* @return
*/
int times();
}
第二步:創(chuàng)建對(duì)應(yīng)的Aspect;
import com.hezy.annotation.LimitAccess;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* AOP類(通知類)
*/
@Component
@Aspect
public class LimitAspect {
@Autowired
private RedisTemplate redisTemplate;
@Pointcut("@annotation(com.hezy.annotation.LimitAccess)")
public void pt(){};
@Around("pt()")
public Object aopAround(ProceedingJoinPoint pjp) throws Throwable {
// 獲取切入點(diǎn)上面的自定義注解
Signature signature = pjp.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
// 獲取方法上面的注解
LimitAccess limitAccess = methodSignature.getMethod().getAnnotation(LimitAccess.class);
// 獲取注解上面的屬性
int limit = limitAccess.times();
String key = limitAccess.key();
// 根據(jù)key去找Redis中的值
Object o = redisTemplate.opsForValue().get(key);
// 如果不存在,說明是首次訪問,存入Redis,過期時(shí)間為limitAccess中的time
if (o == null) {
redisTemplate.opsForValue().set(key, "", limit, TimeUnit.SECONDS);
// 執(zhí)行切入點(diǎn)的方法
return pjp.proceed();
} else {
// 如果存在,說明不是首次訪問,拋出異常
throw new RuntimeException("訪問過于頻繁");
}
}
}
第三步:在需要限制的接口上,加上這個(gè)注解,并設(shè)置key和限制訪問時(shí)間,如下這個(gè)這個(gè)接口,設(shè)置key為set,實(shí)際開發(fā)中可以設(shè)置一個(gè)隨機(jī)值,或者按照規(guī)則自定義設(shè)置,times為限制訪問時(shí)間;
@GetMapping("/limit")
@LimitAccess(key = "limit", times = 10)
public String limit() {
return "success";
}
演示
啟動(dòng)項(xiàng)目,訪問該接口;
(首次訪問,沒得問題,同時(shí)在Redis中存入值)
(短時(shí)間內(nèi),連續(xù)訪問,因?yàn)镽edis中值未過期)
(10秒之后再訪問,又可以了,Redis中的值過期了)
以上就是使用Redis實(shí)現(xiàn)接口防抖,避免短時(shí)間內(nèi)連續(xù)訪問接口。實(shí)際開發(fā)中,可以將異常設(shè)置為自定義異常。
原文鏈接:https://blog.csdn.net/qq_42108331/article/details/134691925
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-08-10 C++示例講解friend?static?const關(guān)鍵字的用法_C 語言
- 2022-12-23 C++類的返回值是*this的成員函數(shù)問題_C 語言
- 2023-07-29 使用 XMLHttpRequest 實(shí)現(xiàn) ajax
- 2023-08-16 uniapp頁面返回到上一個(gè)頁面,更新其數(shù)據(jù)
- 2022-12-23 Kotlin?try?catch異常處理i詳解_Android
- 2022-05-10 一起來學(xué)習(xí)C++中remove與erase的理解_C 語言
- 2023-01-02 Python利用socket實(shí)現(xiàn)多進(jìn)程的端口掃描器_python
- 2022-03-28 深入理解numpy中argmax的具體使用_python
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支