Pra*_*ath 6 java spring-boot spring-webflux
我正在使用 spring-boot 版本 2.0.0.M6。我需要从 spring-boot 应用程序(例如 APP1)到另一个应用程序(播放框架)(例如 APP2)进行异步 HTTP 调用。因此,如果我需要从 APP1 到 APP2 进行 20 个不同的异步调用,APP2 会收到 20 个请求,其中很少是重复的,这意味着这些重复替换了几个不同的请求。预期的:
api/v1/call/1
api/v1/call/2
api/v1/call/3
api/v1/call/4
Run Code Online (Sandbox Code Playgroud)
实际的:
api/v1/call/1
api/v1/call/2
api/v1/call/4
api/v1/call/4
Run Code Online (Sandbox Code Playgroud)
我正在使用 spring 反应式 WebClient。
下面是 build.gradle 中的 spring boot 版本
buildscript {
ext {
springBootVersion = '2.0.0.M6'
//springBootVersion = '2.0.0.BUILD-SNAPSHOT'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven {url "https://plugins.gradle.org/m2/"}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("se.transmode.gradle:gradle-docker:1.2")
}
}
Run Code Online (Sandbox Code Playgroud)
我的 WebClient 初始化代码段
private WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector((HttpClientOptions.Builder builder) -> builder.disablePool()))
.build();
Run Code Online (Sandbox Code Playgroud)
我的POST方法
public <T> Mono<JsonNode> postClient(String url, T postData) {
return Mono.subscriberContext().flatMap(ctx -> {
String cookieString = ctx.getOrDefault(Constants.SubscriberContextConstnats.COOKIES, StringUtils.EMPTY);
URI uri = URI.create(url);
return webClient.post().uri(uri).body(BodyInserters.fromObject(postData)).header(HttpHeaders.COOKIE, cookieString)
.exchange().flatMap(clientResponse ->
{
return clientResponse.bodyToMono(JsonNode.class);
})
.onErrorMap(err -> new TurtleException(err.getMessage(), err))
.doOnSuccess(jsonData -> {
});
});
}
Run Code Online (Sandbox Code Playgroud)
调用此 postClient 方法的代码
private void getResultByKey(PremiumRequestHandler request, String key, BrokerConfig brokerConfig) {
/* Live calls for the insurers */
LOG.info("[PREMIUM SERVICE] LIVE CALLLLL MADE FOR: " + key + " AND REQUEST ID: " + request.getRequestId());
String uri = brokerConfig.getHostUrl() + verticalResolver.determineResultUrl(request.getVertical()) + key;
LOG.info("[PREMIUM SERVICE] LIVE CALL WITH URI : " + uri + " FOR REQUEST ID: " + request.getRequestId());
Mono<PremiumResponse> premiumResponse = reactiveWebClient.postClient(uri, request.getPremiumRequest())
.map(json -> PlatformUtils.mapToClass(json, PremiumResponse.class));
premiumResponse.subscribe(resp -> {
resp.getPremiumResults().forEach(result -> {
LOG.info("Key " + result.getKey());
repository.getResultRepoRawType(request.getVertical())
.save(result).subscribe();
saveResult.subscriberContext(ctx -> {
MultiBrokerMongoDBFactory.setDatabaseNameForCurrentThread(brokerConfig.getBroker());
return ctx;
}).subscribe();
});
}, error -> {
LOG.info("[PREMIUM SERVICE] ERROR RECIEVED FOR " + key + " AND REQUEST ID" + request.getRequestId() + " > " + error.getMessage());
});
}
Run Code Online (Sandbox Code Playgroud)
已经把日志放在客户端代码的端点,在那个点看不到多个请求。
可能是 WebClient 中的一个错误,其中 URI 在多线程环境中被交换。
尝试改变 WebClient,URI 仍然被交换
请帮忙。
Git Repo 添加了github.com/praveenk007/ps-demo
添加我的一些观察:
webClient.get()或者webClient.post()总是DefaultRequestBodyUriSpec在每次调用时返回 new URI,我认为它看起来不像URI被交换。
class DefaultWebClient implements WebClient {
..
@Override
public RequestHeadersUriSpec<?> get() {
return methodInternal(HttpMethod.GET);
}
@Override
public RequestBodyUriSpec post() {
return methodInternal(HttpMethod.POST);
}
..
@Override
public Mono<ClientResponse> exchange() {
ClientRequest request = (this.inserter != null ?
initRequestBuilder().body(this.inserter).build() :
initRequestBuilder().build());
return exchangeFunction.exchange(request).switchIfEmpty(NO_HTTP_CLIENT_RESPONSE_ERROR);
}
private ClientRequest.Builder initRequestBuilder() {
URI uri = (this.uri != null ? this.uri : uriBuilderFactory.expand(""));
return ClientRequest.create(this.httpMethod, uri)
.headers(headers -> headers.addAll(initHeaders()))
.cookies(cookies -> cookies.addAll(initCookies()))
.attributes(attributes -> attributes.putAll(this.attributes));
}
..
}
Run Code Online (Sandbox Code Playgroud)
方法methodInternal如下所示
@SuppressWarnings("unchecked")
private RequestBodyUriSpec methodInternal(HttpMethod httpMethod) {
return new DefaultRequestBodyUriSpec(httpMethod);
}
Run Code Online (Sandbox Code Playgroud)
此外,在提出实际请求时,ClientRequest也会创建新的。
类源
| 归档时间: |
|
| 查看次数: |
2343 次 |
| 最近记录: |