欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年10月31日 09:51:58   作者:陌上千尋雪  
這篇文章主要介紹了基于Redis+Lua腳本實(shí)現(xiàn)分布式限流組件封裝,本文給大家介紹的非常詳細(xì),對大家的學(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ù)訪問
 return false
else
 -- 沒有超過閾值
 -- 設(shè)置當(dāng)前訪問的數(shù)量+1
 redis.call("INCRBY", methodKey, 1)
 -- 設(shè)置過期時(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();
 
 // 如果沒設(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)建測試項(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";
 }
 
}

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

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

相關(guān)文章

  • 使用Redis實(shí)現(xiàn)實(shí)時(shí)排行榜功能

    使用Redis實(shí)現(xiàn)實(shí)時(shí)排行榜功能

    排行榜功能是一個(gè)很普遍的需求。使用 Redis 中有序集合的特性來實(shí)現(xiàn)排行榜是又好又快的選擇。接下來通過本文給大家介紹使用Redis實(shí)現(xiàn)實(shí)時(shí)排行榜功能,需要的朋友可以參考下
    2021-07-07
  • 淺談Redis在分布式系統(tǒng)中的協(xié)調(diào)性運(yùn)用

    淺談Redis在分布式系統(tǒng)中的協(xié)調(diào)性運(yùn)用

    這篇文章主要介紹了Redis在分布式系統(tǒng)中的協(xié)調(diào)性運(yùn)用,講解了Redis在進(jìn)程和線程的調(diào)度上以及消息隊(duì)列中的作用,需要的朋友可以參考下
    2016-03-03
  • 深入了解Redis的看門狗機(jī)制

    深入了解Redis的看門狗機(jī)制

    Redis鎖的延期機(jī)制,通常被稱為看門狗機(jī)制,本文就拉介紹一下Redis的看門狗機(jī)制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • 基于Redission的分布式鎖實(shí)戰(zhàn)

    基于Redission的分布式鎖實(shí)戰(zhàn)

    本文主要介紹了基于Redission的分布式鎖實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Redis主從復(fù)制的原理分析

    Redis主從復(fù)制的原理分析

    Redis主從復(fù)制通過將數(shù)據(jù)鏡像到多個(gè)從節(jié)點(diǎn),實(shí)現(xiàn)高可用性和擴(kuò)展性,主從復(fù)制包括初次全量同步和增量同步兩個(gè)階段,為優(yōu)化復(fù)制性能,可以采用AOF持久化、調(diào)整復(fù)制超時(shí)時(shí)間、優(yōu)化網(wǎng)絡(luò)帶寬等措施,故障轉(zhuǎn)移機(jī)制依賴于Sentinel或Cluster組件
    2025-01-01
  • Redis接口訪問優(yōu)化的方法步驟

    Redis接口訪問優(yōu)化的方法步驟

    本文基于之前的Redis接口訪問進(jìn)行優(yōu)化,引入了接口防抖功能,通過時(shí)間段參數(shù)限制接口調(diào)用頻率,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-10-10
  • Redis中鍵的過期刪除策略深入講解

    Redis中鍵的過期刪除策略深入講解

    這篇文章主要給大家介紹了關(guān)于Redis中鍵的過期刪除策略的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • 將MongoDB作為Redis式的內(nèi)存數(shù)據(jù)庫的使用方法

    將MongoDB作為Redis式的內(nèi)存數(shù)據(jù)庫的使用方法

    這篇文章主要介紹了將MongoDB作為Redis式的內(nèi)存數(shù)據(jù)庫的使用方法,原理其實(shí)只是將內(nèi)存虛擬作為磁盤,需要的朋友可以參考下
    2015-06-06
  • Redis基本數(shù)據(jù)類型Set常用操作命令

    Redis基本數(shù)據(jù)類型Set常用操作命令

    這篇文章主要為大家介紹了Redis基本數(shù)據(jù)類型Set常用操作命令,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Redis分布式鎖實(shí)例分析講解

    Redis分布式鎖實(shí)例分析講解

    分布式鎖是控制分布式系統(tǒng)不同進(jìn)程共同訪問共享資源的一種鎖的實(shí)現(xiàn)。如果不同的系統(tǒng)或同一個(gè)系統(tǒng)的不同主機(jī)之間共享了某個(gè)臨界資源,往往需要互斥來防止彼此干擾,以保證一致性
    2022-12-12

最新評論