diff --git a/CHANGELOG.md b/CHANGELOG.md index f6586a249..ee9076c62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,4 @@ - [feat:support setting load balancing strategy per service.](https://github.com/Tencent/spring-cloud-tencent/pull/1701) - [feat: support shortest response time lb and least connection lb](https://github.com/Tencent/spring-cloud-tencent/pull/1702) - [feat:support traffic mirroring.](https://github.com/Tencent/spring-cloud-tencent/pull/1703) +- [feat: support custom quickstart circuitbreak delay time.](https://github.com/Tencent/spring-cloud-tencent/pull/1704) diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java index 4f0ed5ace..618550500 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/QuickstartCalleeController.java @@ -66,7 +66,7 @@ public class QuickstartCalleeController { @Autowired private DataSourceProperties dataSourceProperties; private boolean ifBadGateway = true; - private boolean ifDelay = false; + private long delay = 0; /** * Get sum of two value. @@ -119,16 +119,19 @@ public class QuickstartCalleeController { @GetMapping("/circuitBreak") public ResponseEntity circuitBreak() throws InterruptedException { if (ifBadGateway) { - LOG.info("Quickstart Callee Service [{}:{}] is called wrong.", ip, port); - return new ResponseEntity<>("failed for call quickstart callee service.", HttpStatus.BAD_GATEWAY); + String result = String.format("Quickstart Callee Service [%s:%s] is call failed.", ip, port); + LOG.info(result); + return new ResponseEntity<>(result, HttpStatus.BAD_GATEWAY); } - if (ifDelay) { - Thread.sleep(200); - LOG.info("Quickstart Callee Service [{}:{}] is called slow.", ip, port); - return new ResponseEntity<>(String.format("Quickstart Callee Service [%s:%s] is called slow.", ip, port), HttpStatus.OK); + if (delay > 0) { + String result = String.format("Quickstart Callee Service [%s:%s] is called after %sms.", ip, port, delay); + Thread.sleep(delay); + LOG.info(result); + return new ResponseEntity<>(result, HttpStatus.OK); } - LOG.info("Quickstart Callee Service [{}:{}] is called right.", ip, port); - return new ResponseEntity<>(String.format("Quickstart Callee Service [%s:%s] is called right.", ip, port), HttpStatus.OK); + String result = String.format("Quickstart Callee Service [%s:%s] is called right.", ip, port); + LOG.info(result); + return new ResponseEntity<>(result, HttpStatus.OK); } /** @@ -139,16 +142,19 @@ public class QuickstartCalleeController { @GetMapping("/circuitBreak/wildcard/{uid}") public ResponseEntity circuitBreakWildcard(@PathVariable String uid) throws InterruptedException { if (ifBadGateway) { - LOG.info("Quickstart Callee Service with uid {} [{}:{}] is called wrong.", uid, ip, port); - return new ResponseEntity<>("failed for call quickstart callee service wildcard.", HttpStatus.BAD_GATEWAY); + String result = String.format("Quickstart Callee Service uid %s [%s:%s] call failed.", uid, ip, port); + LOG.info(result); + return new ResponseEntity<>(result, HttpStatus.BAD_GATEWAY); } - if (ifDelay) { - Thread.sleep(200); - LOG.info("Quickstart Callee Service uid {} [{}:{}] is called slow.", uid, ip, port); - return new ResponseEntity<>(String.format("Quickstart Callee Service [%s:%s] is called slow.", ip, port), HttpStatus.OK); + if (delay > 0) { + String result = String.format("Quickstart Callee Service uid %s [%s:%s] is called after %sms.", uid, ip, port, delay); + Thread.sleep(delay); + LOG.info(result); + return new ResponseEntity<>(result, HttpStatus.OK); } - LOG.info("Quickstart Callee Service uid {} [{}:{}] is called right.", uid, ip, port); - return new ResponseEntity<>(String.format("Quickstart Callee Service %s [%s:%s] is called right.", uid, ip, port), HttpStatus.OK); + String result = String.format("Quickstart Callee Service uid %s [%s:%s] is called right.", uid, ip, port); + LOG.info(result); + return new ResponseEntity<>(result, HttpStatus.OK); } @GetMapping("/setBadGateway") @@ -165,30 +171,35 @@ public class QuickstartCalleeController { } @GetMapping("/setDelay") - public String setDelay(@RequestParam boolean param) { - this.ifDelay = param; - if (param) { - LOG.info("info is set to delay 200ms."); - return "info is set to delay 200ms."; + public ResponseEntity setDelay(@RequestParam long param) { + this.delay = param; + if (param > 0) { + LOG.info("info is set to delay {}ms.", param); + return new ResponseEntity<>(String.format("info is set to delay %sms.", param), HttpStatus.OK); } - else { + if (param == 0) { LOG.info("info is set to no delay."); - return "info is set to no delay."; + return new ResponseEntity<>("info is set to no delay.", HttpStatus.OK); } + return new ResponseEntity<>("delay must be non-negative", HttpStatus.BAD_REQUEST); } + @GetMapping("/faultDetect") public ResponseEntity health() throws InterruptedException { if (ifBadGateway) { - LOG.info("Quickstart Callee Service [{}:{}] is detected wrong.", ip, port); - return new ResponseEntity<>(String.format("Quickstart Callee Service [%s:%s] is detected wrong.", ip, port), HttpStatus.BAD_GATEWAY); + String result = String.format("Quickstart Callee Service [%s:%s] call failed", ip, port); + LOG.info(result); + return new ResponseEntity<>(result, HttpStatus.BAD_GATEWAY); } - if (ifDelay) { - Thread.sleep(200); - LOG.info("Quickstart Callee Service [{}:{}] is detected slow.", ip, port); - return new ResponseEntity<>(String.format("Quickstart Callee Service [%s:%s] is detected slow.", ip, port), HttpStatus.OK); + if (delay > 0) { + Thread.sleep(delay); + String result = String.format("Quickstart Callee Service [%s:%s] is detected after %s.", ip, port, delay); + LOG.info(result); + return new ResponseEntity<>(result, HttpStatus.OK); } - LOG.info("Quickstart Callee Service [{}:{}] is detected right.", ip, port); - return new ResponseEntity<>(String.format("Quickstart Callee Service [%s:%s] is detected right.", ip, port), HttpStatus.OK); + String result = String.format("Quickstart Callee Service [%s:%s] is detected right.", ip, port); + LOG.info(result); + return new ResponseEntity<>(result, HttpStatus.OK); } }