我想知道在对要立即返回对象的方法上返回DeferredResult的方法上使用@HysterixCommand注释有什么区别吗?这是我的代码示例
@HystrixCommand(fallbackMethod = "getAlertsFallbackMethod")
public DeferredResult<List<AlertsBodyVO>> getAlerts(String userId) {
LOGGER.info("Entered getAlerts - userId:{}", userId);
final DeferredResult<List<AlertsBodyVO>> alertsDeferredResult = new DeferredResult<>();
try {
String requestURI = alertsEndpoint + userId.toUpperCase();
LOGGER.info("requestURI:{}", requestURI);
HttpHeaders headers = buildHttpHeader(userName, password);
LOGGER.info("headers:{}", headers);
HttpEntity<String> request = new HttpEntity<>(headers);
LOGGER.info("request:{}", request);
ResponseEntity<AlertsResponseVO> responseEntity = syncRestTemplate.exchange(requestURI, HttpMethod.GET, request, AlertsResponseVO.class);
LOGGER.info("responseEntity:" + responseEntity);
List<AlertsBodyVO> alertsBodyList = Arrays.asList(responseEntity.getBody().getAlertsBody());
alertsDeferredResult.setResult(alertsBodyList);
} catch (final RestClientException rce) {
LOGGER.error("ServiceImpl::getAlerts: failed for user__{}, --Cause::", userId, rce);
ErrorResponseVO alertsResponse = new ErrorResponseVO();
alertsResponse.setErrorMessage("ServiceImpl::getAlerts: failed for user__" + userId );
alertsDeferredResult.setErrorResult(alertsResponse);
}
LOGGER.info("Exiting:{}", alertsDeferredResult);
return alertsDeferredResult;
}
public DeferredResult<List<AlertsBodyVO>> getAlertsFallbackMethod(String userId) {
final DeferredResult<List<AlertsBodyVO>> alertsDeferredResult = new DeferredResult<>();
LOGGER.info("CircuitBreaker:Fallback method for getAlerts returning empty object::userId: {}",userId);
return alertsDeferredResult;
}
此行之后
ResponseEntity<AlertsResponseVO> responseEntity = syncRestTemplate.exchange(requestURI, HttpMethod.GET, request, AlertsResponseVO.class)
1秒后调用回退方法。 我在日志中看到restTemplate成功完成。
对于Hysterix-我使用的是默认设置,在其他返回对象的方法中,这对我来说效果很好。
如果任何人都可以提供有关此行为的任何见解,将不胜感激。