From 5f2ba773b6f7d6e6448fffb700d49e7af1c8fd21 Mon Sep 17 00:00:00 2001 From: atomzhong Date: Thu, 6 Apr 2023 16:50:08 +0800 Subject: [PATCH] RateLimitCaller invoke with query param and headers. --- .../service/callee/BusinessController.java | 18 ++++++++++++------ .../example/service/caller/Controller.java | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/BusinessController.java b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/BusinessController.java index 902d6681f..14852d13e 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/BusinessController.java +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/BusinessController.java @@ -63,6 +63,12 @@ public class BusinessController { @Value("${spring.application.name}") private String appName; + @Value("${server.port:0}") + private int port; + + @Value("${spring.cloud.client.ip-address:127.0.0.1}") + private String ip; + /** * Get information. * @@ -70,12 +76,12 @@ public class BusinessController { */ @GetMapping("/info") public String info() { - return "hello world for ratelimit service " + index.incrementAndGet(); + return String.format("hello world for ratelimit service %s [%s:%s] is called.", index.incrementAndGet(), ip, port); } @GetMapping("/info/webclient") public Mono infoWebClient() { - return Mono.just("hello world for ratelimit service " + index.incrementAndGet()); + return Mono.just(String.format("hello world for ratelimit service %s [%s:%s] is called.", index.incrementAndGet(), ip, port)); } @GetMapping("/invoke/webclient") @@ -93,11 +99,11 @@ public class BusinessController { .header("xxx", "xxx") .retrieve() .bodyToMono(String.class) - .doOnSuccess(s -> builder.append(s + "\n")) + .doOnSuccess(s -> builder.append(s).append("\n")) .doOnError(e -> { if (e instanceof WebClientResponseException) { if (((WebClientResponseException) e).getRawStatusCode() == 429) { - builder.append("TooManyRequests ").append(index.incrementAndGet() + "\n"); + builder.append("TooManyRequests ").append(index.incrementAndGet()).append("\n"); } } }) @@ -133,11 +139,11 @@ public class BusinessController { String.class, "yyy" ); - builder.append(entity.getBody() + "\n"); + builder.append(entity.getBody()).append("\n"); } catch (RestClientException e) { if (e instanceof TooManyRequests) { - builder.append("TooManyRequests ").append(index.incrementAndGet() + "\n"); + builder.append("TooManyRequests ").append(index.incrementAndGet()).append("\n"); } else { throw e; diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/java/com/tencent/cloud/ratelimit/example/service/caller/Controller.java b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/java/com/tencent/cloud/ratelimit/example/service/caller/Controller.java index f0026bb95..89993b2ef 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/java/com/tencent/cloud/ratelimit/example/service/caller/Controller.java +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/java/com/tencent/cloud/ratelimit/example/service/caller/Controller.java @@ -76,7 +76,7 @@ public class Controller { } @GetMapping("/invoke/webclient") - public String invokeInfoWebClient(@RequestParam String value1, @RequestParam String value2, @RequestHeader Map headers) throws InterruptedException, ExecutionException { + public String invokeInfoWebClient(@RequestParam(defaultValue = "value1") String value1, @RequestParam(defaultValue = "value1") String value2, @RequestHeader Map headers) throws InterruptedException, ExecutionException { StringBuffer builder = new StringBuffer(); WebClient webClient = webClientBuilder.baseUrl("http://" + appName).build(); @@ -123,7 +123,7 @@ public class Controller { * @throws InterruptedException exception */ @GetMapping("/invoke") - public String invokeInfo(@RequestParam String value1, @RequestParam String value2, @RequestHeader Map headers) throws InterruptedException { + public String invokeInfo(@RequestParam(defaultValue = "value1") String value1, @RequestParam(defaultValue = "value1") String value2, @RequestHeader Map headers) throws InterruptedException { StringBuffer builder = new StringBuffer(); CountDownLatch count = new CountDownLatch(30); for (int i = 0; i < 30; i++) {