diff --git a/CHANGELOG.md b/CHANGELOG.md index 56a9b6796..70eddaad7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,3 +4,4 @@ - [fix:fix PolarisContextProperties instantiated twice causing NPE.](https://github.com/Tencent/spring-cloud-tencent/pull/1641) - [feat: support tsf gw.](https://github.com/Tencent/spring-cloud-tencent/pull/1645) - [feat:upgrade to 2023.0.6.](https://github.com/Tencent/spring-cloud-tencent/pull/1646) +- [feat:Add log output and test scenarios in the FaultToleranceService file.](https://github.com/Tencent/spring-cloud-tencent/pull/1652) diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java index 7a834190f..45c0c004e 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java @@ -17,8 +17,10 @@ package com.tencent.cloud.quickstart.callee; +import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.util.concurrent.TimeoutException; import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.quickstart.callee.config.DataSourceProperties; @@ -150,4 +152,10 @@ public class QuickstartCalleeController { public String faultToleranceForking() { return faultToleranceService.forking(); } + + @GetMapping("/faultTolerance/raisedException/{exceptionType}") + public String faultToleranceRaisedException(@PathVariable String exceptionType) throws IOException, TimeoutException { + return faultToleranceService.raisedException(exceptionType); + } + } diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/service/FaultToleranceService.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/service/FaultToleranceService.java index 446d53318..6fe7d3324 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/service/FaultToleranceService.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/service/FaultToleranceService.java @@ -17,10 +17,14 @@ package com.tencent.cloud.quickstart.callee.service; +import java.io.IOException; +import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; import com.tencent.cloud.plugin.faulttolerance.annotation.FaultTolerance; import com.tencent.cloud.plugin.faulttolerance.model.FaultToleranceStrategy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -32,31 +36,60 @@ import org.springframework.stereotype.Service; @Service public class FaultToleranceService { + private static final Logger LOG = LoggerFactory.getLogger(FaultToleranceService.class); + private final AtomicInteger failOverCount = new AtomicInteger(0); private final AtomicInteger forkingCount = new AtomicInteger(0); @FaultTolerance(strategy = FaultToleranceStrategy.FAIL_FAST, fallbackMethod = "fallback") public String failFast() { + LOG.info("Test failFast"); throw new RuntimeException("NO"); } public String fallback() { - return "fallback"; + return "fallback success"; } @FaultTolerance(strategy = FaultToleranceStrategy.FAIL_OVER, maxAttempts = 3) public String failOver() { + LOG.info("Test failOver"); if (failOverCount.incrementAndGet() % 4 == 0) { - return "OK"; + return "failOver success,failOverCount=" + failOverCount.get(); } throw new RuntimeException("NO"); } @FaultTolerance(strategy = FaultToleranceStrategy.FORKING, parallelism = 4) public String forking() { + LOG.info("Test forking"); if (forkingCount.incrementAndGet() % 4 == 0) { - return "OK"; + return "forking success, forkingCount=" + forkingCount.get(); } throw new RuntimeException("NO"); } + + @FaultTolerance(strategy = FaultToleranceStrategy.FAIL_OVER, maxAttempts = 3, fallbackMethod = "fallback", + ignoreExceptions = {RuntimeException.class}, raisedExceptions = {Exception.class}) + public String raisedException(String exceptionType) throws IOException, RuntimeException, TimeoutException { + switch (exceptionType) { + case "RuntimeException": + LOG.info("Test failOver for raised RuntimeException"); + throw new RuntimeException("NO"); + case "TimeOutException": + LOG.info("Test failOver for raised TimeOutException"); + if (failOverCount.incrementAndGet() % 4 == 0) { + return "failOver success,failOverCount=" + failOverCount.get(); + } + throw new TimeoutException("NO"); + default: + LOG.info("Test failOver for raised otherException"); + if (failOverCount.incrementAndGet() % 4 == 0) { + return "failOver success,failOverCount=" + failOverCount.get(); + } + throw new IOException("NO"); + } + } + + }