Python 官方文档:入门教程 => 点击学习
1、前言 网上找了很多Redis分布式限流方案,要不就是太大,需要引入第三方jar,而且还无法正常运行,要不就是定时任务定时往key中放入数据,使用的时候调用,严重影响性能,所以着手
网上找了很多Redis分布式限流方案,要不就是太大,需要引入第三方jar,而且还无法正常运行,要不就是定时任务定时往key中放入数据,使用的时候调用,严重影响性能,所以着手自定义实现redis令牌桶。
只用到了spring-boot-starter-data-redis包,并且就几行代码。
a、idea新建SpringBoot项目,引入spring-data-redis包
b、编写令牌桶实现方法RedisLimitExcutor
c、测试功能,创建全局拦截器,测试功能
Maven添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
令牌桶实现方法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;
@Component
public class RedisLimitExcutor {
private StringRedisTemplate stringRedisTemplate;
@Autowired
public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
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;
}
}
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("
@Configuration
public class RequestInterceptor implements HandlerInterceptor {
@Autowired
private RedisLimitExcutor redisLimitExcutor;
@Override
public boolean preHandle(httpservletRequest request, HttpServletResponse response, Object handler) {
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 写业务逻辑
return "aaaaa";
}
}
http://localhost:8080/demo/limit
当刷新频率高了以后,就会报错
5、码云地址(GitHub经常访问不到)
备注:
1、 redis的key可以根据实际情况设置,入例子中的ip+url,可以将全部流量进行控制,防止恶意刷接口,但需要注意的是,使用ip方式,要将QPS设置大一些,因为会出现整个大厦公用一个ip的情况。也可以使用url+userName,将QPS设置小一点,可以更加精准的限制api的访问。
2、可以将抛出异常进行全局捕获和统一返回。
到此这篇关于springboot+redis 实现分布式限流令牌桶的示例代码的文章就介绍到这了,更多相关springboot redis分布式限流令牌桶内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: springboot+redis 实现分布式限流令牌桶的示例代码
本文链接: https://lsjlt.com/news/124811.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0