springboot integrates redisson
Please click here
Application scenarios
Able to accurately limit current flow for specified interfaces
Description
- Redisson uses the leaky bucket algorithm implemented by redis + lua script to limit current
- Distributed current limiting and stand-alone current limiting can be carried out
Use RRateLimiter for current limiting
@GetMapping("/rateLimit") public String rateLimit(HttpServletRequest request) { RRateLimiter rateLimiter = redissonClient.getRateLimiter("redisson_rate_limiter_method1"); //RateType.OVERALL global, RateType.PER_CLIENT for each client, trySetRate initializes the current limiting policy to redis, and will not be modified if executed again. rateLimiter.trySetRate(RateType.OVERALL, 2, 1, RateIntervalUnit.SECONDS); try { // Get token boolean res = rateLimiter.tryAcquire(1); if (!res) { throw new RuntimeException("Failed to obtain permission"); } // Execute logic Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } return "Get permission"; }
Note: The current configuration policy is to fill 2 tokens in 1 second, and you need to obtain 1 token to execute the code
Test
I use apifox as a stress testing tool and open 3 threads for execution. As a result, one returns 500 and two return 200, realizing the current limit of the interface business.
Optimize into annotation mode
Introducing aop dependencies
<!-- SpringBoot interceptor --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
Custom annotations
import com.example.redissiontest.enums.LimitType; import java.lang.annotation.*; /** * Current limiting annotation * * @author */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RateLimiter { public static final String RATE_LIMIT_KEY = "rate_limit:"; /** * Current limiting key */ public String key() default RATE_LIMIT_KEY; /** * Current limiting time, unit seconds */ public int time() default 60; /** * Number of current limits */ public int count() default 100; /** * Current limiting type */ public LimitType limitType() default LimitType.DEFAULT; }
Define enumeration – current limiting type
/** * Current limiting type * * @author */ public enum LimitType { /** * Default policy restricts traffic globally */ DEFAULT, /** * Limit traffic based on requester IP */ IP }
aspectj implementation
package com.example.redissiontest.aspectj; import com.example.redissiontest.annotation.RateLimiter; import com.example.redissiontest.enums.LimitType; import com.example.redissiontest.util.IpUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.reflect.MethodSignature; import org.redisson.api.RRateLimiter; import org.redisson.api.RateIntervalUnit; import org.redisson.api.RateType; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.concurrent.TimeUnit; /** * Current limiting processing * * @author */ @Aspect @Component public class RateLimiterAspect { private RedissonClient redissonClient; @Autowired @SuppressWarnings(value = { "unchecked", "rawtypes" }) public void setRedissonClient1(RedissonClient redissonClient) { this.redissonClient = redissonClient; } @Before("@annotation(rateLimiter)") public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable { int time = rateLimiter.time(); int count = rateLimiter.count(); RRateLimiter rRateLimiter = redissonClient.getRateLimiter(getCombineKey(rateLimiter,point)); //Set current limiting policy rRateLimiter.trySetRate(RateType.OVERALL, count, time, RateIntervalUnit.SECONDS); try { // Adjustable waiting time and number of consumption tokens boolean acquire = rRateLimiter.tryAcquire(1, 1, TimeUnit.SECONDS); if (!acquire) { throw new RuntimeException("The access is too frequent, please try again later"); } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException("Server current limit exception, please try again later"); } } /** * splicing key * key[ip-]className-methodName The policy is current limiting. The IP will be spliced only if the policy is IP current limiting. * @param rateLimiter * @param point * @return */ public String getCombineKey(RateLimiter rateLimiter, JoinPoint point) { StringBuilder stringBuffer = new StringBuilder(rateLimiter.key()); if (rateLimiter.limitType() == LimitType.IP) { stringBuffer.append(IpUtils.getIpAddr()).append("-"); } MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); Class<?> targetClass = method.getDeclaringClass(); stringBuffer.append(targetClass.getName()).append("-").append(method.getName()); return stringBuffer.toString(); } }
IpUtil tool in method
package com.example.redissiontest.util; import cn.hutool.core.util.ArrayUtil; import io.micrometer.core.instrument.util.StringUtils; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.net.InetAddress; import java.net.UnknownHostException; /** * Get IP method * * @author */ public class IpUtils { /** * Get request */ public static HttpServletRequest getRequest() { return getRequestAttributes().getRequest(); } public static ServletRequestAttributes getRequestAttributes() { RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); return (ServletRequestAttributes) attributes; } public static String getIpAddr() { return getIpAddr(getRequest()); } /** * Get client IP * * @param request request object * @return IP address */ public static String getIpAddr(HttpServletRequest request) { if (request == null) { return "unknown"; } String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Forwarded-For"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Real-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip); } /** * Check if it is an internal IP address * * @param ip IP address * @return result */ public static boolean internalIp(String ip) { byte[] addr = textToNumericFormatV4(ip); return internalIp(addr) || "127.0.0.1".equals(ip); } /** * Check if it is an internal IP address * * @param addr byte address * @return result */ private static boolean internalIp(byte[] addr) { if (ArrayUtil.isEmpty(addr) || addr.length < 2) { return true; } final byte b0 = addr[0]; final byte b1 = addr[1]; // 10.x.x.x/8 final byte SECTION_1 = 0x0A; // 172.16.x.x/12 final byte SECTION_2 = (byte) 0xAC; final byte SECTION_3 = (byte) 0x10; final byte SECTION_4 = (byte) 0x1F; // 192.168.x.x/16 final byte SECTION_5 = (byte) 0xC0; final byte SECTION_6 = (byte) 0xA8; switch(b0) { case SECTION_1: return true; case SECTION_2: if (b1 >= SECTION_3 & amp; & amp; b1 <= SECTION_4) { return true; } case SECTION_5: switch (b1) { case SECTION_6: return true; } default: return false; } } /** * Convert IPv4 address to bytes * * @param text IPv4 address * @return byte byte */ public static byte[] textToNumericFormatV4(String text) { if (text.length() == 0) { return null; } byte[] bytes = new byte[4]; String[] elements = text.split("\.", -1); try { long l; int i; switch(elements.length) { case 1: l = Long.parseLong(elements[0]); if ((l < 0L) || (l > 4294967295L)) { return null; } bytes[0] = (byte) (int) (l >> 24 & amp; 0xFF); bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF); bytes[2] = (byte) (int) ((l & amp; 0xFFFF) >> 8 & amp; 0xFF); bytes[3] = (byte) (int) (l & 0xFF); break; case 2: l = Integer.parseInt(elements[0]); if ((l < 0L) || (l > 255L)) { return null; } bytes[0] = (byte) (int) (l & 0xFF); l = Integer.parseInt(elements[1]); if ((l < 0L) || (l > 16777215L)) { return null; } bytes[1] = (byte) (int) (l >> 16 & amp; 0xFF); bytes[2] = (byte) (int) ((l & amp; 0xFFFF) >> 8 & amp; 0xFF); bytes[3] = (byte) (int) (l & 0xFF); break; case 3: for (i = 0; i < 2; + + i) { l = Integer.parseInt(elements[i]); if ((l < 0L) || (l > 255L)) { return null; } bytes[i] = (byte) (int) (l & 0xFF); } l = Integer.parseInt(elements[2]); if ((l < 0L) || (l > 65535L)) { return null; } bytes[2] = (byte) (int) (l >> 8 & amp; 0xFF); bytes[3] = (byte) (int) (l & 0xFF); break; case 4: for (i = 0; i < 4; + + i) { l = Integer.parseInt(elements[i]); if ((l < 0L) || (l > 255L)) { return null; } bytes[i] = (byte) (int) (l & 0xFF); } break; default: return null; } } catch (NumberFormatException e) { return null; } return bytes; } /** * Get IP address * * @return local IP address */ public static String getHostIp() { try { return InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { } return "127.0.0.1"; } /** * Get hostname * * @return local host name */ public static String getHostName() { try { return InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { } return "Unknown"; } /** * Get the first non-unknown IP address from a multi-level reverse proxy * * @param ip obtained IP address * @return the first non-unknown IP address */ public static String getMultistageReverseProxyIp(String ip) { //Multi-level reverse proxy detection if (ip != null & amp; & amp; ip.indexOf(",") > 0) { final String[] ips = ip.trim().split(","); for (String subIp : ips) { if (false == isUnknown(subIp)) { ip = subIp; break; } } } return ip; } /** * Detect whether the given string is unknown, mostly used to detect HTTP requests. * * @param checkString The string being detected * @return Is it unknown? */ public static boolean isUnknown(String checkString) { return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString); } }
Test
@GetMapping("/annotationLimit") @RateLimiter(time = 1, count = 2) public String annotationLimit() { // Execute logic try { Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } return "Execution successful"; }