Redis+Lua 스 크 립 트 를 기반 으로 분포 식 스 트림 제한 구성 요소 패 키 징 을 실현 하 는 방법
7762 단어 RedisLua 각본분산 식 스 트림 제한 구성 요소
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 스 크 립 트 가 분포 식 스 트림 구성 요 소 를 실현 하 는 내용 은 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 지원 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
그래프 구조를 무상으로 취급할 수 없는 것은 싫기 때문에, redisgraph를 WSL2에 극치고 설치해 보았습니다.제목은 만우절이므로. 그렇다, 역시, 앞으로는 그래프 구조 데이터베이스라고 생각했다. 생각한 것은 몇 년 전인가. 전부터 Neo4j는 시험하고 있지만, 영업 분들로부터 상용 라이센스가 높다고 가르쳐 주었으므로, 전전...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.