0604-Zuul构建API Gateway-Zuul的回退
Posted 木子旭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了0604-Zuul构建API Gateway-Zuul的回退相关的知识,希望对你有一定的参考价值。
一、概述
Zuul本事是基于Hystrix和Ribbon等,默认会支持Hystrix的Command等
在Zuul给定的路由回路跳闸,可以通过创建一个类型为ZuulFallbackProvider的bean来提供回退响应。在此bean中,您需要指定回退的路由ID,并提供ClientHttpResponse作为回退。这是一个非常简单的ZuulFallbackProvider实现。
二、提供Hystrix路由回退
当Zuul中给定路由的电路被触发时,您可以通过创建一个类型为ZuulFallbackProvider的bean来提供回退响应。在此bean中,您需要指定回退的路由ID,并提供ClientHttpResponse作为回退。这是一个非常简单的ZuulFallbackProvider实现。
class MyFallbackProvider implements ZuulFallbackProvider { @Override public String getRoute() { return "customers"; } @Override public ClientHttpResponse fallbackResponse() { return new ClientHttpResponse() { @Override public HttpStatus getStatusCode() throws IOException { return HttpStatus.OK; } @Override public int getRawStatusCode() throws IOException { return 200; } @Override public String getStatusText() throws IOException { return "OK"; } @Override public void close() { } @Override public InputStream getBody() throws IOException { return new ByteArrayInputStream("fallback".getBytes()); } @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return headers; } }; } }
路由配置
zuul: routes: customers: /customers/**
如果您希望为所有路由提供默认回退,则可以创建类型为ZuulFallbackProvider的bean,并让getRoute方法返回*或null
class MyFallbackProvider implements ZuulFallbackProvider { @Override public String getRoute() { return "*"; } @Override public ClientHttpResponse fallbackResponse() { return new ClientHttpResponse() { @Override public HttpStatus getStatusCode() throws IOException { return HttpStatus.OK; } @Override public int getRawStatusCode() throws IOException { return 200; } @Override public String getStatusText() throws IOException { return "OK"; } @Override public void close() { } @Override public InputStream getBody() throws IOException { return new ByteArrayInputStream("fallback".getBytes()); } @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return headers; } }; } }
如果您想选择基于故障原因的响应,请使用FallbackProvider,它将替换未来版本中的ZuulFallbackProvder。
class MyFallbackProvider implements FallbackProvider { @Override public String getRoute() { return "*"; } @Override public ClientHttpResponse fallbackResponse(final Throwable cause) { if (cause instanceof HystrixTimeoutException) { return response(HttpStatus.GATEWAY_TIMEOUT); } else { return fallbackResponse(); } } @Override public ClientHttpResponse fallbackResponse() { return response(HttpStatus.INTERNAL_SERVER_ERROR); } private ClientHttpResponse response(final HttpStatus status) { return new ClientHttpResponse() { @Override public HttpStatus getStatusCode() throws IOException { return status; } @Override public int getRawStatusCode() throws IOException { return status.value(); } @Override public String getStatusText() throws IOException { return status.getReasonPhrase(); } @Override public void close() { } @Override public InputStream getBody() throws IOException { return new ByteArrayInputStream("fallback".getBytes()); } @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return headers; } }; } }
都是
以上是关于0604-Zuul构建API Gateway-Zuul的回退的主要内容,如果未能解决你的问题,请参考以下文章
如何构建一个 REST API,该 API 采用资源 id 的数组
如何使用 api-platform 构建 GraphQL API?