1. 程式人生 > 其它 >SpringBoot+Redis實現介面限流

SpringBoot+Redis實現介面限流

原文連結:https://mp.weixin.qq.com/s/xa7oLSiSnYlVPEyMReCCaQ

1.使用maven新增依賴庫,本專案中使用的是:
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
   <version>2.1.4.RELEASE</version>
</dependency>
2.配置redis服務

下載地址:

https://github.com/MicrosoftArchive/redis/releases

下載完成後啟動即可

 

 linux安裝教程可參考:

https://blog.csdn.net/fm_vae/article/details/80234340
3.回到正題,目的是使用redis達到介面限流的效果。

定義一個註解標明需要使用限流的介面

@Retention(RUNTIME)
@Target(METHOD)
public @interface AccessLimit {
 
    int seconds();
    int maxCount();
}

在springboot的攔截器中,如果你沒有配置攔截器,需要自定義類繼承HandlerInterceptor

 @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
        //如果請求輸入方法
        if (handler instanceof HandlerMethod) {
            HandlerMethod hm = (HandlerMethod) handler;
            //獲取方法中的註解,看是否有該註解
AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class); if (accessLimit != null) { long seconds = accessLimit.seconds(); int maxCount = accessLimit.maxCount(); //關於key的生成規則可以自己定義 本專案需求是對每個方法都加上限流功能,如果你只是針對ip地址限流,那麼key只需要只用ip就好 String key = SystemUtil.getClientIp(httpServletRequest)+hm.getMethod().getName(); //從redis中獲取使用者訪問的次數 try { long q = redisService.incr(key, seconds);//此操作代表獲取該key對應的值自增1後的結果 if (q > maxCount) { //加1 render(httpServletResponse, new ResponseMsg(0, "請求過於頻繁,請稍候再試", null)); //這裡的CodeMsg是一個返回引數 return false; } return true; }catch (RedisConnectionFailureException e){ logger.info("redis錯誤"+e.getMessage().toString()); return true; } } } return false; } private void render(HttpServletResponse response, ResponseMsg cm) throws Exception { response.setContentType("application/json;charset=UTF-8"); OutputStream out = response.getOutputStream(); String str = new Gson().toJson(cm); out.write(str.getBytes("UTF-8")); out.flush(); out.close(); }

上面使用到的redisservice

public interface  RedisService {
 
    /**
     * set存資料
     * @param key
     * @param value
     * @return
     */
    boolean set(String key, String value);
 
    /**
     * get獲取資料
     * @param key
     * @return
     */
    String get(String key);
 
    /**
     * 設定有效天數
     * @param key
     * @param expire
     * @return
     */
    boolean expire(String key, long expire);
 
    /**
     * 移除資料
     * @param key
     * @return
     */
    boolean remove(String key);
 
    /**
     * 獲取自增1後的 值
     * @param key
     * @param time
     * @return
     */
    Long incr(String key,long time);
}

redisservice的實現類

@Service("redisService")
public class RedisServiceImpl implements RedisService {
 
 
    @Resource
    private RedisTemplate<String, ?> redisTemplate;
 
 
    @Override
    public boolean set(final String key, final String value) {
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                connection.set(serializer.serialize(key), serializer.serialize(value));
                return true;
            }
        });
        return result;
    }
 
    @Override
    public String get(final String key) {
        String result = redisTemplate.execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                byte[] value = connection.get(serializer.serialize(key));
                return serializer.deserialize(value);
            }
        });
        return result;
    }
 
    @Override
    public boolean expire(final String key, long expire) {
        return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }
 
    @Override
    public boolean remove(final String key) {
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                connection.del(key.getBytes());
                return true;
            }
        });
        return result;
    }
    @Override
    public Long incr(String key,long time){
        long count = redisTemplate.opsForValue().increment(key, 1);
        if (count == 1) {
            //設定有效期一分鐘
            set(key,"1");
            redisTemplate.expire(key, time, TimeUnit.SECONDS);
        }
        return count;
    }
}

至此限流的準備工作都做完了,測試一下ok,在controller方法中加上如下註解即可

@AccessLimit(seconds=second, maxCount=maxCount)