Я пытаюсь реализовать обработку исключений для Feign Client вместе с Hystrix. Я использовал fallbackFactory для обработки исключений, но похоже, что поток не происходит при вызове службы.
CurrencyConversion вызывает микросервис CurrencyExchange микросервис. Если я отключаю CurrencyExchange , вызов go не откатывается, и я получаю внутреннюю ошибку сервера. Ниже приведен код моей реализации.
CurrencyConversionController
@RestController
public class CurrencyConversionController {
@Autowired
private CurrencyExchangeProxy currencyExchangeProxy;
@GetMapping("/currency-converter-feign/from/{from}/to/{to}/quantity/{quantity}")
public CurrencyConversionBean convertCurrencyFeign(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity)
{
CurrencyConversionBean response = currencyExchangeProxy.retrieveExchangeValue(from, to);
CurrencyConversionBean result = new CurrencyConversionBean(response.getId(),from,to, response.getConversionMultiple(),quantity, quantity.multiply(response.getConversionMultiple()),response.getPort());
//kafkaTemplate.send(TOPIC, result);
return result;
}
}
CurrencyExchangeProxy
@FeignClient(name = "currency-exchange-services", fallbackFactory = CurrencyConversionFallback.class)
@RibbonClient(name="currency-exchange-service")
public interface CurrencyExchangeProxy {
@GetMapping("/currency-exchange/from/{from}/to/{to}")
public CurrencyConversionBean retrieveExchangeValue(@PathVariable String from, @PathVariable String to);
}
@Component
class CurrencyConversionFallback implements FallbackFactory<CurrencyExchangeProxy> {
@Override
public CurrencyExchangeProxy create(Throwable cause) {
return new CurrencyConversionServiceFallback(cause);
}
}
class CurrencyConversionServiceFallback implements CurrencyExchangeProxy {
private final Throwable cause;
public CurrencyConversionServiceFallback(Throwable cause){
this.cause = cause;
}
@Override
public CurrencyConversionBean retrieveExchangeValue(String from, String to){
if(cause instanceof FeignException && ((FeignException) cause).status() == 404) {
System.out.println("404 error took place" + cause.getLocalizedMessage());
}
else System.out.println("Other error took place" + cause.getLocalizedMessage());
return new CurrencyConversionBean(null,from, to ,null,null,null,0);
}
}
FeignErrorDecoder
@Component
public class FeignErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
switch (response.status()) {
case 400:
break;
case 404: {
if (methodKey.contains("retrieveExchangeValue"))
return new ResponseStatusException(HttpStatus.valueOf(response.status()),
"Exchange currency not found");
break;
}
default:
return new Exception(response.reason());
}
return null;
}
}