
公司的API接口被刷了,那是因为你没这样做
api限流的场景
发布日期:2021-05-06 22:27:13
浏览次数:26
分类:原创文章
本文共 3905 字,大约阅读时间需要 13 分钟。
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
上一篇:
下一篇:
作者: 海向
来源: cnblogs.com/haixiang/p/12012728.html
api限流的场景
限流的需求出现在许多常见的场景中
1.秒杀活动,有人使用软件恶意刷单抢货,需要限流防止机器参与活动
2.某api被各式各样系统广泛调用,严重消耗网络、内存等资源,需要合理限流
3.淘宝获取ip所在城市接口、微信公众号识别微信用户等开发接口,免费提供给用户时需要限流,更具有实时性和准确性的接口需要付费。
api限流实战
首先我们编写注解类AccessLimit
,使用注解方式在方法上限流更优雅更方便!三个参数分别代表有效时间、最大访问次数、是否需要登录,可以理解为 seconds 内最多访问 maxCount 次。
import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface AccessLimit { int seconds(); int maxCount(); boolean needLogin() default true;}
限流的思路
1.通过路径:ip的作为key,访问次数为value的方式对某一用户的某一请求进行唯一标识
2.每次访问的时候判断key
是否存在,是否count
超过了限制的访问次数
3.若访问超出限制,则应response
返回msg:请求过于频繁
给前端予以展示
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@Componentpublic class AccessLimtInterceptor implements HandlerInterceptor { @Autowired private RedisService redisService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod hm = (HandlerMethod) handler; AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class); if (null == accessLimit) { return true; } int seconds = accessLimit.seconds(); int maxCount = accessLimit.maxCount(); boolean needLogin = accessLimit.needLogin(); if (needLogin) { //判断是否登录 } String key = request.getContextPath() + ":" + request.getServletPath() + ":" + ip ; Integer count = redisService.get(key); if (null == count || -1 == count) { redisService.set(key, 1); redisService.expire(seconds); return true; } if (count < maxCount) { redisService.inCr(key); return true; } if (count >= maxCount) {// response 返回 json 请求过于频繁请稍后再试 return false; } } return true; }}
注册拦截器并配置拦截路径和不拦截路径
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;// extends WebMvcConfigurerAdapter 已经废弃,java 8开始直接继承就可以@Configurationpublic class IntercepterConfig implements WebMvcConfigurer { @Autowired private AccessLimtInterceptor accessLimtInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(accessLimtInterceptor) .addPathPatterns("/拦截路径") .excludePathPatterns("/不被拦截路径 通常为登录注册或者首页"); }}
在Controller
层的方法上直接可以使用注解@AccessLimit
import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("test")public class TestControler { @GetMapping("accessLimit") @AccessLimit(seconds = 3, maxCount = 10) public String testAccessLimit() { //xxxx return ""; }}
说句题外话,springboot全家桶技术交流群可以加我微信,但是坑位有限哦,由于忙于工作,有时不能及时回复大家,请多包涵。
猜你喜欢1、图文结合!一文搞懂 Redis 常用知识点!2、一个女生不主动联系你还有机会吗?3、基于Spring Security OAuth2.0实现单点登录SSO【完整源码】4、如果我是面试官,我会问你 Spring 那些问题?5、Mybatis的这些坑!把我坑惨了!6、某小型公司持续集成工具 jenkins 实践7、免费版的 IDEA 为啥不能使用 Tomcat ?8、做一个完整的Java Web项目需要掌握的技能强烈推荐一位大佬的公众号好文章,我在看❤️
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年04月12日 20时10分55秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
BottomNavigationView控件item多于3个时文字不显示
2021-05-08
函数指针的典型应用-计算函数的定积分(矩形法思想)
2021-05-08
8051单片机(STC89C52)以定时器中断模式实现两倒计时器异步计时
2021-05-08
用 wxPython 打印你的 App
2021-05-08
vue项目通过vue.config.js配置文件进行proxy反向代理跨域
2021-05-08
Linux下安装MySql过程
2021-05-08
android:使用audiotrack 类播放wav文件
2021-05-08
vue通过better-scroll 封装自定义的下拉刷新组件
2021-05-08
android解决:使用多线程和Handler同步更新UI
2021-05-08
Element UI 中动态路由的分析及实现
2021-05-08
使用springMVC配置视图管理器后找不到指定的页面
2021-05-08
杭电 2007 平方和与立方和(输入数据的大小顺序并不能默认)
2021-05-08
十大排序算法之三:插入排序(Python)
2021-05-08
利用递归实现二叉树的前中后序遍历(Python)
2021-05-08
冒泡排序又来啦(C/C++版本)
2021-05-08
python负数存储
2021-05-08
合并两个有序数组
2021-05-08
聊聊我的五一小假期
2021-05-08
Vue新建项目——页面初始化
2021-05-08
Node.js包使用系列(一)——修改NPM全局下载和缓存路径
2021-05-08