如何管理 Feign 错误? [英] How to manage Feign errors?

查看:35
本文介绍了如何管理 Feign 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 Spring-bootSpring-cloudSpring-cloud-netflixSpring-cloud-feign.

We are using Spring-boot with Spring-cloud and Spring-cloud-netflix with Spring-cloud-feign.

我们正在创建网关应用程序,在 Feign 的帮助下,该应用程序将尝试与我们的 authentication 微服务通信以验证其凭据.在这里您可以看到我们 Feign authentication 客户端的示例:

We are creating our Gateway application that with the help of Feign will try to communicate with our authentication microservice in order to validate their credentials. Here you can see an example of our Feign authentication client:

@FeignClient(value="auth", configuration = AuthClientConfiguration.class)

public interface AuthClient {
   @RequestMapping(method = RequestMethod.GET, value = "/tokens", consumes = MediaType.APPLICATION_JSON_VALUE)
   Single<Session> getSession(@RequestHeader("Authorization") String token);
}

问题是,我们如何处理客户端可能提出的所有异常?我的意思是,例如,我们如何才能捕捉到 NetworkExceptionTimeoutException 已被抛出?我们已经定义了我们自己的 ErrorDecoder 但似乎这种类型的侦听器"仅在请求到达并返回响应时才起作用(在我们的例子中来自 authentication 客户端).那么,我们如何管理这些其他异常呢?

The question is, how we can deal with all the exceptions that could be raised by the client? I mean, how we can for example catch that a NetworkException or a TimeoutException has been thrown? We've defined our own ErrorDecoder but it appears that this "kind of listener" only works when the request has arrived and the response returned (in our case from authentication client). So, how we can manage this other exceptions?

最好,

推荐答案

错误解码器正在解码 HTTP 错误响应(500、404、401 等...).异常会在客户端调用中冒泡,所以使用 try/catch 应该可以工作.

Error decoders are decoding HTTP error responses (500, 404, 401, etc...). Exceptions will bubble up in client calls, so using try/catch should work.

    try {
        return client.home();
    } catch (RuntimeException e) {
        e.printStackTrace();
        throw e;
    }

这篇关于如何管理 Feign 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆