@@ -132,21 +132,23 @@ public @interface CurrentLimit {
132132}
133133```
134134来几个使用的例子吧<br >
135- 1 . 限流一个Controller中的所有接口。(例如,需要每个方法每30秒只允许调用10次 )<br >
135+ 1 . 限流一个Controller中的所有接口。(例如,需要每个方法每5秒只允许调用3次 )<br >
136136``` java
137- @CurrentLimit (interval = 30 , limit = 10 , message = " class,您的手速太快了 ,请稍后再试" )
137+ @CurrentLimit (interval = 5 , limit = 3 , message = " 在类上做限流,每个接口每5秒只能访问3次,您已经超过3次访问 ,请稍后再试" )
138138@RestController
139- @RequestMapping (" /object" )
140- public class ObjectCurrentLimitController {
141-
142- @PostMapping (" /havaParam" )
143- public void havaParam (@RequestBody Map<String , String > map ) {
144- System . out. println(" 业务代码havaParam……" );
139+ @RequestMapping (" /type/each" )
140+ public class TypeEachCurrentLimitController {
141+ private static final AtomicInteger ATOMIC_INTEGER_HAVE_PARAM = new AtomicInteger ();
142+ private static final AtomicInteger ATOMIC_INTEGER_NO_PARAM = new AtomicInteger ();
143+
144+ @GetMapping (" /haveParam" )
145+ public String haveParam (String param ) {
146+ return String . format(" 第%s次访问【param:%s】,成功获得数据!" , ATOMIC_INTEGER_HAVE_PARAM . incrementAndGet(), param);
145147 }
146148
147149 @GetMapping (" /noParam" )
148- public void noParam () {
149- System . out . println( " 业务代码noParam…… " );
150+ public String noParam () {
151+ return String . format( " 第%s次访问,成功获得数据! " , ATOMIC_INTEGER_NO_PARAM . incrementAndGet() );
150152 }
151153}
152154```
@@ -158,36 +160,55 @@ public class ObjectCurrentLimitController {
158160
1591614 . 限流某个方法的并发数
160162``` java
161- @CurrentLimit (interval = 20 , limit = 6 , message = " class,您的手速太快了,请稍后再试" )// 对当前这个类进行整体限流
162163@RestController
163- public class LimiterController {
164- private static final AtomicInteger ATOMIC_INTEGER_1 = new AtomicInteger ();
165- private static final AtomicInteger ATOMIC_INTEGER_2 = new AtomicInteger ();
166- private static final AtomicInteger ATOMIC_INTEGER_3 = new AtomicInteger ();
167-
168- @CurrentLimit (interval = 20 , limit = 5 , message = " ALL,您的手速太快了,请稍后再试" )// 对这个方法进行整体限流
169- @GetMapping (" /limitTest1" )
170- public int testLimiter1 () {
171- return ATOMIC_INTEGER_1 . incrementAndGet();
164+ @RequestMapping (" /method" )
165+ public class MethodLimiterController {
166+ private static final AtomicInteger ATOMIC_INTEGER_LOCAL = new AtomicInteger ();
167+ private static final AtomicInteger ATOMIC_INTEGER_IP = new AtomicInteger ();
168+ private static final AtomicInteger ATOMIC_INTEGER_USER = new AtomicInteger ();
169+ private static final AtomicInteger ATOMIC_INTEGER_SESSION = new AtomicInteger ();
170+ private static final AtomicInteger ATOMIC_INTEGER_CUSTOM = new AtomicInteger ();
171+
172+ @CurrentLimit (interval = 5 , limit = 3 , message = " 此方法对所有来源的请求都限流,5秒只能访问3次,您已经超过3次访问,请稍后再试" )
173+ @GetMapping (" /local" )
174+ public String local () {
175+ return String . format(" 第%s次访问,成功获得数据!" , ATOMIC_INTEGER_LOCAL . incrementAndGet());
172176 }
173177
174- @CurrentLimit (interval = 20 , limit = 3 , limitType = LimitTypeEnum . CUSTOM )// 对这个方法进行自定义限流
175- @GetMapping (" /limitTest2" )
176- public int testLimiter2 (HttpServletRequest request ) {
177- // 根据一系列操作查出来了用户id
178- // 限流时在httpServletRequest中根据Const.CUSTOM的值进行限流
179- request. setAttribute(Constant . CUSTOM , " 用户id" );
180- return ATOMIC_INTEGER_2 . incrementAndGet();
178+ @CurrentLimit (interval = 5 , limit = 3
179+ , message = " 此方法访问根据IP限流,5秒只能访问3次,您已经超过3次访问,请稍后再试" )
180+ @GetMapping (" /ip" )
181+ public String ip () {
182+ return String . format(" 第%s次访问,成功获得数据!" , ATOMIC_INTEGER_IP . incrementAndGet());
183+ }
184+
185+ @CurrentLimit (interval = 5 , limit = 3
186+ , message = " 此方法访问根据用户限流,5秒只能访问3次,您已经超过3次访问,请稍后再试" )
187+ @GetMapping (" /user" )
188+ public String user () {
189+ return String . format(" 第%s次访问,成功获得数据!" , ATOMIC_INTEGER_USER . incrementAndGet());
181190 }
182191
183- @CurrentLimit (interval = 20 , limit = 5 , limitType = LimitTypeEnum . IP , message = " IP,您的手速太快了,请稍后再试" ) // 对这个方法进行IP限流
184- @GetMapping (" /limitTest3" )
185- public int testLimiter3 () {
186- return ATOMIC_INTEGER_3 . incrementAndGet();
192+ @CurrentLimit (interval = 5 , limit = 3
193+ , message = " 此方法访问根据Session限流,5秒只能访问3次,您已经超过3次访问,请稍后再试" )
194+ @GetMapping (" /session" )
195+ public String session () {
196+ return String . format(" 第%s次访问,成功获得数据!" , ATOMIC_INTEGER_SESSION . incrementAndGet());
197+ }
198+
199+ @CurrentLimit (interval = 5 , limit = 3 , limitType = LimitTypeEnum . CUSTOM
200+ , message = " 此方法为自定义限流,5秒只能访问3次,您已经超过3次访问,请稍后再试" )
201+ @GetMapping (" /custom" )
202+ public String testLimiter2 (HttpServletRequest request ) {
203+ // 根据一系列操作查出来了用户id
204+ // 限流时在httpServletRequest中根据Const.CUSTOM的值进行限流
205+ request. setAttribute(Constant . CUSTOM , " user_id" );
206+ return String . format(" 第%s次访问,成功获得数据!" , ATOMIC_INTEGER_CUSTOM . incrementAndGet());
187207 }
188208}
189209```
190210
211+ 5 . 更多示例请移步[ 演示示例] ( https://github.com/chqiuu/spring-redis-current-limit/tree/main/spring-redis-current-limit-demo )
191212
192213## 更多信息
193214
0 commit comments