-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from study-hub-inu/feat/concurrency-redisson
Feat/concurrency redisson
- Loading branch information
Showing
9 changed files
with
158 additions
and
31 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
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
24 changes: 24 additions & 0 deletions
24
src/main/java/kr/co/studyhubinu/studyhubserver/common/redisson/CustomSpringELParser.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,24 @@ | ||
package kr.co.studyhubinu.studyhubserver.common.redisson; | ||
|
||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
import org.springframework.expression.spel.support.StandardEvaluationContext; | ||
|
||
@Slf4j | ||
@NoArgsConstructor | ||
public class CustomSpringELParser { | ||
public static String 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]); | ||
} | ||
Object value = parser.parseExpression(key).getValue(context, Object.class); | ||
if (value == null) { | ||
log.warn("CustomSpringELParser evaluated value is null for key={}", key); | ||
return null; | ||
} | ||
return value.toString(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/kr/co/studyhubinu/studyhubserver/common/redisson/RedissonDistributedLock.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 kr.co.studyhubinu.studyhubserver.common.redisson; | ||
|
||
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 RedissonDistributedLock { | ||
|
||
String hashKey(); | ||
String field(); | ||
} |
64 changes: 64 additions & 0 deletions
64
...ain/java/kr/co/studyhubinu/studyhubserver/common/redisson/RedissonDistributedLockAop.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,64 @@ | ||
package kr.co.studyhubinu.studyhubserver.common.redisson; | ||
|
||
import kr.co.studyhubinu.studyhubserver.exception.apply.LockAcquisitionException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
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.springframework.stereotype.Component; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Aspect | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Component | ||
public class RedissonDistributedLockAop { | ||
|
||
private final RedissonClient redissonClient; | ||
private static final int LOCK_WAIT_TIME = 10; | ||
private static final int LOCK_LEASE_TIME = 3; | ||
|
||
@Around("@annotation(kr.co.studyhubinu.studyhubserver.common.redisson.RedissonDistributedLock)") | ||
public Object around(ProceedingJoinPoint joinPoint) throws Throwable { | ||
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | ||
Method method = signature.getMethod(); | ||
RedissonDistributedLock redissonDistributedLock = method.getAnnotation(RedissonDistributedLock.class); | ||
String hashKey = getDynamicValue(signature, joinPoint, redissonDistributedLock.hashKey()); | ||
String field = getDynamicValue(signature, joinPoint, redissonDistributedLock.field()); | ||
|
||
RLock lock = redissonClient.getLock(hashKey + ":" + field); | ||
|
||
Object result; | ||
boolean available = false; | ||
try { | ||
available = lock.tryLock(LOCK_WAIT_TIME, LOCK_LEASE_TIME, TimeUnit.SECONDS); | ||
if (!available) { | ||
log.warn("Redisson GetLock Timeout {}", field); | ||
throw new LockAcquisitionException(); | ||
} | ||
|
||
result = joinPoint.proceed(); | ||
} catch (InterruptedException e) { | ||
throw new LockAcquisitionException(); | ||
} finally { | ||
if (available) { | ||
lock.unlock(); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
// 메서드 파라미터(field와 hashkey)를 기반으로 동적으로 값을 지정 | ||
public String getDynamicValue(MethodSignature signature, ProceedingJoinPoint joinPoint, String distributedLock) { | ||
return CustomSpringELParser.getDynamicValue( | ||
signature.getParameterNames(), | ||
joinPoint.getArgs(), | ||
distributedLock); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/kr/co/studyhubinu/studyhubserver/config/RedissonConfig.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,28 @@ | ||
package kr.co.studyhubinu.studyhubserver.config; | ||
|
||
import org.redisson.Redisson; | ||
import org.redisson.api.RedissonClient; | ||
import org.redisson.config.Config; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class RedissonConfig { | ||
@Value("${spring.redis.host}") | ||
private String redisHost; | ||
|
||
@Value("${spring.redis.port}") | ||
private int redisPort; | ||
|
||
private static final String REDISSON_HOST_PREFIX = "redis://"; | ||
|
||
@Bean | ||
public RedissonClient redissonClient() { | ||
RedissonClient redisson = null; | ||
Config config = new Config(); | ||
config.useSingleServer().setAddress(REDISSON_HOST_PREFIX + redisHost + ":" + redisPort); | ||
redisson = Redisson.create(config); | ||
return redisson; | ||
} | ||
} |
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
47 changes: 20 additions & 27 deletions
47
...yhubinu/studyhubserver/studypost/domain/implementations/StudyPostApplyEventPublisher.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
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