-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Feat: RedissonConfig 설정 * Feat: Redisson Lock 구현(임시) * Fix: RaceCondition 테스트 오류 수정 1. RedissonLock의 value로 hostId만 넘기기 2. RaceConditionTest를 SpringBootTest로 테스트 3. BeforeEach로 Redis 초기화 * Refactor: GroupMember 조회 fetch join 적용 * Refactor: random GroupMember 조회 fetch join 적용 * Refactor: 방문자 조회수 비동기 로직 추가 * Feat: 질문 지목 시 count 증가 동시성 제어 * Refactor: merge weekly9 * Refactor: 누락된 코드 추가 * Refactor: 누락된 코드 추가 * Refactor: 코드 수정 * Refactor: redissonClient 제거 * Refactor: redis 설정 변경 * Refactor: redis 설정 변경 * HotFix: 이미지 파일 resize 비율 조정 * Refactor: RedissonClient 사용 테스트 ActiveProfile 설정 * Refactor: RedissonClient 사용 테스트 Profile 설정 * Fix: Profile() 메서드에 적용 * Fix: ProfileIntegrationTest, RaceConditionTest 주석처리 * Fix: 충돌 해결 * Refactor: Random Question Response 수정 * Chore: 불필요한 Import 제거 * Import: 충돌 해결 --------- Co-authored-by: yso8296 <66588512+yso8296@users.noreply.github.com> Co-authored-by: Kwon Da woon <82216606+momnpa333@users.noreply.github.com>
- Loading branch information
1 parent
3bcf8d5
commit 286b95e
Showing
19 changed files
with
331 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/supernova/whokie/global/annotation/RedissonLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package supernova.whokie.global.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface RedissonLock { | ||
String value(); | ||
long waitTime() default 5000L; | ||
long leaseTime() default 2000L; | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/supernova/whokie/global/aop/RedissonAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package supernova.whokie.global.aop; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.redisson.api.RLock; | ||
import org.redisson.api.RedissonClient; | ||
import org.redisson.client.RedisException; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
import org.springframework.expression.spel.support.StandardEvaluationContext; | ||
import org.springframework.stereotype.Component; | ||
import supernova.whokie.global.annotation.RedissonLock; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Aspect | ||
@Component | ||
@RequiredArgsConstructor | ||
public class RedissonAspect { | ||
|
||
private final RedissonClient redissonClient; | ||
|
||
@Around("@annotation(supernova.whokie.global.annotation.RedissonLock)") | ||
public Object redissonLock(ProceedingJoinPoint joinPoint) throws Throwable { | ||
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | ||
Method method = signature.getMethod(); | ||
RedissonLock annotation = method.getAnnotation(RedissonLock.class); | ||
String lockKey = method.getName() + getDynamicValue(signature.getParameterNames(), joinPoint.getArgs(), annotation.value()); | ||
RLock lock = redissonClient.getLock(lockKey); | ||
try { | ||
boolean lockable = lock.tryLock(annotation.waitTime(), annotation.leaseTime(), TimeUnit.MILLISECONDS); | ||
if (!lockable) { | ||
return false; | ||
} | ||
return joinPoint.proceed(); | ||
} catch (RedisException e) { | ||
throw new Exception("Temporary errors failed to access the service"); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
public Object getDynamicValue(String[] parameterNames, Object[] args, String key) { | ||
SpelExpressionParser parser = new SpelExpressionParser(); | ||
StandardEvaluationContext context = new StandardEvaluationContext(); | ||
|
||
for(int i=0; i < parameterNames.length; i++) { | ||
context.setVariable(parameterNames[i], args[i]); | ||
} | ||
return parser.parseExpression(key).getValue(context, Object.class); | ||
} | ||
} |
27 changes: 12 additions & 15 deletions
27
src/main/java/supernova/whokie/global/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,33 @@ | ||
package supernova.whokie.global.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.redisson.Redisson; | ||
import org.redisson.api.RedissonClient; | ||
import org.redisson.config.Config; | ||
import org.redisson.spring.data.connection.RedissonConnectionFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
import supernova.whokie.global.property.RedisProperties; | ||
|
||
@Profile("redis") | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class RedisConfig { | ||
|
||
private final RedisProperties redisProperties; | ||
private static final String REDISSON_HOST_PREFIX = "redis://"; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(redisProperties.host(), redisProperties.port()); | ||
public RedissonClient redissonClient() { | ||
Config config = new Config(); | ||
config.useSingleServer().setAddress(REDISSON_HOST_PREFIX + redisProperties.host() + ":" + redisProperties.port()); | ||
return Redisson.create(config); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { | ||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
|
||
// Key는 String으로 저장 | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
// Value는 기본적으로 Jdk 직렬화로 저장 | ||
redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
|
||
return redisTemplate; | ||
public RedisConnectionFactory redisConnectionFactory(RedissonClient redissonClient) { | ||
return new RedissonConnectionFactory(redissonClient); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.