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

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

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

創(chuàng)建限流組件項(xià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])
 
-- 獲取當(dāng)前流量大小
local count = tonumber(redis.call('get', methodKey) or "0")
 
-- 是否超出限流閾值
if count + 1 > limit then
 -- 拒絕服務(wù)訪問(wèn)
 return false
else
 -- 沒(méi)有超過(guò)閾值
 -- 設(shè)置當(dāng)前訪問(wèn)的數(shù)量+1
 redis.call("INCRBY", methodKey, 1)
 -- 設(shè)置過(guò)期時(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)建一個(gè)自定義注解 

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)建一個(gè)切入點(diǎn)

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();
 
 // 如果沒(méi)設(shè)置methodkey, 從調(diào)用方法簽名生成自動(dòng)一個(gè)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)建測(cè)試項(xià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";
 }
 
}

開(kāi)始測(cè)試,快速點(diǎn)擊結(jié)果如下

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

    Redis集群的實(shí)現(xiàn)全過(guò)程

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

    解決Redis連接無(wú)法正常釋放的問(wèn)題

    這篇文章主要介紹了解決Redis連接無(wú)法正常釋放的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • Redis 命令的詳解及簡(jiǎn)單實(shí)例

    Redis 命令的詳解及簡(jiǎn)單實(shí)例

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

    redis開(kāi)啟過(guò)期監(jiān)聽(tīng)的實(shí)現(xiàn)示例

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

    Redis刪除過(guò)期key策略詳解

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

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

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

最新評(píng)論

岳普湖县| 夏邑县| 内江市| 承德县| 华安县| 资溪县| 四会市| 淄博市| 衡东县| 河南省| 东港市| 九龙城区| 卢龙县| 镇原县| 呼图壁县| 江津市| 萍乡市| 湘西| 宣化县| 工布江达县| 化隆| 崇左市| 句容市| 柯坪县| 陆川县| 兴安盟| 东源县| 台安县| 祥云县| 阜宁县| 蓬安县| 乌拉特中旗| 开封县| 丽江市| 满城县| 茶陵县| 沙田区| 乌兰浩特市| 都匀市| 长泰县| 商河县|