繁体   English   中英

使用 CompletableFuture 异步调用 FeignClient Springboot

[英]Async call of a FeignClient Springboot with CompletableFuture

我想用 Feign 客户端调用异步 rest 端点并进行了以下更改。

调用它时 CompletableFuture.get() 没有完成。

while 一直循环...

while(!combinedFuture.isDone()) { log.info("useraccount - waiting for combinedFuture 2: " + request.toString()); }

调用API的接口:

@FeignClient(value = "clientUser", url = "http://localhost:8898/springboot", fallback = UserFallback.class)
public interface User {

@RequestMapping(method = RequestMethod.GET, value = "/user/", produces = "application/json")
@Async
CompletableFuture<UserInfo> findUserInfo(@RequestHeader(value = "Authorization", required = true) String authorizationHeader);
}

Controller 方法:

@PostMapping(value = "/springboot/useraccount/", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> userAccount(@requestbody @Valid AuthRequest request) {
log.info("useraccount - request received with request body: " + request.toString());
try {

   if (Strings.isBlank(request.getUsername()) || Strings.isBlank(request.getPassword())) {
       throw new BadRequestException("invalid username or password");
   }

   String token = authorisationService.obtainAuthToken(request.getUsername(), request.getPassword());

   CompletableFuture<UserInfo> userInfo = clientUser.findUserInfo(token);

   CompletableFuture<UserAccountInfo> userAccountInfo = clientAccount.findAccountInfo(token);

   CompletableFuture<Void> combinedFuture
           = CompletableFuture.allOf(userInfo, userAccountInfo);

   while(!combinedFuture.isDone()) {
       log.info("useraccount - waiting for combinedFuture 2: " + request.toString());
   }
   Optional<UserAccountResponse> userAccountResponse = userAccountService.getAccountInfo(
           userAccountInfo.get(), userInfo.get()
   );


   if (userAccountResponse.isEmpty()) {
       throw new BadCredentialsException("Bad Credentials");
   }

   return ResponseEntity.ok().body(userAccountResponse);
   } catch (BadCredentialsException | UnAuthorizedException ex) {
   return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
   } catch (BadRequestException ex) {
   return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
   } catch (ExecutionException e) {
   return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
   } catch (InterruptedException e) {
   return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
   }
}

(更新)已更改为使用 CompletableFuture.supplyAsync

但现在 object 总是 null...

使用 CompletableFuture.supplyAsync

@Service
public class AccountService {

@Autowired
Account accountClient;

@Async
public Optional<UserAccountInfo> getAccountInfo(String token) {
    return Optional.of(accountClient.findAccountInfo(token));
}

}

设法像这样解决它:

 @Async
public CompletableFuture<UserAccountInfo> getAccountInfo(String token)   {
    try {
        System.out.println(
                "Current Thread account Name: "
                        + Thread.currentThread().getName());
        Thread.currentThread().sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return    CompletableFuture.completedFuture(accountClient.findAccountInfo(token).getBody());
}

userInfo 服务也是如此。 然后在 controller 上:

CompletableFuture<UserAccountInfo> userAccountInfo = accountService.getAccountInfo(token);
       CompletableFuture<UserInfo> userInfo = userService.getUserInfo(token);

       Optional<UserAccountResponse> userAccountResponse = userAccountService.getAccountInfo(
               userAccountInfo.get(),userInfo.get()
       );

所以这意味着两个服务将开始在一个新线程中运行,而主线程继续运行直到找到 first.get()。

通过这样做,完成的最大等待时间是线程花费更多时间的时间,而不是两者的总和,因为它是同步的。

谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM