-
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.
- Redis & Redisson 적용 - 분산락 aop로 적용 - 통합테스트 변경
- Loading branch information
1 parent
4a0bcc2
commit 0e8acf5
Showing
9 changed files
with
70 additions
and
4 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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/kpop/ticketing/domain/common/annotation/DistributedLock.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,11 @@ | ||
package com.kpop.ticketing.domain.common.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 DistributedLock { | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/kpop/ticketing/domain/common/aspect/DistributedLockAspect.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,38 @@ | ||
package com.kpop.ticketing.domain.common.aspect; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.redisson.api.RLock; | ||
import org.redisson.api.RedissonClient; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Aspect | ||
@Order(value = 1) | ||
public class DistributedLockAspect { | ||
|
||
private final RedissonClient redissonClient; | ||
|
||
@Around("@annotation(com.kpop.ticketing.domain.common.annotation.DistributedLock)") | ||
public Object lock(final ProceedingJoinPoint joinPoint) throws Throwable { | ||
// joinPoint는 호출된 메서드의 인자 정보를 가지고 있음 | ||
Long seatId = (Long) joinPoint.getArgs()[1]; | ||
RLock rLock = redissonClient.getLock(String.format("lock:%s", seatId)); | ||
|
||
try { | ||
if (!rLock.tryLock()) { | ||
return false; | ||
} | ||
Object returnValue = joinPoint.proceed(); | ||
rLock.unlock(); | ||
return returnValue; | ||
} catch (final Exception e) { | ||
Thread.currentThread().interrupt(); | ||
throw new InterruptedException(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,7 @@ spring: | |
format_sql: true | ||
show_sql: true | ||
open-in-view: false | ||
data: | ||
redis: | ||
host: localhost | ||
port: 6379 |
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 |
---|---|---|
|
@@ -16,3 +16,7 @@ spring: | |
format_sql: true | ||
show_sql: true | ||
open-in-view: false | ||
data: | ||
redis: | ||
host: localhost | ||
port: 6379 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,7 @@ spring: | |
database-platform: H2 | ||
hibernate: | ||
ddl-auto: update | ||
data: | ||
redis: | ||
host: localhost | ||
port: 6379 |