- * 网关的默认回退FallbackProvider的机制只适用于通过配置文件中配置的url来跨服务调用,而不拦截通过FeignClient跨服务调用, - * 也就是说要自己实现一个FeignClient服务的fallback才可以,但是如果通过FeignClient的服务要是特别多怎么办,每一个都要写一个不得累死, - * 这里想到了通过网关的错误过滤器来统一处理FeignClient跨服务调用出错的问题 - * - * @author : qinyingjie - * @version : 2.2.0 - * @date : 2023/1/11 12:48 - */ -@Component -public class GoodsServiceFallBackProvider implements FallbackProvider { - - @Override - public String getRoute() { - return "goods-service"; - } - - @Override - public ClientHttpResponse fallbackResponse(String route, Throwable cause) { - return new ClientHttpResponse() { - - @Override - public HttpHeaders getHeaders() { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON_UTF8); - return headers; - } - - @Override - public InputStream getBody() throws IOException { - return new ByteArrayInputStream("order-service is not available!".getBytes()); - } - - @Override - public HttpStatus getStatusCode() throws IOException { - return HttpStatus.BAD_REQUEST; - } - - @Override - public int getRawStatusCode() throws IOException { - return this.getStatusCode().value(); - } - - @Override - public String getStatusText() throws IOException { - return this.getStatusCode().getReasonPhrase(); - } - - @Override - public void close() { - - } - }; - } - -} diff --git a/springcloud-service-zuul/src/main/java/com/kwan/springcloud/filter/MyZuulFilter.java b/springcloud-service-zuul/src/main/java/com/kwan/springcloud/filter/MyZuulFilter.java deleted file mode 100644 index edca4538b46ba407a6eb6ce881174dfb05bc2e34..0000000000000000000000000000000000000000 --- a/springcloud-service-zuul/src/main/java/com/kwan/springcloud/filter/MyZuulFilter.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.kwan.springcloud.filter; - -import com.netflix.zuul.ZuulFilter; -import com.netflix.zuul.context.RequestContext; -import com.netflix.zuul.exception.ZuulException; -import org.springframework.stereotype.Component; - -import javax.servlet.http.HttpServletRequest; - -@Component -public class MyZuulFilter extends ZuulFilter { - /** - * 表示当前的过滤器是否被调用 - */ - @Override - public boolean shouldFilter() { - return true; - } - - /** - * filterOrder表示执行的优先级,值越小表示优先级越高 - */ - @Override - public int filterOrder() { - return 0; - } - - /** - * pre: 在请求被路由之前调用 - * routing: 在请求被路由之中调用 - * post: 在请求被路由之后调用 - * error: 处理请求发生错误时调用 - */ - @Override - public String filterType() { - return "pre"; - } - - /** - * 真正执行Filter逻辑的方法 - */ - @Override - public Object run() throws ZuulException { - RequestContext ctx = RequestContext.getCurrentContext(); - HttpServletRequest request = ctx.getRequest(); - System.err.println("--------------uri: -------------- " + request.getRequestURI()); - return ctx; - } -} \ No newline at end of file diff --git a/springcloud-service-zuul/src/main/resources/application.yaml b/springcloud-service-zuul/src/main/resources/application.yaml index 2e0e56865b56db0335d8d44fb97a3e8d3a61a0c8..1da6d169f1f1e9cd6dfa6fb2b5ce60c3745583e8 100644 --- a/springcloud-service-zuul/src/main/resources/application.yaml +++ b/springcloud-service-zuul/src/main/resources/application.yaml @@ -11,14 +11,28 @@ zuul: sensitiveHeaders: Cookie,Set-Cookie,Authorization routes: api-portal: - path: /portal-service/** #访问路径:http:/localhost:5001/hello-service/hello + path: /portal-service/** #访问路径:http:/localhost:8888/portal-service/portal/1 serviceId: portal-service api-goods: path: /goods-service/** serviceld: goods-service + url: http://localhost:9100/,http://localhost:9200/,http://localhost:9300/ + retryable: true + semaphore: + max-semaphores: 100 + ribbon-isolation-strategy: thread +# prefix: /api + strip-prefix: false #服务提供者 eureka: client: service-url: - defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/ \ No newline at end of file + defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/ + + +ribbon: + ConnectTimeout: 250 #连接超时时间(ms),默认值为250ms + ReadTimeout: 2000 #通信超时时间(ms),默认值为2000ms + MaxAutoRetriesNextServer: 0 #对同一服务不同实例重试次数(同一服务下集群个数的重试次数) + MaxAutoRetries: 2 #对同一实例重试的次数(单个集群节点服务重试的次数) \ No newline at end of file