【发布时间】:2020-04-20 01:52:09
【问题描述】:
我想处理来自 feign 客户端的任何异常,即使服务不可用。但是我无法使用 try/catch 捕获它们。这是我的假客户:
@FeignClient(name = "api-service", url ="localhost:8888")
public interface ClientApi extends SomeApi {
}
api 在哪里:
@Path("/")
public interface SomeApi {
@GET
@Path("test")
String getValueFromApi();
}
使用 try/catch 的客户端:
@Slf4j
@Service
@AllArgsConstructor
public class SampleController implements SomeApi {
@Autowired
private final ClientApi clientApi;
@Override
public String getValueFromApi() {
try {
return clientApi.getValueFromApi();
} catch (Throwable e) {
log.error("CAN'T CATCH");
return "";
}
}
}
依赖在版本中:
- spring-boot 2.2.2.RELEASE
- spring-cloud Hoxton.SR1
代码应该根据How to manage Feign errors?工作。
我收到了一些长堆栈跟踪,其中例外是:
- 原因:java.net.ConnectException:连接被拒绝(连接被拒绝)
- 引起:feign.RetryableException:连接被拒绝(连接被拒绝)执行GET http://localhost:8888/test
- 原因:com.netflix.hystrix.exception.HystrixRuntimeException:ClientApi#getValueFromApi() 失败并且没有可用的回退。
即使客户端服务(在本例中为 localhost:8888)不可用,如何正确捕获 Feign 异常?
附言。当 feign 客户端服务可用时,它可以工作,好的。我只关注异常方面。
【问题讨论】:
标签: spring-boot spring-cloud-feign