【问题标题】:Feign Client Error HandlingFeign 客户端错误处理
【发布时间】:2018-03-21 14:19:19
【问题描述】:

我正在使用 Feign 客户端,

我有定位服务。所以我使用 FeignClient 为我的 LocationService 创建了一个客户端。

@FeignClient(url="http://localhost:9003/location/v1", name="location-service")
public interface LocationControllerVOneClient {

    @RequestMapping(value = "/getMultipleLocalities", method = RequestMethod.POST)
    public Response<Map<Integer, Locality>> getMultipleLocalities(List<Integer> localityIds);

    @RequestMapping(value = "/getMultipleCities", method = RequestMethod.POST)
    public Response<Map<Integer, City>> getMultipleCities(List<Integer> cityIds);

    @RequestMapping(value = "/getMultipleStates", method = RequestMethod.POST)
    public Response<Map<Integer, State>> getMultipleStates(List<Integer> stateIds);

    @RequestMapping(value = "/getMultipleCitiesName", method = RequestMethod.POST)
    public Response<Map<Integer, String>> getMultipleCitiesName(MultiValueMap<String, String> formParams);

    @RequestMapping(value = "/getMultipleStatesName", method = RequestMethod.POST)
    public Response<Map<Integer, String>> getMultipleStatesName(MultiValueMap<String, String> formParams);

    @RequestMapping(value = "/getMultipleLocalitiesName", method = RequestMethod.POST)
    public Response<Map<Integer, String>> getMultipleLocalitiesName(MultiValueMap<String, String> formParams);

}

现在其他服务可能会通过 LocationClient 调用此 LocationService。

我想在一个共同的地方为这个 Feign Client(LocationClient) 做异常处理(即我只是不希望每个调用者都这样做。这应该是 LocationClient 的一部分)。异常可能是连接被拒绝(如果 LocationService 已关闭)、超时等。

【问题讨论】:

    标签: spring-boot spring-cloud-feign


    【解决方案1】:

    您可以定义一个备用客户端,当超时或连接被拒绝等异常出现时调用:

    @FeignClient(url="http://localhost:9003/location/v1", name="location-service", fallback=LocationFallbackClient.class)
    public interface LocationControllerVOneClient {
        ...
    }
    

    LocationFallbackClient 必须实现 LocationControllerVOneClient

    【讨论】:

      【解决方案2】:

      您可以使用 feign ErrorDecoder 进行异常处理。以下是供您参考的网址。

      https://github.com/OpenFeign/feign/wiki/Custom-error-handling

      例子:

      public class MyErrorDecoder implements ErrorDecoder {
      
          private final ErrorDecoder defaultErrorDecoder = new Default();
      
          @Override
          public Exception decode(String methodKey, Response response) {
              if (response.status() >= 400 && response.status() <= 499) {
                  return new MyBadRequestException();
              }
              return defaultErrorDecoder.decode(methodKey, response);
          }
      
      }
      

      要获得此 ErrorDecoder,您需要为它创建一个 bean,如下所示:

      @Bean
      public MyErrorDecoder myErrorDecoder() {
        return new MyErrorDecoder();
      }
      

      【讨论】:

      • bean的名字应该怎么取?谁会根据名字得到豆子?我只看到使用openfeign 的方法,而不是把 Spring 的客户端伪装成 bean
      猜你喜欢
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 2021-10-12
      • 2019-06-09
      • 2017-09-14
      相关资源
      最近更新 更多