Spring Boot WebClient OAuth-在同一时间命中多个请求时超时



我使用的是带有webflux和oauth2-client的spring-boot(2.4.4(,我的代码是从API服务器成功获取的,当我使用poster点击时,每次点击的响应时间约为500ms。但当我使用jMeter同时命中10-100个数据时,会导致错误超时。是我的代码错了还是我的代码中遗漏了什么?

这是我的OAuth配置:

@Bean
public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(ReactiveClientRegistrationRepository clientRegistrationRepository, ReactiveOAuth2AuthorizedClientService authorizedClientService) {
ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder().clientCredentials().authorizationCode().refreshToken().build();
AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientManager = new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(clientRegistrationRepository, authorizedClientService);
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
return authorizedClientManager;
}
@Bean
public WebClient webClient(ReactiveOAuth2AuthorizedClientManager authorizedClientManager) {
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
oauth.setDefaultClientRegistrationId("custom-registrationId");
WebClient.Builder builder = WebClient.builder();
builder.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
builder.filter(oauth);
return builder.build();
}

我的Web客户端上的实现使用了块,因为我需要在发送到客户端之前解析来自API的响应

@Autowired
private WebClient webClient;
String response = webClient.post()
.uri(URL_API)
.bodyValue(jsonRequest)
.exchangeToMono(clientResponse -> clientResponse.bodyToMono(String.class))
.timeout(Duration.ofMillis(10000))
.share().block();

问题是当我使用jMeter时。当我直接访问API服务器时,一切都是成功的,但不是通过我的应用程序

reactor.core.Exceptions$ReactiveException: java.util.concurrent.TimeoutException: Did not observe any item or terminal signal within 10000ms in 'flatMap' (and no fallback has been configured)

多亏了Toerktumlare,我学会了如何编写代码。所以我认为在这个非阻塞框架中,最好这样写,而不是阻塞((

Mono<String> response = webClient.post()
.uri(URL_API)
.bodyValue(jsonRequest)
.exchangeToMono(clientResponse -> clientResponse.bodyToMono(String.class))
.timeout(Duration.ofMillis(10000));
Mono<Object> result = response.map(res -> {
// Throw the logic parsing and other from response in here
})
return result;

相关内容

  • 没有找到相关文章