feat:Add log output and test scenarios in the FaultToleranceService file (#1652)

pull/1656/head
cooper30 2 months ago committed by GitHub
parent 5f462b0cd6
commit 0d1565fa1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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)

@ -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);
}
}

@ -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 successfailOverCount=" + 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 successfailOverCount=" + failOverCount.get();
}
throw new TimeoutException("NO");
default:
LOG.info("Test failOver for raised otherException");
if (failOverCount.incrementAndGet() % 4 == 0) {
return "failOver successfailOverCount=" + failOverCount.get();
}
throw new IOException("NO");
}
}
}

Loading…
Cancel
Save