使用Redis完成接口限流的過程
接口限流
在一個高并發(fā)系統(tǒng)中對流量的把控是非常重要的,當(dāng)巨大的流量直接請求到我們的服務(wù)器上沒多久就可能造成接口不可用,不處理的話甚至?xí)斐烧麄€應(yīng)用不可用。為了避免這種情況的發(fā)生我們就需要在請求接口時對接口進行限流的操作。
怎么做?
基于springboot而言,我們想到的是通過redis的自加:incr來實現(xiàn)。我們可以通過用戶的唯一標(biāo)識來設(shè)計成redis的key,值為單位時間內(nèi)用戶的請求次數(shù)。
一、準(zhǔn)備工作
創(chuàng)建Spring Boot 工程,引入 Web 和 Redis 依賴,同時考慮到接口限流一般是通過注解來標(biāo)記,而注解是通過 AOP 來解析的,所以我們還需要加上 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)備好一個redis示例,并在項目中進行配置。
#具體配置以實際為主這里只是演示 spring: redis: host: localhost port: 6379 password: 123
二、創(chuàng)建限流注解
限流我們一般分為兩種情況:
1、針對某一個接口單位時間內(nèi)指定允許訪問次數(shù),例如:A接口1分鐘內(nèi)允許訪問100次;
2、針對ip地址進行限流,例如:ip地址A可以在1分鐘內(nèi)訪問接口50次;
針對這兩種情況我們定義一個枚舉類:
public enum LimitType {
/**
* 默認(rèn)策略
*/
DEFAULT,
/**
* 根據(jù)IP進行限流
*/
IP
}接下來定義限流注解:
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RateLimiter {
/**
* 限流key
*/
String key() default "rate_limit:";
/**
* 限流時間,單位秒
*/
int time() default 60;
/**
* 限流次數(shù)
*/
int count() default 50;
/**
* 限流類型
*/
LimitType limitType() default LimitType.DEFAULT;
}第一個參數(shù) key 是一個前綴,實際使用過程中是這個前綴加上接口方法的完整路徑共同來組成一個 key 來存到redis中。使用時在需要進行限流的接口中加上注解并配置詳細(xì)的參數(shù)即可。
三、定制RedisTemplate
在實際使用過程中我們通常是通過RedisTemplate來操作redis的,所以這里就需要定制我們需要的RedisTemplate,默認(rèn)的RedisTemplate中是有一下小問題的,就是直接使用JdkSerializationRedisSerializer這個工具進行序列化時存放到redis中的key和value是會多一些前綴的,這樣就會導(dǎo)致我們在讀取數(shù)據(jù)時可能會出現(xiàn)錯誤。
修改 RedisTemplate 序列化方案,代碼如下:
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置連接工廠
template.setConnectionFactory(factory);
//使用Jackson2JsonRedisSerializer來序列化和反序列化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等會跑出異常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
// 值采用json序列化
template.setValueSerializer(jacksonSeial);
//使用StringRedisSerializer來序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
// 設(shè)置hash key 和value序列化模式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}四、開發(fā)lua腳本
我們在java 代碼中將 Lua 腳本定義好,然后發(fā)送到 Redis 服務(wù)端去執(zhí)行。我們在 resources 目錄下新建 lua 文件夾專門用來存放 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 都是一會調(diào)用時候傳進來的參數(shù),tonumber 就是把字符串轉(zhuǎn)為數(shù)字,redis.call 就是執(zhí)行具體的 redis 指令。具體的流程:
- 首先獲取到傳進來的 key 以及 限流的 count 和時間 time。
- 通過 get 獲取到這個 key 對應(yīng)的值,這個值就是當(dāng)前時間段內(nèi)這個接口訪問了多少次。
- 如果是第一次訪問,此時拿到的結(jié)果為 nil,否則拿到的結(jié)果應(yīng)該是一個數(shù)字,所以接下來就判斷,如果拿到的結(jié)果是一個數(shù)字,并且這個數(shù)字還大于 count,那就說明已經(jīng)超過流量限制了,那么直接返回查詢的結(jié)果即可。
- 如果拿到的結(jié)果為 nil,說明是第一次訪問,此時就給當(dāng)前 key 自增 1,然后設(shè)置一個過期時間。
- 最后把自增 1 后的值返回就可以了。
接下來寫一個Bean來加載這個腳本:
@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ù),則限制訪問
if (number==null || number.intValue() > count) {
throw new ServiceException("訪問過于頻繁,請稍候再試");
}
log.info("限制請求'{}',當(dāng)前請求'{}',緩存key'{}'", count, number.intValue(), combineKey);
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("服務(wù)器限流異常,請稍候再試");
}
}
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();
}
}六、自定義異常處理
由于訪問次數(shù)達(dá)到限制時是拋異常出來,所以我們還需要寫一個全局異常捕獲:
/**
* 自定義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()是我們在些項目是自定義的統(tǒng)一返回
return Result.failure(e.getMessage());
}
}七、測試結(jié)果
測試代碼:
@GetMapping("/strategy")
@RateLimiter(time = 3,count = 1,limitType = LimitType.IP)
public String strategyTest(){
return "test";
}當(dāng)訪問次數(shù)大于配置的限制時限制接口調(diào)用 ↓

正常結(jié)果 ↓

到此這篇關(guān)于使用Redis完成接口限流的過程的文章就介紹到這了,更多相關(guān)Redis接口限流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis+Caffeine多級緩存數(shù)據(jù)一致性解決方案
兩級緩存Redis+Caffeine可以解決緩存雪等問題也可以提高接口的性能,但是可能會出現(xiàn)緩存一致性問題,如果數(shù)據(jù)頻繁的變更,可能會導(dǎo)致Redis和Caffeine數(shù)據(jù)不一致的問題,所以本文給大家介紹了Redis+Caffeine多級緩存數(shù)據(jù)一致性解決方案,需要的朋友可以參考下2024-12-12
Redis中的動態(tài)字符串學(xué)習(xí)教程
這篇文章主要介紹了Redis中的動態(tài)字符串學(xué)習(xí)教程,以sds模塊的使用為主進行講解,需要的朋友可以參考下2015-08-08
Redis increment 函數(shù)處理并發(fā)序列號案例
這篇文章主要介紹了Redis increment 函數(shù)處理并發(fā)序列號案例,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08

