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