欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot+redis 實現(xiàn)分布式限流令牌桶的示例代碼

 更新時間:2021年04月28日 10:53:25   作者:zhijiesmile  
這篇文章主要介紹了springboot+redis 實現(xiàn)分布式限流令牌桶 ,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1、前言

網上找了很多redis分布式限流方案,要不就是太大,需要引入第三方jar,而且還無法正常運行,要不就是定時任務定時往key中放入數(shù)據,使用的時候調用,嚴重影響性能,所以著手自定義實現(xiàn)redis令牌桶。
只用到了spring-boot-starter-data-redis包,并且就幾行代碼。

2、環(huán)境準備

a、idea新建springboot項目,引入spring-data-redis包
b、編寫令牌桶實現(xiàn)方法RedisLimitExcutor
c、測試功能,創(chuàng)建全局攔截器,測試功能

3、上代碼

在這里插入圖片描述

maven添加依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

令牌桶實現(xiàn)方法RedisLimitExcutor

package com.example.redis_limit_demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 令牌桶實現(xiàn)
 */
@Component
public class RedisLimitExcutor {

    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    /**
     * 令牌的
     *
     * @param key        key值
     * @param limitCount 容量
     * @param seconds    時間間隔
     * @return
     */
    public boolean tryAccess(String key, int limitCount, int seconds) {
        String luaScript = buildLuaScript();
        RedisScript<Long> redisScript = new DefaultRedisScript<>(luaScript, Long.class);
        List<String> keys = new ArrayList<>();
        keys.add(key);
        Long count = stringRedisTemplate.execute(redisScript, keys, String.valueOf(limitCount), String.valueOf(seconds));
        if (count != 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 腳本
     *
     * @return
     */
    private static final String buildLuaScript() {
        StringBuilder lua = new StringBuilder();
        lua.append(" local key = KEYS[1]");
        lua.append("\nlocal limit = tonumber(ARGV[1])");
        lua.append("\nlocal curentLimit = tonumber(redis.call('get', key) or \"0\")");
        lua.append("\nif curentLimit + 1 > limit then");
        lua.append("\nreturn 0");
        lua.append("\nelse");
        lua.append("\n redis.call(\"INCRBY\", key, 1)");
        lua.append("\nredis.call(\"EXPIRE\", key, ARGV[2])");
        lua.append("\nreturn curentLimit + 1");
        lua.append("\nend");
        return lua.toString();
    }
}

攔截器配置WebAppConfig

package com.example.redis_limit_demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 攔截器配置
 */
@Configuration
public class WebAppConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getRequestInterceptor()).addPathPatterns("/**");
    }

    @Bean
    public RequestInterceptor getRequestInterceptor() {
        return new RequestInterceptor();
    }
}

攔截器實現(xiàn)RequestInterceptor

package com.example.redis_limit_demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 攔截器實現(xiàn)
 */
@Configuration
public class RequestInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisLimitExcutor redisLimitExcutor;


    /**
     * 只有返回true才會繼續(xù)向下執(zhí)行,返回false取消當前請求
     *
     * @param request
     * @param response
     * @param handler
     * @return
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        /**
         * 根據實際情況設置QPS
         */
        String url = request.getRequestURI();
        String ip = getIpAdd(request);
        //QPS設置為5,手動刷新接口可以測試出來
        if (!redisLimitExcutor.tryAccess(ip+url, 5, 1)) {
            throw new RuntimeException("調用頻繁");
        } else {
            return true;
        }
    }

    public static final  String getIpAdd(HttpServletRequest request) {
        String ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                // 根據網卡取本機配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    return null;
                }
                ipAddress = inet.getHostAddress();
            }
        }
        // 如果通過代理訪問,可能獲取2個IP,這時候去第二個(代理服務端IP)
        if (ipAddress.split(",").length > 1) {
            ipAddress = ipAddress.split(",")[1].trim();
        }
        return ipAddress;
    }


}

測試controller

package com.example.redis_limit_demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("demo")
@RestController
public class DemoController {


    @RequestMapping("limit")
    public String demo() {
        //todo 寫業(yè)務邏輯
        return "aaaaa";
    }

}

4、運行項目,訪問接口

http://localhost:8080/demo/limit

在這里插入圖片描述

當刷新頻率高了以后,就會報錯

5、碼云地址(GitHub經常訪問不到)

備注:

1、 redis的key可以根據實際情況設置,入例子中的ip+url,可以將全部流量進行控制,防止惡意刷接口,但需要注意的是,使用ip方式,要將QPS設置大一些,因為會出現(xiàn)整個大廈公用一個ip的情況。也可以使用url+userName,將QPS設置小一點,可以更加精準的限制api的訪問。
2、可以將拋出異常進行全局捕獲和統(tǒng)一返回。

到此這篇關于springboot+redis 實現(xiàn)分布式限流令牌桶的示例代碼的文章就介紹到這了,更多相關springboot redis分布式限流令牌桶內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Spring Security6 最新版配置及實現(xiàn)動態(tài)權限管理

    Spring Security6 最新版配置及實現(xiàn)動態(tài)權限管理

    Spring Security 在最近幾個版本中配置的寫法都有一些變化,很多常見的方法都廢棄了,并且將在未來的 Spring Security7 中移除,因此又補充了一些新的內容,重新發(fā)一下,供各位使用 Spring Security 的小伙伴們參考,需要的朋友可以參考下
    2024-03-03
  • JAVA基于Redis實現(xiàn)計數(shù)器限流的使用示例

    JAVA基于Redis實現(xiàn)計數(shù)器限流的使用示例

    計數(shù)器法是限流算法里最簡單也是最容易實現(xiàn)的一種算法,本文主要介紹了JAVA基于Redis實現(xiàn)計數(shù)器限流的使用示例,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • Java 中String StringBuilder 與 StringBuffer詳解及用法實例

    Java 中String StringBuilder 與 StringBuffer詳解及用法實例

    這篇文章主要介紹了Java 中String StringBuilder 與 StringBuffer詳解及用法實例的相關資料,需要的朋友可以參考下
    2017-02-02
  • Java如何對方法進行調用詳解

    Java如何對方法進行調用詳解

    今天給大家整理了Java如何對方法進行調用,文中有非常詳細的介紹及代碼示例,對正在學習java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • CentOS安裝solr 4.10.3詳細教程

    CentOS安裝solr 4.10.3詳細教程

    這篇文章主要為大家詳細介紹了CentOS安裝solr 4.10.3的詳細教程 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Spring-boot結合Shrio實現(xiàn)JWT的方法

    Spring-boot結合Shrio實現(xiàn)JWT的方法

    這篇文章主要介紹了Spring-boot結合Shrio實現(xiàn)JWT的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Mybatis中where標簽與if標簽結合使用詳細說明

    Mybatis中where標簽與if標簽結合使用詳細說明

    mybatis中if和where用于動態(tài)sql的條件拼接,在查詢語句中如果缺失某個條件,通過if和where標簽可以動態(tài)的改變查詢條件,下面這篇文章主要給大家介紹了關于Mybatis中where標簽與if標簽結合使用的詳細說明,需要的朋友可以參考下
    2023-03-03
  • SpringBoot配置 Druid 三種方式(包括純配置文件配置)

    SpringBoot配置 Druid 三種方式(包括純配置文件配置)

    本文給大家分享在項目中用純 YML(application.yml 或者 application.properties)文件、Java 代碼配置 Bean 和注解三種方式配置 Alibaba Druid 用于監(jiān)控或者查看 SQL 狀況的相關知識,感興趣的朋友一起看看吧
    2021-10-10
  • Java框架解說之BIO NIO AIO不同IO模型演進之路

    Java框架解說之BIO NIO AIO不同IO模型演進之路

    網上很多IO資料,對新手來說,越看越暈。根據自己的理解,總結對比了一下BIO、NIO、AIO,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-10-10
  • java 多線程的同步幾種方法

    java 多線程的同步幾種方法

    這篇文章主要介紹了java 多線程的同步幾種方法的相關資料,這里提供5種方法,需要的朋友可以參考下
    2017-09-09

最新評論