springboot+redis 實現(xiàn)分布式限流令牌桶的示例代碼
1、前言
網(wǎng)上找了很多redis分布式限流方案,要不就是太大,需要引入第三方j(luò)ar,而且還無法正常運行,要不就是定時任務(wù)定時往key中放入數(shù)據(jù),使用的時候調(diào)用,嚴(yán)重影響性能,所以著手自定義實現(xiàn)redis令牌桶。
只用到了spring-boot-starter-data-redis包,并且就幾行代碼。
2、環(huán)境準(zhǔn)備
a、idea新建springboot項目,引入spring-data-redis包
b、編寫令牌桶實現(xiàn)方法RedisLimitExcutor
c、測試功能,創(chuàng)建全局?jǐn)r截器,測試功能
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取消當(dāng)前請求
*
* @param request
* @param response
* @param handler
* @return
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
/**
* 根據(jù)實際情況設(shè)置QPS
*/
String url = request.getRequestURI();
String ip = getIpAdd(request);
//QPS設(shè)置為5,手動刷新接口可以測試出來
if (!redisLimitExcutor.tryAccess(ip+url, 5, 1)) {
throw new RuntimeException("調(diào)用頻繁");
} 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")) {
// 根據(jù)網(wǎng)卡取本機(jī)配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
return null;
}
ipAddress = inet.getHostAddress();
}
}
// 如果通過代理訪問,可能獲取2個IP,這時候去第二個(代理服務(wù)端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è)務(wù)邏輯
return "aaaaa";
}
}
4、運行項目,訪問接口
http://localhost:8080/demo/limit

當(dāng)刷新頻率高了以后,就會報錯
5、碼云地址(GitHub經(jīng)常訪問不到)
備注:
1、 redis的key可以根據(jù)實際情況設(shè)置,入例子中的ip+url,可以將全部流量進(jìn)行控制,防止惡意刷接口,但需要注意的是,使用ip方式,要將QPS設(shè)置大一些,因為會出現(xiàn)整個大廈公用一個ip的情況。也可以使用url+userName,將QPS設(shè)置小一點,可以更加精準(zhǔn)的限制api的訪問。
2、可以將拋出異常進(jìn)行全局捕獲和統(tǒng)一返回。
到此這篇關(guān)于springboot+redis 實現(xiàn)分布式限流令牌桶的示例代碼的文章就介紹到這了,更多相關(guān)springboot redis分布式限流令牌桶內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用SpringBoot?+?Redis?實現(xiàn)接口限流的方式
- SpringBoot整合Redis并且用Redis實現(xiàn)限流的方法 附Redis解壓包
- SpringBoot中使用Redis對接口進(jìn)行限流的實現(xiàn)
- 基于SpringBoot+Redis實現(xiàn)一個簡單的限流器
- SpringBoot使用Redis對用戶IP進(jìn)行接口限流的示例詳解
- Springboot使用redis實現(xiàn)接口Api限流的實例
- SpringBoot使用Redis對用戶IP進(jìn)行接口限流的項目實踐
- Springboot+Redis實現(xiàn)API接口限流的示例代碼
- Springboot使用redis實現(xiàn)接口Api限流的示例代碼
- SpringBoot使用Redis進(jìn)行限流功能實現(xiàn)
相關(guān)文章
Spring Security6 最新版配置及實現(xiàn)動態(tài)權(quán)限管理
Spring Security 在最近幾個版本中配置的寫法都有一些變化,很多常見的方法都廢棄了,并且將在未來的 Spring Security7 中移除,因此又補(bǔ)充了一些新的內(nèi)容,重新發(fā)一下,供各位使用 Spring Security 的小伙伴們參考,需要的朋友可以參考下2024-03-03
JAVA基于Redis實現(xiàn)計數(shù)器限流的使用示例
計數(shù)器法是限流算法里最簡單也是最容易實現(xiàn)的一種算法,本文主要介紹了JAVA基于Redis實現(xiàn)計數(shù)器限流的使用示例,具有一定的參考價值,感興趣的可以了解一下2023-09-09
Java 中String StringBuilder 與 StringBuffer詳解及用法實例
這篇文章主要介紹了Java 中String StringBuilder 與 StringBuffer詳解及用法實例的相關(guān)資料,需要的朋友可以參考下2017-02-02
Spring-boot結(jié)合Shrio實現(xiàn)JWT的方法
這篇文章主要介紹了Spring-boot結(jié)合Shrio實現(xiàn)JWT的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Mybatis中where標(biāo)簽與if標(biāo)簽結(jié)合使用詳細(xì)說明
mybatis中if和where用于動態(tài)sql的條件拼接,在查詢語句中如果缺失某個條件,通過if和where標(biāo)簽可以動態(tài)的改變查詢條件,下面這篇文章主要給大家介紹了關(guān)于Mybatis中where標(biāo)簽與if標(biāo)簽結(jié)合使用的詳細(xì)說明,需要的朋友可以參考下2023-03-03
SpringBoot配置 Druid 三種方式(包括純配置文件配置)
本文給大家分享在項目中用純 YML(application.yml 或者 application.properties)文件、Java 代碼配置 Bean 和注解三種方式配置 Alibaba Druid 用于監(jiān)控或者查看 SQL 狀況的相關(guān)知識,感興趣的朋友一起看看吧2021-10-10
Java框架解說之BIO NIO AIO不同IO模型演進(jìn)之路
網(wǎng)上很多IO資料,對新手來說,越看越暈。根據(jù)自己的理解,總結(jié)對比了一下BIO、NIO、AIO,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10

