使用Redis完成接口限流的過(guò)程
接口限流
在一個(gè)高并發(fā)系統(tǒng)中對(duì)流量的把控是非常重要的,當(dāng)巨大的流量直接請(qǐng)求到我們的服務(wù)器上沒(méi)多久就可能造成接口不可用,不處理的話甚至?xí)斐烧麄€(gè)應(yīng)用不可用。為了避免這種情況的發(fā)生我們就需要在請(qǐng)求接口時(shí)對(duì)接口進(jìn)行限流的操作。
怎么做?
基于springboot而言,我們想到的是通過(guò)redis的自加:incr來(lái)實(shí)現(xiàn)。我們可以通過(guò)用戶的唯一標(biāo)識(shí)來(lái)設(shè)計(jì)成redis的key,值為單位時(shí)間內(nèi)用戶的請(qǐng)求次數(shù)。
一、準(zhǔn)備工作
創(chuàng)建Spring Boot 工程,引入 Web 和 Redis 依賴,同時(shí)考慮到接口限流一般是通過(guò)注解來(lái)標(biāo)記,而注解是通過(guò) AOP 來(lái)解析的,所以我們還需要加上 AOP 的依賴:
<!-- 需要的依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>提前準(zhǔn)備好一個(gè)redis示例,并在項(xiàng)目中進(jìn)行配置。
#具體配置以實(shí)際為主這里只是演示 spring: redis: host: localhost port: 6379 password: 123
二、創(chuàng)建限流注解
限流我們一般分為兩種情況:
1、針對(duì)某一個(gè)接口單位時(shí)間內(nèi)指定允許訪問(wèn)次數(shù),例如:A接口1分鐘內(nèi)允許訪問(wèn)100次;
2、針對(duì)ip地址進(jìn)行限流,例如:ip地址A可以在1分鐘內(nèi)訪問(wèn)接口50次;
針對(duì)這兩種情況我們定義一個(gè)枚舉類:
public enum LimitType {
/**
* 默認(rèn)策略
*/
DEFAULT,
/**
* 根據(jù)IP進(jìn)行限流
*/
IP
}接下來(lái)定義限流注解:
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RateLimiter {
/**
* 限流key
*/
String key() default "rate_limit:";
/**
* 限流時(shí)間,單位秒
*/
int time() default 60;
/**
* 限流次數(shù)
*/
int count() default 50;
/**
* 限流類型
*/
LimitType limitType() default LimitType.DEFAULT;
}第一個(gè)參數(shù) key 是一個(gè)前綴,實(shí)際使用過(guò)程中是這個(gè)前綴加上接口方法的完整路徑共同來(lái)組成一個(gè) key 來(lái)存到redis中。使用時(shí)在需要進(jìn)行限流的接口中加上注解并配置詳細(xì)的參數(shù)即可。
三、定制RedisTemplate
在實(shí)際使用過(guò)程中我們通常是通過(guò)RedisTemplate來(lái)操作redis的,所以這里就需要定制我們需要的RedisTemplate,默認(rèn)的RedisTemplate中是有一下小問(wèn)題的,就是直接使用JdkSerializationRedisSerializer這個(gè)工具進(jìn)行序列化時(shí)存放到redis中的key和value是會(huì)多一些前綴的,這樣就會(huì)導(dǎo)致我們?cè)谧x取數(shù)據(jù)時(shí)可能會(huì)出現(xiàn)錯(cuò)誤。
修改 RedisTemplate 序列化方案,代碼如下:
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置連接工廠
template.setConnectionFactory(factory);
//使用Jackson2JsonRedisSerializer來(lái)序列化和反序列化redis的value值(默認(rèn)使用JDK的序列化方式)
Jackson2JsonRedisSerializer<Object> jacksonSeial = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修飾符范圍,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化輸入的類型,類必須是非final修飾的,final修飾的類,比如String,Integer等會(huì)跑出異常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
// 值采用json序列化
template.setValueSerializer(jacksonSeial);
//使用StringRedisSerializer來(lái)序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
// 設(shè)置hash key 和value序列化模式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}四、開(kāi)發(fā)lua腳本
我們?cè)趈ava 代碼中將 Lua 腳本定義好,然后發(fā)送到 Redis 服務(wù)端去執(zhí)行。我們?cè)?resources 目錄下新建 lua 文件夾專門用來(lái)存放 lua 腳本,腳本內(nèi)容如下:
local key = KEYS[1]
local count = tonumber(ARGV[1])
local time = tonumber(ARGV[2])
local current = redis.call('get', key)
if current and tonumber(current) > count then
return tonumber(current)
end
current = redis.call('incr', key)
if tonumber(current) == 1 then
redis.call('expire', key, time)
end
return tonumber(current)KEYS 和 ARGV 都是一會(huì)調(diào)用時(shí)候傳進(jìn)來(lái)的參數(shù),tonumber 就是把字符串轉(zhuǎn)為數(shù)字,redis.call 就是執(zhí)行具體的 redis 指令。具體的流程:
- 首先獲取到傳進(jìn)來(lái)的 key 以及 限流的 count 和時(shí)間 time。
- 通過(guò) get 獲取到這個(gè) key 對(duì)應(yīng)的值,這個(gè)值就是當(dāng)前時(shí)間段內(nèi)這個(gè)接口訪問(wèn)了多少次。
- 如果是第一次訪問(wèn),此時(shí)拿到的結(jié)果為 nil,否則拿到的結(jié)果應(yīng)該是一個(gè)數(shù)字,所以接下來(lái)就判斷,如果拿到的結(jié)果是一個(gè)數(shù)字,并且這個(gè)數(shù)字還大于 count,那就說(shuō)明已經(jīng)超過(guò)流量限制了,那么直接返回查詢的結(jié)果即可。
- 如果拿到的結(jié)果為 nil,說(shuō)明是第一次訪問(wèn),此時(shí)就給當(dāng)前 key 自增 1,然后設(shè)置一個(gè)過(guò)期時(shí)間。
- 最后把自增 1 后的值返回就可以了。
接下來(lái)寫(xiě)一個(gè)Bean來(lái)加載這個(gè)腳本:
@Bean
public DefaultRedisScript<Long> limitScript() {
DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua/limit.lua")));
redisScript.setResultType(Long.class);
return redisScript;
}五、解析注解
自定義切面解析注解:
@Slf4j
@Aspect
@Component
public class RateLimiterAspect {
private final RedisTemplate redisTemplate;
private final RedisScript<Long> limitScript;
public RateLimiterAspect(RedisTemplate redisTemplate, RedisScript<Long> limitScript) {
this.redisTemplate = redisTemplate;
this.limitScript = limitScript;
}
@Around("@annotation(com.example.demo.annotation.RateLimiter)")
public Object doBefore(ProceedingJoinPoint joinPoint) throws Throwable{
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
RateLimiter rateLimiter = methodSignature.getMethod().getAnnotation(RateLimiter.class);
//判斷該方法是否存在限流的注解
if (null != rateLimiter){
//獲得注解中的配置信息
int count = rateLimiter.count();
int time = rateLimiter.time();
String key = rateLimiter.key();
//調(diào)用getCombineKey()獲得存入redis中的key key -> 注解中配置的key前綴-ip地址-方法路徑-方法名
String combineKey = getCombineKey(rateLimiter, methodSignature);
log.info("combineKey->,{}",combineKey);
//將combineKey放入集合
List<Object> keys = Collections.singletonList(combineKey);
log.info("keys->",keys);
try {
//執(zhí)行l(wèi)ua腳本獲得返回值
Long number = (Long) redisTemplate.execute(limitScript, keys, count, time);
//如果返回null或者返回次數(shù)大于配置次數(shù),則限制訪問(wèn)
if (number==null || number.intValue() > count) {
throw new ServiceException("訪問(wèn)過(guò)于頻繁,請(qǐng)稍候再試");
}
log.info("限制請(qǐng)求'{}',當(dāng)前請(qǐng)求'{}',緩存key'{}'", count, number.intValue(), combineKey);
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("服務(wù)器限流異常,請(qǐng)稍候再試");
}
}
return joinPoint.proceed();
}
/**
* Gets combine key.
*
* @param rateLimiter the rate limiter
* @param signature the signature
* @return the combine key
*/
public String getCombineKey(RateLimiter rateLimiter, MethodSignature signature) {
StringBuilder stringBuffer = new StringBuilder(rateLimiter.key());
if (rateLimiter.limitType() == LimitType.IP) {
stringBuffer.append(RequestUtil.getIpAddr(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest())).append("-");
}
Method method = signature.getMethod();
Class<?> targetClass = method.getDeclaringClass();
stringBuffer.append(targetClass.getName()).append("-").append(method.getName());
return stringBuffer.toString();
}
}六、自定義異常處理
由于訪問(wèn)次數(shù)達(dá)到限制時(shí)是拋異常出來(lái),所以我們還需要寫(xiě)一個(gè)全局異常捕獲:
/**
* 自定義ServiceException
*/
public class ServiceException extends Exception{
public ServiceException(){
super();
}
public ServiceException(String msg){
super(msg);
}
}
/**
* 異常捕獲處理
*/
@RestControllerAdvice
public class GlobalExceptionAdvice {
@ExceptionHandler(ServiceException.class)
public Result<Object> serviceException(ServiceException e) {
//Result.failure()是我們?cè)谛╉?xiàng)目是自定義的統(tǒng)一返回
return Result.failure(e.getMessage());
}
}七、測(cè)試結(jié)果
測(cè)試代碼:
@GetMapping("/strategy")
@RateLimiter(time = 3,count = 1,limitType = LimitType.IP)
public String strategyTest(){
return "test";
}當(dāng)訪問(wèn)次數(shù)大于配置的限制時(shí)限制接口調(diào)用 ↓

正常結(jié)果 ↓

到此這篇關(guān)于使用Redis完成接口限流的過(guò)程的文章就介紹到這了,更多相關(guān)Redis接口限流內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解redis是如何實(shí)現(xiàn)隊(duì)列消息的ack
這篇文章主要介紹了關(guān)于redis是如何實(shí)現(xiàn)隊(duì)列消息的ack的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-04-04
Redis和Lua實(shí)現(xiàn)分布式限流器的方法詳解
這篇文章主要給大家介紹了關(guān)于Redis和Lua實(shí)現(xiàn)分布式限流器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Redis和Lua具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Redis+Caffeine多級(jí)緩存數(shù)據(jù)一致性解決方案
兩級(jí)緩存Redis+Caffeine可以解決緩存雪等問(wèn)題也可以提高接口的性能,但是可能會(huì)出現(xiàn)緩存一致性問(wèn)題,如果數(shù)據(jù)頻繁的變更,可能會(huì)導(dǎo)致Redis和Caffeine數(shù)據(jù)不一致的問(wèn)題,所以本文給大家介紹了Redis+Caffeine多級(jí)緩存數(shù)據(jù)一致性解決方案,需要的朋友可以參考下2024-12-12
Redis中的動(dòng)態(tài)字符串學(xué)習(xí)教程
這篇文章主要介紹了Redis中的動(dòng)態(tài)字符串學(xué)習(xí)教程,以sds模塊的使用為主進(jìn)行講解,需要的朋友可以參考下2015-08-08
Redis在windows環(huán)境下如何啟動(dòng)
這篇文章主要介紹了Redis在windows環(huán)境下如何啟動(dòng)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Redis increment 函數(shù)處理并發(fā)序列號(hào)案例
這篇文章主要介紹了Redis increment 函數(shù)處理并發(fā)序列號(hào)案例,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08
Redis上實(shí)現(xiàn)分布式鎖以提高性能的方案研究
這篇文章主要介紹了Redis上實(shí)現(xiàn)分布式鎖以提高性能的方案研究,其中重點(diǎn)需要理解異步算法與鎖的自動(dòng)釋放,需要的朋友可以參考下2015-12-12

