From f4d4e03cd9e9dce576f51b3d269f3963b05500c7 Mon Sep 17 00:00:00 2001 From: atomzhong <48663740+atomzhong@users.noreply.github.com> Date: Fri, 7 Apr 2023 10:09:18 +0800 Subject: [PATCH] add CustomLabelResolver to RateLimitCaller. (#951) --- CHANGELOG.md | 1 + .../service/callee/BusinessController.java | 18 ++++++--- .../service/callee/CustomLabelResolver.java | 17 ++++++++- .../callee/CustomLabelResolverReactive.java | 12 +++--- .../src/main/resources/bootstrap.yml | 3 ++ .../example/service/caller/Controller.java | 38 +++++++++++++------ .../src/main/resources/bootstrap.yml | 1 + 7 files changed, 66 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ade11558f..6544af093 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Change Log --- +- [feature: optimize polaris-ratelimit-example, add caller query params and headers, add callee custom label resolver.](https://github.com/Tencent/spring-cloud-tencent/pull/951) - [feature: add config for customized local port.](https://github.com/Tencent/spring-cloud-tencent/pull/923) - [feat:support webclient and gateway report call metrics](https://github.com/Tencent/spring-cloud-tencent/pull/924) - [feature: optimize polaris-discovery-example/discovery-callee-service, add client-ip return.](https://github.com/Tencent/spring-cloud-tencent/pull/939) 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 75cb35a40..747d657b4 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 @@ -64,18 +64,24 @@ 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. * @return information */ @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-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolver.java b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolver.java index 06ff020a2..a4bd4376e 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolver.java +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolver.java @@ -24,7 +24,10 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelServletResolver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** @@ -34,14 +37,26 @@ import org.springframework.stereotype.Component; */ @Component public class CustomLabelResolver implements PolarisRateLimiterLabelServletResolver { + private static final Logger LOG = LoggerFactory.getLogger(CustomLabelResolver.class); + @Value("${label.key-value:}") + private String[] keyValues; @Override public Map resolve(HttpServletRequest request) { // rate limit by some request params. such as query params, headers .. + return getLabels(keyValues); + } + + static Map getLabels(String[] keyValues) { Map labels = new HashMap<>(); - labels.put("user", "zhangsan"); + for (String kv : keyValues) { + String key = kv.substring(0, kv.indexOf(":")); + String value = kv.substring(kv.indexOf(":") + 1); + labels.put(key, value); + } + LOG.info("Current labels:{}", labels); return labels; } } diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolverReactive.java b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolverReactive.java index eb6e01b03..a94662388 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolverReactive.java +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/CustomLabelResolverReactive.java @@ -17,14 +17,16 @@ package com.tencent.cloud.ratelimit.example.service.callee; -import java.util.HashMap; import java.util.Map; import com.tencent.cloud.polaris.ratelimit.spi.PolarisRateLimiterLabelReactiveResolver; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; +import static com.tencent.cloud.ratelimit.example.service.callee.CustomLabelResolver.getLabels; + /** * resolver custom label from request. * @@ -32,13 +34,13 @@ import org.springframework.web.server.ServerWebExchange; */ @Component public class CustomLabelResolverReactive implements PolarisRateLimiterLabelReactiveResolver { + @Value("${label.key-value:}") + private String[] keyValues; + @Override public Map resolve(ServerWebExchange exchange) { // rate limit by some request params. such as query params, headers .. - Map labels = new HashMap<>(); - labels.put("user", "zhangsan"); - - return labels; + return getLabels(keyValues); } } diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml index 06f431b8f..67168f141 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml @@ -22,3 +22,6 @@ management: logging: level: com.tencent.cloud.polaris: debug + +label: + key-value: user:zhangsan, user2:lisi 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 291d509b2..f169e0116 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 @@ -19,10 +19,12 @@ package com.tencent.cloud.ratelimit.example.service.caller; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Consumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,7 +36,9 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.HttpClientErrorException.TooManyRequests; import org.springframework.web.client.RestClientException; @@ -55,7 +59,7 @@ public class Controller { @Autowired private WebClient.Builder webClientBuilder; - private String appName = "RateLimitCalleeService"; + private final String appName = "RateLimitCalleeService"; /** * Get information. @@ -72,25 +76,33 @@ public class Controller { } @GetMapping("/invoke/webclient") - public String invokeInfoWebClient() throws InterruptedException, ExecutionException { + public String invokeInfoWebClient(@RequestParam(defaultValue = "value1", required = false) String value1, @RequestParam(defaultValue = "value1", required = false) String value2, @RequestHeader Map headers) throws InterruptedException, ExecutionException { StringBuffer builder = new StringBuffer(); WebClient webClient = webClientBuilder.baseUrl("http://" + appName).build(); + + Consumer headersConsumer = httpHeaders -> { + for (Map.Entry entry : headers.entrySet()) { + httpHeaders.add(entry.getKey(), entry.getValue()); + } + }; + List> monoList = new ArrayList<>(); for (int i = 0; i < 30; i++) { Mono response = webClient.get() .uri(uriBuilder -> uriBuilder .path("/business/info/webclient") - .queryParam("yyy", "yyy") + .queryParam("value1", value1) + .queryParam("value2", value2) .build() ) - .header("xxx", "xxx") + .headers(headersConsumer) .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"); } } }) @@ -111,26 +123,28 @@ public class Controller { * @throws InterruptedException exception */ @GetMapping("/invoke") - public String invokeInfo() throws InterruptedException { + public String invokeInfo(@RequestParam(defaultValue = "value1", required = false) String value1, @RequestParam(defaultValue = "value1", required = false) String value2, @RequestHeader Map headers) throws InterruptedException { StringBuffer builder = new StringBuffer(); CountDownLatch count = new CountDownLatch(30); for (int i = 0; i < 30; i++) { new Thread(() -> { try { HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.add("xxx", "xxx"); + for (Map.Entry entry : headers.entrySet()) { + httpHeaders.add(entry.getKey(), entry.getValue()); + } ResponseEntity entity = restTemplate.exchange( - "http://" + appName + "/business/info?yyy={yyy}", + "http://" + appName + "/business/info?value1={value1}&value2={value2}", HttpMethod.GET, new HttpEntity<>(httpHeaders), String.class, - "yyy" + value1, value2 ); - 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/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/resources/bootstrap.yml index d977986ed..b4610df36 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-caller-service/src/main/resources/bootstrap.yml @@ -18,3 +18,4 @@ management: logging: level: com.tencent.cloud.polaris: debug +