Redis+Lua 스 크 립 트 를 기반 으로 분포 식 스 트림 제한 구성 요소 패 키 징 을 실현 하 는 방법

스 트림 제한 구성 요소 항목 만 들 기

pom.xml 파일 에 의존 도 를 도입 합 니 다.

 <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 디 렉 터 리 에 lua 스 크 립 트 만 들 기  ratelimiter.lua

--
-- Created by IntelliJ IDEA.
-- User:   
--
 
--         
local methodKey = KEYS[1]
redis.log(redis.LOG_DEBUG, 'key is', methodKey)
 
--            
local limit = tonumber(ARGV[1])
 
--         
local count = tonumber(redis.call('get', methodKey) or "0")
 
--         
if count + 1 > limit then
 --       
 return false
else
 --       
 --          +1
 redis.call("INCRBY", methodKey, 1)
 --       
 redis.call("EXPIRE", methodKey, 1)
 --   
 return true
end
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;
 }
 
}
사용자 정의 설명 만 들 기 

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

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();
 
 //      methodkey,              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.   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");
 }
 }
 
}
테스트 항목 만 들 기

pom.xml 에 구성 요소 도입

application.yml 설정

spring:
 redis:
 host: 192.168.0.218
 port: 6379
 password: 123456
 database: 0
 application:
 name: ratelimiter-test
server:
 port: 10087
컨트롤 러 생 성

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";
 }
 
}
테스트 를 시작 합 니 다.빠 른 클릭 결 과 는 다음 과 같 습 니 다.


Redis+Lua 스 크 립 트 를 기반 으로 분포 식 스 트림 구성 요소 패 키 징 을 실현 하 는 방법 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 Redis+Lua 스 크 립 트 가 분포 식 스 트림 구성 요 소 를 실현 하 는 내용 은 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 지원 바 랍 니 다!

좋은 웹페이지 즐겨찾기