Springboot+Redis實(shí)現(xiàn)API接口限流的示例代碼
添加Redis的jar包.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
在application.yml中配置redis
spring: ## Redis redis: database: 0 host: 127.0.0.1 port: 6379 password: jedis:pool: max-active: 8 max-wait: -1ms max-idle: 8 min-idle: 0 timeout: 2000ms
添加自定義注解
@Inherited@Documented@Target({ElementType.FIELD,ElementType.TYPE,ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)public @interface AccessLimit { //指定second 時(shí)間內(nèi) API請(qǐng)求次數(shù) int times() default 4; // 請(qǐng)求次數(shù)的指定時(shí)間范圍 秒數(shù)(redis數(shù)據(jù)過(guò)期時(shí)間) int second() default 10;}
編寫攔截器
import com.ys.xlb.annotation.AccessLimit;import com.ys.xlb.bean.Code;import com.ys.xlb.exception.GlobalException;import com.ys.xlb.utils.IpUtils;import com.ys.xlb.utils.RequestUtils;import com.ys.xlb.utils.ResultUtils;import lombok.extern.slf4j.Slf4j;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.lang.reflect.Method;import java.util.concurrent.TimeUnit;/** * @ClassName AccessLimitInterceptor * @description: API請(qǐng)求限流攔截器 * @time 2019-04-20 11:08 **/@Slf4j@Componentpublic class AccessLimitInterceptor implements HandlerInterceptor { @Resource private RedisTemplate<String, Integer> redisTemplate; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {try{ // Handler 是否為 HandlerMethod 實(shí)例 if(handler instanceof HandlerMethod){// 強(qiáng)轉(zhuǎn)HandlerMethod handlerMethod = (HandlerMethod) handler;// 獲取方法Method method = handlerMethod.getMethod();// 是否有AccessLimit注解if(!method.isAnnotationPresent(AccessLimit.class)){ return true;}// 獲取注解內(nèi)容信息AccessLimit accessLimit = method.getAnnotation(AccessLimit.class);if(accessLimit == null){ return true;}int times = accessLimit.times();//請(qǐng)求次數(shù)int second = accessLimit.second();//請(qǐng)求時(shí)間范圍//根據(jù) IP + API 限流String key = IpUtils.getIpAddr(request) + request.getRequestURI();//根據(jù)key獲取已請(qǐng)求次數(shù)Integer maxTimes = redisTemplate.opsForValue().get(key);if(maxTimes == null){ //set時(shí)一定要加過(guò)期時(shí)間 redisTemplate.opsForValue().set(key, 1, second, TimeUnit.SECONDS);}else if(maxTimes < times){ redisTemplate.opsForValue().set(key, maxTimes+1, second, TimeUnit.SECONDS);}else{ // 30405 API_REQUEST_TOO_MUCH 請(qǐng)求過(guò)于頻繁 RequestUtils.out(response, ResultUtils.error(Code.API_REQUEST_TOO_MUCH)); return false;} }}catch (Exception e){ log.error('API請(qǐng)求限流攔截異常,請(qǐng)檢查Redis是否開(kāi)啟!',e); throw new GlobalException(Code.BAD_REQUEST,e.getMessage());}return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }}
方法中的IP工具類方法
/** * IpUtils工具類方法 * 獲取真實(shí)的ip地址 * @param request * @return */ public static String getIpAddr(HttpServletRequest request) {String ip = request.getHeader('X-Forwarded-For');if(org.apache.commons.lang.StringUtils.isNotEmpty(ip) && !'unKnown'.equalsIgnoreCase(ip)){ //多次反向代理后會(huì)有多個(gè)ip值,第一個(gè)ip才是真實(shí)ip int index = ip.indexOf(','); if(index != -1){return ip.substring(0,index); }else{return ip; }}ip = request.getHeader('X-Real-IP');if(org.apache.commons.lang.StringUtils.isNotEmpty(ip) && !'unKnown'.equalsIgnoreCase(ip)){ return ip;}return request.getRemoteAddr(); }
RequestUtils.out()方法
/** * @Title: out * @Description: response輸出JSON數(shù)據(jù) * @param response : 響應(yīng)請(qǐng)求 * @param object: object * @return void **/ public static void out(ServletResponse response, Object object){PrintWriter out = null;try { response.setContentType('application/json;charset=UTF-8'); response.setCharacterEncoding('UTF-8'); out = response.getWriter(); out.println(JSONObject.fromObject(resultMap).toString());} catch (Exception e) { log.error('輸出JSON報(bào)錯(cuò)!'+e);}finally{ if(null != out){out.flush();out.close(); }} }
配置攔截器
@Configurationpublic class ApplicationConfig implements WebMvcConfigurer { //這里需要注入攔截器 否則無(wú)法獲取到攔截器注入的RedisTemplate<String, Integer> redisTemplate; @Bean public AccessLimitInterceptor accessLimitInterceptor(){return new AccessLimitInterceptor(); } /** * 配置攔截器 * @author lance * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LoginInterceptor()).addPathPatterns('/**').excludePathPatterns('/static/**','/login.html','/user/login');//API限流攔截registry.addInterceptor(accessLimitInterceptor()).addPathPatterns('/**').excludePathPatterns('/static/**','/login.html'); }}
配置攔截器的類中必須先注入這個(gè)攔截器否則無(wú)法獲取到攔截器注入的RedisTemplate<String, Integer> redisTemplate
使用注解
/** * @Title: selectAll * @Description: 查詢文章信息 **/ @AccessLimit(times = 5) @RequestMapping(value = 'selectAll' , method = {RequestMethod.GET,RequestMethod.POST}) //GetMapping(value = 'selectAll') public ResultBody selectAll(Article article) {return articleService.selectAll(article); }
請(qǐng)求測(cè)試
時(shí)間間隔為默認(rèn)的10s, 10s內(nèi)請(qǐng)求第6次出現(xiàn)此返回值,完成.
參考博客:https://blog.csdn.net/zrg523/article/details/82185088
到此這篇關(guān)于Springboot+Redis實(shí)現(xiàn)API接口限流的示例代碼的文章就介紹到這了,更多相關(guān)Springboot+Redis接口API限流內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 基于 Python 實(shí)踐感知器分類算法2. Python如何批量生成和調(diào)用變量3. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車4. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫(huà)特效5. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖6. python利用opencv實(shí)現(xiàn)顏色檢測(cè)7. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)8. Python 中如何使用 virtualenv 管理虛擬環(huán)境9. Python獲取B站粉絲數(shù)的示例代碼10. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題
