add CustomLabelResolver to RateLimitCaller. (#951)

pull/960/head
atomzhong 2 years ago committed by GitHub
parent 02a9c3c2b7
commit f4d4e03cd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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)

@ -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<String> 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;

@ -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<String, String> resolve(HttpServletRequest request) {
// rate limit by some request params. such as query params, headers ..
return getLabels(keyValues);
}
static Map<String, String> getLabels(String[] keyValues) {
Map<String, String> 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;
}
}

@ -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<String, String> resolve(ServerWebExchange exchange) {
// rate limit by some request params. such as query params, headers ..
Map<String, String> labels = new HashMap<>();
labels.put("user", "zhangsan");
return labels;
return getLabels(keyValues);
}
}

@ -22,3 +22,6 @@ management:
logging:
level:
com.tencent.cloud.polaris: debug
label:
key-value: user:zhangsan, user2:lisi

@ -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<String, String> headers) throws InterruptedException, ExecutionException {
StringBuffer builder = new StringBuffer();
WebClient webClient = webClientBuilder.baseUrl("http://" + appName).build();
Consumer<HttpHeaders> headersConsumer = httpHeaders -> {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpHeaders.add(entry.getKey(), entry.getValue());
}
};
List<Mono<String>> monoList = new ArrayList<>();
for (int i = 0; i < 30; i++) {
Mono<String> 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<String, String> 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<String, String> entry : headers.entrySet()) {
httpHeaders.add(entry.getKey(), entry.getValue());
}
ResponseEntity<String> 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;

Loading…
Cancel
Save