-
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.
feat: 서킷브레이커가 열려있다면 fallback 메서드를 실행한다.
- Loading branch information
1 parent
1bfa052
commit 3859dd0
Showing
7 changed files
with
94 additions
and
9 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
37 changes: 37 additions & 0 deletions
37
.../integrated/techhub/resilience4j/circuitbreaker/aspect/WebClientCircuitBreakerAspect.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,37 @@ | ||
package com.integrated.techhub.resilience4j.circuitbreaker.aspect; | ||
|
||
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; | ||
import lombok.RequiredArgsConstructor; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
@Aspect | ||
@Component | ||
@RequiredArgsConstructor | ||
public class WebClientCircuitBreakerAspect { | ||
|
||
private final CircuitBreakerRegistry registry; | ||
|
||
@Around("execution(* org.springframework.web.reactive.function.client.WebClient.*(..)) && args(url,..)") | ||
public Object aspect(ProceedingJoinPoint pjp, String url) throws Throwable { | ||
return aspect(pjp, new URI(url)); | ||
} | ||
|
||
@Around("execution(* org.springframework.web.reactive.function.client.WebClient.*(..)) && args(uri,..)") | ||
public Object aspect(ProceedingJoinPoint pjp, URI uri) throws Throwable { | ||
return registry.circuitBreaker(findHost(uri)) | ||
.executeCheckedSupplier(pjp::proceed); | ||
} | ||
|
||
private String findHost(URI uri) { | ||
return Optional.ofNullable(uri) | ||
.map(URI::getHost) | ||
.orElse("default"); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
.../integrated/techhub/resilience4j/circuitbreaker/config/CircuitRecordFailurePredicate.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,16 @@ | ||
package com.integrated.techhub.resilience4j.circuitbreaker.config; | ||
|
||
import com.integrated.techhub.common.exception.TechHubException; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public class CircuitRecordFailurePredicate implements Predicate<Throwable> { | ||
|
||
@Override | ||
public boolean test(Throwable throwable) { | ||
if (throwable instanceof TechHubException) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...egrated/techhub/resilience4j/circuitbreaker/exception/CircuitBreakerInvalidException.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 com.integrated.techhub.resilience4j.circuitbreaker.exception; | ||
|
||
import com.integrated.techhub.common.exception.ErrorCode; | ||
import com.integrated.techhub.common.exception.TechHubException; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
public class CircuitBreakerInvalidException extends TechHubException { | ||
|
||
public CircuitBreakerInvalidException() { | ||
super(new ErrorCode(NOT_FOUND, "깃허브 서버가 불안정합니다. 다른 API를 사용해주세요")); | ||
} | ||
|
||
} |
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