[Feign] 如何处理非 2xx 的响应? 如何获取 Header ?
文章目录尝试使用 ResponseEntity分析 AsyncResponseHandler使用 feign.Response 来处理非 2xx 响应, 获取 Header参考备注在构建微服务的过程中, 我们时常会需要借助 Spring Cloud Open Feign 组件调用第三方依赖服务. 有时, 被依赖的服务使用 RESTful 实现接口, 而调用方需要处理非 2xx 状态的请求. 这个时
欢迎加入 Spring cloud 交流群: 617143034
本博客站点已全量迁移至 DevDengChao 的博客 https://blog.dengchao.fun , 后续的新内容将优先在自建博客站进行发布, 欢迎大家访问.
简介
在构建微服务的过程中, 我们时常会需要借助 Spring Cloud Open Feign 组件调用第三方依赖服务. 有时, 被依赖的服务使用 RESTful 实现接口, 而调用方需要处理非 2xx 状态的请求. 这个时候, 问题来了.
尝试使用 ResponseEntity
熟悉 Spring MVC 的同学可能会想到 ResponseEntity 这个类, 并尝试定义类似以下形式的 api:
@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() 方法中有所发现:
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, 状态码非 2xx, 并且默认没有对 404 状态解码, 从而触发了异常.
使用 feign.Response 来处理非 2xx 响应, 获取 Header
分析出异常产生的原因后, 解法也就一目了然了. 将 maybeOk
方法的返回值类型修改为 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 framework @GitHub
- Spring Cloud Open Feign @GitHub
- Open Feign @GitHub
- Feign REST Client: How to get the HTTP status? @StackOverflow
备注
演示代码是基于 Spring Cloud Hoxton SR6 与 Spring 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
欢迎大家使用阿里云优惠券, 新购续费更优惠:
更多推荐
所有评论(0)