RateLimitCaller invoke with query param and headers.

pull/953/head
atomzhong 3 years ago
parent 6202f6266d
commit 5f2ba773b6

@ -63,6 +63,12 @@ public class BusinessController {
@Value("${spring.application.name}") @Value("${spring.application.name}")
private String appName; 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. * Get information.
* *
@ -70,12 +76,12 @@ public class BusinessController {
*/ */
@GetMapping("/info") @GetMapping("/info")
public String 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") @GetMapping("/info/webclient")
public Mono<String> infoWebClient() { 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") @GetMapping("/invoke/webclient")
@ -93,11 +99,11 @@ public class BusinessController {
.header("xxx", "xxx") .header("xxx", "xxx")
.retrieve() .retrieve()
.bodyToMono(String.class) .bodyToMono(String.class)
.doOnSuccess(s -> builder.append(s + "\n")) .doOnSuccess(s -> builder.append(s).append("\n"))
.doOnError(e -> { .doOnError(e -> {
if (e instanceof WebClientResponseException) { if (e instanceof WebClientResponseException) {
if (((WebClientResponseException) e).getRawStatusCode() == 429) { 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, String.class,
"yyy" "yyy"
); );
builder.append(entity.getBody() + "\n"); builder.append(entity.getBody()).append("\n");
} }
catch (RestClientException e) { catch (RestClientException e) {
if (e instanceof TooManyRequests) { if (e instanceof TooManyRequests) {
builder.append("TooManyRequests ").append(index.incrementAndGet() + "\n"); builder.append("TooManyRequests ").append(index.incrementAndGet()).append("\n");
} }
else { else {
throw e; throw e;

@ -76,7 +76,7 @@ public class Controller {
} }
@GetMapping("/invoke/webclient") @GetMapping("/invoke/webclient")
public String invokeInfoWebClient(@RequestParam String value1, @RequestParam String value2, @RequestHeader Map<String, String> headers) throws InterruptedException, ExecutionException { public String invokeInfoWebClient(@RequestParam(defaultValue = "value1") String value1, @RequestParam(defaultValue = "value1") String value2, @RequestHeader Map<String, String> headers) throws InterruptedException, ExecutionException {
StringBuffer builder = new StringBuffer(); StringBuffer builder = new StringBuffer();
WebClient webClient = webClientBuilder.baseUrl("http://" + appName).build(); WebClient webClient = webClientBuilder.baseUrl("http://" + appName).build();
@ -123,7 +123,7 @@ public class Controller {
* @throws InterruptedException exception * @throws InterruptedException exception
*/ */
@GetMapping("/invoke") @GetMapping("/invoke")
public String invokeInfo(@RequestParam String value1, @RequestParam String value2, @RequestHeader Map<String, String> headers) throws InterruptedException { public String invokeInfo(@RequestParam(defaultValue = "value1") String value1, @RequestParam(defaultValue = "value1") String value2, @RequestHeader Map<String, String> headers) throws InterruptedException {
StringBuffer builder = new StringBuffer(); StringBuffer builder = new StringBuffer();
CountDownLatch count = new CountDownLatch(30); CountDownLatch count = new CountDownLatch(30);
for (int i = 0; i < 30; i++) { for (int i = 0; i < 30; i++) {

Loading…
Cancel
Save