即使服务不可用,我也想处理假客户端的任何异常。但是我无法使用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 "";
}
}
}
依赖关系在版本中:
代码应根据How to manage Feign errors?起作用。
我收到了一些长堆栈跟踪,其中例外是:
- 由:java.net.ConnectException引起:连接被拒绝(连接被拒绝)
- 原因:feign.RetryableException:执行GET http://localhost:8888/test的连接被拒绝(连接被拒绝)>
- 由以下原因引起:com.netflix.hystrix.exception.HystrixRuntimeException:ClientApi#getValueFromApi()失败,没有可用的后备。
即使客户端服务(在本例中为localhost:8888)不可用,如何正确捕获假冒伪劣?
Ps。当假冒客户服务可用时,可以正常工作。我只关注异常方面。
即使服务不可用,我也想处理假客户端的任何异常。但是我无法使用try / catch来捕获它们。这是我的假客户:@FeignClient(name =“ api-service”,url =“ ...
处理您的服务不可用的情况的更好方法是使用断路器模式。幸运的是,使用Netflix Hystrix作为断路器模式的实现很容易。