国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:118日期:2023-03-14 15:36:59
1、前言

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

2、環(huán)境準(zhǔn)備

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

3、上代碼

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

maven添加依賴

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

令牌桶實(shí)現(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;/** * 令牌桶實(shí)現(xiàn) */@Componentpublic class RedisLimitExcutor { private StringRedisTemplate stringRedisTemplate; @Autowired public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate = stringRedisTemplate; } /** * 令牌的 * * @param keykey值 * @param limitCount 容量 * @param seconds 時(shí)間間隔 * @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;/** * 攔截器配置 */@Configurationpublic class WebAppConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(getRequestInterceptor()).addPathPatterns('/**'); } @Bean public RequestInterceptor getRequestInterceptor() {return new RequestInterceptor(); }}

攔截器實(shí)現(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;/** * 攔截器實(shí)現(xiàn) */@Configurationpublic class RequestInterceptor implements HandlerInterceptor { @Autowired private RedisLimitExcutor redisLimitExcutor; /** * 只有返回true才會(huì)繼續(xù)向下執(zhí)行,返回false取消當(dāng)前請(qǐng)求 * * @param request * @param response * @param handler * @return */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {/** * 根據(jù)實(shí)際情況設(shè)置QPS */String url = request.getRequestURI();String ip = getIpAdd(request);//QPS設(shè)置為5,手動(dòng)刷新接口可以測(cè)試出來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ī)配置的IPInetAddress inet = null;try { inet = InetAddress.getLocalHost();} catch (UnknownHostException e) { return null;}ipAddress = inet.getHostAddress(); }}// 如果通過代理訪問,可能獲取2個(gè)IP,這時(shí)候去第二個(gè)(代理服務(wù)端IP)if (ipAddress.split(',').length > 1) { ipAddress = ipAddress.split(',')[1].trim();}return ipAddress; }}

測(cè)試controller

package com.example.redis_limit_demo.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RequestMapping('demo')@RestControllerpublic class DemoController { @RequestMapping('limit') public String demo() {//todo 寫業(yè)務(wù)邏輯return 'aaaaa'; }}4、運(yùn)行項(xiàng)目,訪問接口

http://localhost:8080/demo/limit

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

當(dāng)刷新頻率高了以后,就會(huì)報(bào)錯(cuò)

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

備注:

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

到此這篇關(guān)于springboot+redis 實(shí)現(xiàn)分布式限流令牌桶的示例代碼的文章就介紹到這了,更多相關(guān)springboot redis分布式限流令牌桶內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 亚洲欧美在线观看视频 | 国产成年人网站 | 最新久久免费视频 | 国产日产亚洲系列首页 | 国产精品久久人人做人人爽 | 波多野结衣视频免费 | 九九视频免费精品视频免费 | 亚洲成人在线播放 | 国产在线爱做人成小视频 | 久久久久性| 国产一级爱 | 日本一级特黄大一片免 | 手机在线观看亚洲国产精品 | 美女黄色片免费 | 欧美午夜三级我不卡在线观看 | 中国女警察一级毛片视频 | 一区二区三区高清在线 | 国产在线91区精品 | 亚洲第一区香蕉_国产a | 亚洲在线视频观看 | 久久久久久一级毛片免费野外 | 国产在线高清视频 | 日本人视频网站一 | 91精品久久久久久久久网影视 | 免费特黄 | 国产免费久久 | 国产一区二区在线观看免费 | 欧美野外性k8播放性迷宫 | 真人一级毛片 | 亚洲欧美另类色妞网站 | 一本色道久久爱88av | 世界一级毛片 | 曰韩三级| 日韩亚洲欧美综合一区二区三区 | 手机在线观看亚洲国产精品 | 成人午夜免费在线观看 | 久久久久久久国产精品 | 欧美精品午夜久久久伊人 | 国产成人高清亚洲一区91 | 久久久99精品免费观看 | 日本一区二区不卡久久入口 |