最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Redis+Lua腳本實現(xiàn)分布式限流組件封裝的方法

 更新時間:2020年10月31日 09:51:58   作者:陌上千尋雪  
這篇文章主要介紹了基于Redis+Lua腳本實現(xiàn)分布式限流組件封裝,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

創(chuàng)建限流組件項目

pom.xml文件中引入相關(guān)依賴

 <dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
 
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-aop</artifactId>
 </dependency>
 
 <dependency>
 <groupId>com.google.guava</groupId>
 <artifactId>guava</artifactId>
 <version>18.0</version>
 </dependency>
 
 </dependencies>

在resources目錄下創(chuàng)建lua腳本  ratelimiter.lua

--
-- Created by IntelliJ IDEA.
-- User: 寒夜
--
 
-- 獲取方法簽名特征
local methodKey = KEYS[1]
redis.log(redis.LOG_DEBUG, 'key is', methodKey)
 
-- 調(diào)用腳本傳入的限流大小
local limit = tonumber(ARGV[1])
 
-- 獲取當前流量大小
local count = tonumber(redis.call('get', methodKey) or "0")
 
-- 是否超出限流閾值
if count + 1 > limit then
 -- 拒絕服務(wù)訪問
 return false
else
 -- 沒有超過閾值
 -- 設(shè)置當前訪問的數(shù)量+1
 redis.call("INCRBY", methodKey, 1)
 -- 設(shè)置過期時間
 redis.call("EXPIRE", methodKey, 1)
 -- 放行
 return true
end

創(chuàng)建RedisConfiguration 類

package com.imooc.springcloud;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
 
/**
 * @author 寒夜
 */
@Configuration
public class RedisConfiguration {
 
 @Bean
 public RedisTemplate<String, String> redisTemplate(
 RedisConnectionFactory factory) {
 return new StringRedisTemplate(factory);
 }
 
 @Bean
 public DefaultRedisScript loadRedisScript() {
 DefaultRedisScript redisScript = new DefaultRedisScript();
 redisScript.setLocation(new ClassPathResource("ratelimiter.lua"));
 redisScript.setResultType(java.lang.Boolean.class);
 return redisScript;
 }
 
}

創(chuàng)建一個自定義注解 

package com.hy.annotation;
 
import java.lang.annotation.*;
 
/**
 * @author 寒夜
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AccessLimiter {
 
 int limit();
 
 String methodKey() default "";
 
}

創(chuàng)建一個切入點

package com.hy.annotation;
 
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Collectors;
 
/**
 * @author 寒夜
 */
@Slf4j
@Aspect
@Component
public class AccessLimiterAspect {
 
 private final StringRedisTemplate stringRedisTemplate;
 
 private final RedisScript<Boolean> rateLimitLua;
 
 public AccessLimiterAspect(StringRedisTemplate stringRedisTemplate, RedisScript<Boolean> rateLimitLua) {
 this.stringRedisTemplate = stringRedisTemplate;
 this.rateLimitLua = rateLimitLua;
 }
 
 
 
 @Pointcut(value = "@annotation(com.hy.annotation.AccessLimiter)")
 public void cut() {
 log.info("cut");
 }
 
 @Before("cut()")
 public void before(JoinPoint joinPoint) {
 // 1. 獲得方法簽名,作為method Key
 MethodSignature signature = (MethodSignature) joinPoint.getSignature();
 Method method = signature.getMethod();
 
 AccessLimiter annotation = method.getAnnotation(AccessLimiter.class);
 if (annotation == null) {
 return;
 }
 
 String key = annotation.methodKey();
 int limit = annotation.limit();
 
 // 如果沒設(shè)置methodkey, 從調(diào)用方法簽名生成自動一個key
 if (StringUtils.isEmpty(key)) {
 Class[] type = method.getParameterTypes();
 key = method.getClass() + method.getName();
 
 if (type != null) {
 String paramTypes = Arrays.stream(type)
  .map(Class::getName)
  .collect(Collectors.joining(","));
 log.info("param types: " + paramTypes);
 key += "#" + paramTypes;
 }
 }
 
 // 2. 調(diào)用Redis
 boolean acquired = stringRedisTemplate.execute(
 rateLimitLua, // Lua script的真身
 Lists.newArrayList(key), // Lua腳本中的Key列表
 Integer.toString(limit) // Lua腳本Value列表
 );
 
 if (!acquired) {
 log.error("your access is blocked, key={}", key);
 throw new RuntimeException("Your access is blocked");
 }
 }
 
}

創(chuàng)建測試項目

pom.xml中引入組件

application.yml配置

spring:
 redis:
 host: 192.168.0.218
 port: 6379
 password: 123456
 database: 0
 application:
 name: ratelimiter-test
server:
 port: 10087

創(chuàng)建controller

package com.hy;
 
import com.hy.annotation.AccessLimiter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @author 寒夜
 */
@RestController
@Slf4j
public class Controller {
 
 private final com.hy.AccessLimiter accessLimiter;
 
 public Controller(com.hy.AccessLimiter accessLimiter) {
 this.accessLimiter = accessLimiter;
 }
 
 @GetMapping("test")
 public String test() {
 accessLimiter.limitAccess("ratelimiter-test", 3);
 return "success";
 }
 
 // 提醒! 注意配置掃包路徑(com.hy路徑不同)
 @GetMapping("test-annotation")
 @AccessLimiter(limit = 1)
 public String testAnnotation() {
 return "success";
 }
 
}

開始測試,快速點擊結(jié)果如下

到此這篇關(guān)于基于Redis+Lua腳本實現(xiàn)分布式限流組件封裝的方法的文章就介紹到這了,更多相關(guān)Redis+Lua腳本實現(xiàn)分布式限流組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解如何在Windows上配置和使用Redis持久化功能

    詳解如何在Windows上配置和使用Redis持久化功能

    Redis 是一個強大的內(nèi)存數(shù)據(jù)庫,常用于緩存和實時數(shù)據(jù)處理,然而,由于其內(nèi)存特性,一旦服務(wù)器重啟或故障,存儲在 Redis 中的數(shù)據(jù)可能會丟失,為了確保數(shù)據(jù)的安全性和持久性,Redis 提供了多種持久化機制,本文將詳細介紹如何在 Windows 上配置和使用 Redis 的持久化功能
    2024-08-08
  • Redis用GEO實現(xiàn)附近的人功能

    Redis用GEO實現(xiàn)附近的人功能

    GEO就是Geolocation的簡寫形式,代表地理坐標,這篇文章主要介紹了Redis用GEO實現(xiàn)附近的人功能,需要的朋友可以參考下
    2024-08-08
  • 詳解RedisTemplate下Redis分布式鎖引發(fā)的系列問題

    詳解RedisTemplate下Redis分布式鎖引發(fā)的系列問題

    這篇文章主要介紹了詳解RedisTemplate下Redis分布式鎖引發(fā)的系列問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • redis與mongodb的區(qū)別總結(jié)

    redis與mongodb的區(qū)別總結(jié)

    在本篇文章里小編給大家分享的是關(guān)于redis與mongodb的區(qū)別的相關(guān)知識點內(nèi)容,有需要的朋友們參考下。
    2019-06-06
  • Redis集群的實現(xiàn)全過程

    Redis集群的實現(xiàn)全過程

    Redis集群的實現(xiàn)方案主要有客戶端分片、代理模式和Cluster模式,其中,Cluster模式是Redis官方推薦的實現(xiàn)方案,它具有高可用性、高性能和自動分片等優(yōu)點
    2024-12-12
  • 解決Redis連接無法正常釋放的問題

    解決Redis連接無法正常釋放的問題

    這篇文章主要介紹了解決Redis連接無法正常釋放的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Redis 命令的詳解及簡單實例

    Redis 命令的詳解及簡單實例

    這篇文章主要介紹了Redis 命令的詳解及簡單實例的相關(guān)資料,這里提供基礎(chǔ)語法及使用實例,需要的朋友可以參考下
    2017-08-08
  • redis開啟過期監(jiān)聽的實現(xiàn)示例

    redis開啟過期監(jiān)聽的實現(xiàn)示例

    在Java項目中使用Redis的過期監(jiān)聽功能來實現(xiàn)訂單未付款到期自動取消,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-11-11
  • Redis刪除過期key策略詳解

    Redis刪除過期key策略詳解

    Redis是一款高性能的開源內(nèi)存數(shù)據(jù)庫,廣泛應(yīng)用于緩存、消息隊列、實時分析等場景,在Redis中,我們經(jīng)常需要刪除過期的key,以釋放內(nèi)存空間并保持數(shù)據(jù)的有效性,本文將為您詳細介紹Redis的過期key刪除策略,幫助您更好地管理和優(yōu)化Redis數(shù)據(jù)庫
    2023-10-10
  • Redis實戰(zhàn)之Redis實現(xiàn)異步秒殺優(yōu)化詳解

    Redis實戰(zhàn)之Redis實現(xiàn)異步秒殺優(yōu)化詳解

    這篇文章主要給大家介紹了Redis實戰(zhàn)之Redis實現(xiàn)異步秒殺優(yōu)化方法,文章通過圖片和代碼介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,感興趣的同學(xué)可以自己動手試一下
    2023-09-09

最新評論

长治县| 五寨县| 白河县| 隆回县| 兴业县| 塔城市| 沙雅县| 芜湖市| 东乡县| 海城市| 蓝田县| 教育| 淳化县| 锦屏县| 青阳县| 阜城县| 桃园县| 文登市| 宝丰县| 衡水市| 青神县| 准格尔旗| 元江| 凭祥市| 阳信县| 太仓市| 吉林省| 阳西县| 曲阜市| 沛县| 汉沽区| 长垣县| 嘉黎县| 鄂尔多斯市| 盐亭县| 车险| 禹城市| 马龙县| 呼图壁县| 绥中县| 建瓯市|