Spring WebClient:同时多个请求

时间:2020-01-17 18:04:31

标签: spring webclient spring-webflux

是否可以在Web客户端上将多个请求链接在一起?例如,我希望能够在进行交易时更新买卖双方的余额。现在,它只会更新买方余额:


    public Mono<Void> isAccountBalanceGreater(Account acc, Product prd) {


       double newBuyerBalance  =acc.getBalance() - prd.getPrice();

       Mono<Account> seller =  webClientBuilder.build().get().uri("http://account-service/user/accounts/{userId}/", prd.getProductId())
    .retrieve().bodyToMono(Account.class)
    .map(a-> new Account(a.getAccountId(),a.getOwner(),a.getPin(),a.getBalance()+prd.getPrice(),a.getUserId()));

    Account newOwnerAcc = new Account(acc.getAccountId(),acc.getOwner(),acc.getPin(),newBuyerBalance,acc.getUserId());


return webClientBuilder.build().put()
.uri("http://account-service/account/update/{accountId}",acc.getAccountId())
.body(Mono.just(newOwnerAcc),Account.class)
.retrieve().bodyToMono(Void.class);





        }

有没有一种方法可以将两个put方法一起调用,以便同时更新两个余额?

更新:此方法适用于将Mono值作为uri变量调用。

    return acc.flatMap(a->{


               UriComponents urlc = UriComponentsBuilder.fromUriString("http://account-service/account/update/{accountId}")
                       .encode().build();

               URI uri = urlc.expand(a.getAccountId()).toUri();

                return webClientBuilder.build().put()
                         .uri(uri)
                             .body(acc,Account.class).retrieve().bodyToMono(Void.class);
            });


0 个答案:

没有答案