From e732dee412e2f8f33bc6ae61a22addc7152c1d41 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 19 Oct 2023 10:33:49 +0800 Subject: [PATCH] feat:optimize quickstart circuit-breaker examples. --- .../callee/QuickstartCalleeController.java | 6 +++ .../callee/QuickstartCalleeController.java | 38 +++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) 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 30df9f67b..072255d85 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 @@ -105,4 +105,10 @@ public class QuickstartCalleeController { LOG.info("Quickstart Callee Service [{}:{}] is called right.", ip, port); return String.format("Quickstart Callee Service [%s:%s] is called right.", ip, port); } + + @GetMapping("/faultDetect") + public String health() { + LOG.info("Quickstart Callee Service [{}:{}] is detected right.", ip, port); + return String.format("Quickstart Callee Service [%s:%s] is detected right.", ip, port); + } } 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 9d97dfd89..d4de94f3d 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 @@ -107,34 +107,56 @@ 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); } if (ifDelay) { - Thread.sleep(100); + 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); } - LOG.info("Quickstart Callee Service [{}:{}] is called wrong.", ip, port); - return new ResponseEntity<>(String.format("Quickstart Callee Service [%s:%s] is called wrong.", ip, port), 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); } @GetMapping("/setBadGateway") - public void setBadGateway(@RequestParam boolean param) { + public String setBadGateway(@RequestParam boolean param) { + this.ifBadGateway = param; if (param) { LOG.info("info is set to return HttpStatus.BAD_GATEWAY."); + return "info is set to return HttpStatus.BAD_GATEWAY."; } else { LOG.info("info is set to return HttpStatus.OK."); + return "info is set to return HttpStatus.OK."; } - this.ifBadGateway = param; } @GetMapping("/setDelay") - public void setDelay(@RequestParam boolean param) { + public String setDelay(@RequestParam boolean param) { + this.ifDelay = param; if (param) { - LOG.info("info is set to delay 100ms."); + LOG.info("info is set to delay 200ms."); + return "info is set to delay 200ms."; } else { LOG.info("info is set to no delay."); + return "info is set to no delay."; } - this.ifDelay = param; + } + + @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); + } + 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); + } + 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); } }