redis接口限流完整代码demo拿来就能用:附压测工具jmeter使用遇到的问题

一、redis限流代码demo gitee地址

https://gitee.com/a1437758743/redis-current-limiting.git

二、核心代码

1.构建限流注解

package com.jingye.demo.annotation;



import com.jingye.demo.constant.CacheConstants;
import com.jingye.demo.enums.LimitType;

import java.lang.annotation.*;

/**
 * 限流注解
 * 
 * @author ruoyi
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RateLimiter
{
    /**
     * 限流key
     */
    public String key() default CacheConstants.RATE_LIMIT_KEY;

    /**
     * 限流时间,单位秒
     */
    public int time() default 60;

    /**
     * 限流次数
     */
    public int count() default 100;

    /**
     * 限流类型
     */
    public LimitType limitType() default LimitType.DEFAULT;
}

2.限流切面类

package com.jingye.demo.aspectj;


import com.jingye.demo.annotation.RateLimiter;
import com.jingye.demo.enums.LimitType;
import com.jingye.demo.util.ServletUtils;
import com.jingye.demo.util.StringUtils;
import com.jingye.demo.util.ip.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.osgi.framework.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

/**
 * 限流处理
 *
 * @author ruoyi
 */
@Aspect
@Component
public class RateLimiterAspect
{
    private static final Logger log = LoggerFactory.getLogger(RateLimiterAspect.class);

    private RedisTemplate<Object, Object> redisTemplate;

    private RedisScript<Long> limitScript;

    @Autowired
    public void setRedisTemplate1(RedisTemplate<Object, Object> redisTemplate)
    {
        this.redisTemplate = redisTemplate;
    }

    @Autowired
    public void setLimitScript(RedisScript<Long> limitScript)
    {
        this.limitScript = limitScript;
    }

    @Before("@annotation(rateLimiter)")
    public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable
    {
        int time = rateLimiter.time();
        int count = rateLimiter.count();

        String combineKey = getCombineKey(rateLimiter, point);
        List<Object> keys = Collections.singletonList(combineKey);
        try
        {
            Long number = redisTemplate.execute(limitScript, keys, count, time);
            if (StringUtils.isNull(number) || number.intValue() > count)
            {
                throw new ServiceException("访问过于频繁,请稍候再试");
            }
            log.info("限制请求'{}',当前请求'{}',缓存key'{}'", count, number.intValue(), combineKey);
        }
        catch (ServiceException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new RuntimeException("服务器限流异常,请稍候再试");
        }
    }

    public String getCombineKey(RateLimiter rateLimiter, JoinPoint point)
    {
        StringBuffer stringBuffer = new StringBuffer(rateLimiter.key());
        if (rateLimiter.limitType() == LimitType.IP)
        {
            stringBuffer.append(IpUtils.getIpAddr(ServletUtils.getRequest())).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();
    }
}

3.redis配置类

package com.jingye.demo.config;


import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置
 * 
 * @author ruoyi
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport
{
    @Bean
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
//    @ConditionalOnSingleCandidate
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
    {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);

        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);

        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public DefaultRedisScript<Long> limitScript()
    {
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptText(limitScriptText());
        redisScript.setResultType(Long.class);
        return redisScript;
    }

    /**
     * 限流脚本
     */
    private String limitScriptText()
    {
        return "local key = KEYS[1]\n" +
                "local count = tonumber(ARGV[1])\n" +
                "local time = tonumber(ARGV[2])\n" +
                "local current = redis.call('get', key);\n" +
                "if current and tonumber(current) > count then\n" +
                "    return tonumber(current);\n" +
                "end\n" +
                "current = redis.call('incr', key)\n" +
                "if tonumber(current) == 1 then\n" +
                "    redis.call('expire', key, time)\n" +
                "end\n" +
                "return tonumber(current);";
    }
}

4.controller测试接口

package com.jingye.demo.controller;

import com.jingye.demo.annotation.RateLimiter;
import com.jingye.demo.enums.LimitType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/testRedisLimiter")
public class TestRedisLimiterController {

    @PostMapping("/test")
    //五秒才能请求一次
    @RateLimiter(key = "rate_limit_test", time = 5, count = 1, limitType = LimitType.DEFAULT)
    public String test() {
        System.out.println("接口被请求了");
        return "接口被请求了";
    }
}

5.可能存在难理解的lua脚本介绍

执行脚本的代码

Long number = redisTemplate.execute(limitScript, keys, count, time);
/**
     * 限流脚本
     */
    private String limitScriptText()
    {
        return "local key = KEYS[1]\n" +
                "local count = tonumber(ARGV[1])\n" +
                "local time = tonumber(ARGV[2])\n" +
                "local current = redis.call('get', key);\n" +
                "if current and tonumber(current) > count then\n" +
                "    return tonumber(current);\n" +
                "end\n" +
                "current = redis.call('incr', key)\n" +
                "if tonumber(current) == 1 then\n" +
                "    redis.call('expire', key, time)\n" +
                "end\n" +
                "return tonumber(current);";
    }
local key = KEYS[1] 
1.获取参数key
local count = tonumber(ARGV[1]
2.获取传入的第一个参数次数并且转为数字

local time = tonumber(ARGV[2])
3.获取第2个参数转为数字

local current = redis.call('get', key);
4.redis.call 前面是指redis命令 后面是指参数
这句的意思是获取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);
5.
如果redis.call('get', key)获取到了值 然后去判断当前redis的值是否比传入的count大
如果是的 就是直接返回当前数
如果没有获取到值并且没超过最大限流数
对key的值进行 incr 自增
如果是第一次自增 返回的值是1 表示是第一次是一个新的key和新的时间节点
这时候需要对新key设置传入的 过期时间
最后返回redis key 的值


上面这段不懂还是不懂可以看下面两点 
第一
if(布尔表达式)
then
   --[ 在布尔表达式为 true 时执行的语句 --]
end
第二
and 就是 两个都为真 有点像java的 &&
or 是有一个为真就行 像java 的 ||
备注一点 就是如果redis get不到数据
127.0.0.1:6379> get qujingye current 是为false(nil)
127.0.0.1:6379>Lua语言里,只有false和nil才为false,其他任何数据都是true0也是true!!!

6.jmeter安装测试可能出现的问题

jmeter安装文档很多如:

https://blog.csdn.net/Bella_7/article/details/119144406

备注:

1.设置中文

在这里插入图片描述

2.如果安装后启动无法找到jdk路径

Not able to find Java executable or version. Please check your Java installation.

只需要在启动文件 jmeter.bat 里面加上下面这两行即可(放在最前面就行):

SET JAVA_HOME=C:\Program Files\Java\jdk-1.8.0_40(这里是自己JDK的路径)
SET PATH=%SystemRoot%/system32;%SystemRoot%;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin
3.post请求压测

在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小瞿码上有

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值