-
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 #27 from tswlun002/feature/add-circuit-breaker-pat…
…tern-for-services Feature/add circuit breaker pattern for services
- Loading branch information
Showing
19 changed files
with
335 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,4 @@ download: | |
message: | ||
successful: "Document is uploaded." | ||
fail: "Failed to upload document." | ||
|
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,2 +1,10 @@ | ||
server: | ||
port: 8072 | ||
port: 8072 | ||
resilience4j.circuitbreaker: | ||
configs: | ||
default: | ||
register-health-indicator: true | ||
sliding-window-size: 5 | ||
wait-duration-in-open-state: 20000 | ||
permitted-number-of-calls-in-half-open-state: 3 | ||
failure-rate-threshold: 60 |
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
17 changes: 15 additions & 2 deletions
17
document-service/src/main/java/com/documentservice/exception/AppException.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,13 +1,26 @@ | ||
package com.documentservice.exception; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.Builder; | ||
|
||
import java.io.Serializable; | ||
|
||
@Builder | ||
public record AppException( | ||
|
||
public record AppException ( | ||
String statusCodeMessage, | ||
String message, | ||
String path, | ||
String timestamp, | ||
int status | ||
) { | ||
) implements Serializable{ | ||
|
||
public String toJson() throws JsonProcessingException { | ||
ObjectMapper objectMapper = new ObjectMapper() | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); | ||
return objectMapper.writeValueAsString(this); | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
document-service/src/main/java/com/documentservice/exception/UserServiceException.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,7 @@ | ||
package com.documentservice.exception; | ||
|
||
public class UserServiceException extends RuntimeException { | ||
public UserServiceException(String message) { | ||
super(message); | ||
} | ||
} |
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
38 changes: 34 additions & 4 deletions
38
document-service/src/main/java/com/documentservice/user/UserApi.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,14 +1,44 @@ | ||
package com.documentservice.user; | ||
|
||
import com.documentservice.exception.InvalidUser; | ||
import com.documentservice.exception.InternalServerError; | ||
import feign.RetryableException; | ||
import io.github.resilience4j.circuitbreaker.CallNotPermittedException; | ||
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; | ||
import io.github.resilience4j.retry.annotation.Retry; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
|
||
@FeignClient("gatewayserver") | ||
import java.util.concurrent.TimeoutException; | ||
|
||
@FeignClient(value = "users") | ||
public interface UserApi { | ||
@GetMapping(value = "/pdf-editor/users/{username}") | ||
ResponseEntity<UserDto> getUser(@RequestHeader("trace-Id") String traceId, @PathVariable("username") String username) throws InvalidUser; | ||
@CircuitBreaker(name = "users", fallbackMethod = "getUserFallback") | ||
@Retry(name = "users") | ||
@GetMapping(value = "/pdf-editor/users/{username}",produces = MediaType.APPLICATION_JSON_VALUE) | ||
ResponseEntity<?> getUser(@RequestHeader("trace-Id") String traceId, @PathVariable("username") String username); | ||
default ResponseEntity<String> getUserFallback( @RequestHeader("trace-Id") String traceId, @PathVariable("username") String username,TimeoutException ex){ | ||
final Logger logger = LoggerFactory.getLogger(UserApi.class); | ||
logger.error("User service is not available, error: {}, trace-id:{}",ex.getCause(),traceId,ex); | ||
throw new InternalServerError( "Request timed out, please try again sometime later."); | ||
} | ||
default ResponseEntity<String> getUserFallback(@RequestHeader("trace-Id") String traceId, @PathVariable("username") String username, RetryableException ex) { | ||
final Logger logger = LoggerFactory.getLogger(UserApi.class); | ||
logger.error("User service is not available, error: {}, trace-id:{}",ex,traceId); | ||
throw new InternalServerError("Service is down, please try again sometime later."); | ||
|
||
} | ||
default ResponseEntity<String> getUserFallback( @RequestHeader("trace-Id") String traceId, @PathVariable("username") String username,CallNotPermittedException ex){ | ||
final Logger logger = LoggerFactory.getLogger(UserApi.class); | ||
logger.error("User service is not available, error: {}, trace-id:{}",ex.getCause(),traceId,ex); | ||
var string = String.format("Hello %s!, we notice your request please try sometime later or contact the support team ", username); | ||
throw new InternalServerError(string); | ||
|
||
} | ||
|
||
} |
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.