使用 Spring Cloud Open Feign 时如何处理非 2xx 请求或获取请求头

# 简介

在构建微服务的过程中, 我们时常会需要借助 Spring Cloud Open Feign (opens new window) 组件调用第三方依赖服务.

有时, 被依赖的服务使用 REST 风格实现接口, 并且调用方需要处理非 2xx 状态的请求. 这个时候, 问题就来了.

aliyun 2020 promotion (opens new window)

# 尝试使用 ResponseEntity

熟悉 Spring MVC 的同学可能会想到 ResponseEntity (opens new window) 这个类, 并尝试定义类似以下形式的 FeignClient :

@FeignClient
public interface Dependency {

	@RequestMapping
	org.springframework.http.ResponseEntity<Object> maybeOk();
}	

然后再这样使用:

ResponseEntity<Object> response = dependency.maybeOk();
if (!response.getStatusCode().is2xxSuccessful()) {
    // do something A
} else {
    // do something B
}

但实际上, 当依赖服务返回的状态码不是 2xx 时, 业务代码块 A 会因为没有处理 FeignException 而被跳过:

[404 NOT FOUND] during [GET] to [https://httpbin.org/status/404] [Dependency#maybeOk()]: []
feign.FeignException$NotFound: [404 NOT FOUND] during [GET] to [https://httpbin.org/status/404] [Dependency#maybeOk()]: []
	at feign.FeignException.clientErrorStatus(FeignException.java:201)
	at feign.FeignException.errorStatus(FeignException.java:177)
	at feign.FeignException.errorStatus(FeignException.java:169)
	at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:92)
	at feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:96) // 关键类 AsyncResponseHandler
	at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138)
	at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89)
	at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100)
	at dev.dengchao.$Proxy180.maybeOk(Unknown Source)
	at dev.dengchao.FeignTests.test(FeignTests.java:39)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)

# 分析 AsyncResponseHandler

根据异常日志, 反向分析异常出现的原因, 可以在 AsyncResponseHandler#handleResponse() (opens new window) 方法中有所发现:

void handleResponse(CompletableFuture<Object> resultFuture,
                      String configKey,
                      Response response,
                      Type returnType,
                      long elapsedTime) {
    // copied fairly liberally from SynchronousMethodHandler
    boolean shouldClose = true;
    try {
      if (logLevel != Level.NONE) {
        response = logger.logAndRebufferResponse(configKey, logLevel, response,
            elapsedTime);
      }
      if (Response.class == returnType) {
        if (response.body() == null) {
          resultFuture.complete(response);
        } else if (response.body().length() == null
            || response.body().length() > MAX_RESPONSE_BUFFER_SIZE) {
          shouldClose = false;
          resultFuture.complete(response);
        } else {
          // Ensure the response body is disconnected
          final byte[] bodyData = Util.toByteArray(response.body().asInputStream());
          resultFuture.complete(response.toBuilder().body(bodyData).build());
        }
      } else if (response.status() >= 200 && response.status() < 300) {
        if (isVoidType(returnType)) {
          resultFuture.complete(null);
        } else {
          final Object result = decode(response, returnType);
          shouldClose = closeAfterDecode;
          resultFuture.complete(result);
        }
      } else if (decode404 && response.status() == 404 && !isVoidType(returnType)) {
        final Object result = decode(response, returnType);
        shouldClose = closeAfterDecode;
        resultFuture.complete(result);
      } else {
        resultFuture.completeExceptionally(errorDecoder.decode(configKey, response)); // 上面抛出异常的地方
      }
    } catch (final IOException e) {
      if (logLevel != Level.NONE) {
        logger.logIOException(configKey, logLevel, e, elapsedTime);
      }
      resultFuture.completeExceptionally(errorReading(response.request(), response, e));
    } catch (final Exception e) {
      resultFuture.completeExceptionally(e);
    } finally {
      if (shouldClose) {
        ensureClosed(response.body());
      }
    }
  }

通过分析, 我们可以得知由于 maybeOk() 方法返回值类型不是 Response (opens new window), 状态码非 2xx, 并且默认没有对 404 状态解码, 从而触发了异常.

# 使用 feign.Response 来处理非 2xx 响应, 获取 Header

分析出异常产生的原因后, 解法也就一目了然了.

maybeOk 方法的返回值类型修改为 feign.Response :

@FeignClient
public interface Dependency {

	@RequestMapping
	feign.Response<Object> maybeOk();
}	

然后再这样使用:

Response response = dependency.maybeOk();
if (response.status() != 200) {
	// do something A
} else {
	Map<String, Collection<String>> headers = response.headers();
    // do something B
}

这样, 业务代码块 A 就能正常的在依赖服务返回非 200 状态时被调用了.

# 参考

# 备注

演示代码是基于 Spring Cloud Hoxton SR6Spring Boot 2.2.8.RELEASE 相关类库进行展示的, 如有出入, 请自行分析.

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.FeignException;
import feign.Response;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;
import java.util.Map;

@Slf4j
@SpringBootTest
public class FeignTests {
    @Autowired
    HttpBinService service;
    @Autowired
    ObjectMapper mapper;

    @Test
    void test() throws IOException {
        Response response = service.ok();
        Assertions.assertEquals(HttpStatus.OK.value(), response.status());
        Ip ip = mapper.readValue(response.body().asInputStream(), Ip.class);
        log.info("{}", ip);

        Assertions.assertEquals(HttpStatus.NOT_FOUND.value(), service.notFound().status());

        try {
            service.maybeOk();
        } catch (Exception e) {
            Assertions.assertTrue(e instanceof FeignException);
            Assertions.assertEquals(HttpStatus.NOT_FOUND.value(), ((FeignException) e).status());
        }
    }

    @FeignClient(value = "http-bin", url = "https://httpbin.org")
    interface HttpBinService {

        @GetMapping("/ip")
        Response ok();

        @GetMapping("/status/404")
        Response notFound();

        @GetMapping("/status/404")
        ResponseEntity<Object> maybeOk();
    }

    @Data
    static class Ip {
        private String origin;
    }
}

# 推广

欢迎加入 Spring Cloud 交流群: 617143034 (opens new window)

欢迎大家点击下方的图片领取限量 阿里云优惠券 (opens new window), 新购续费更优惠: 限量阿里云优惠券 (opens new window)