Spring Cloud Zuul是Netflix出品的一个基于JVM路由和服务端的负载均衡器,提供动态路由,监控,弹性,安全等的边缘服务。
参数调优
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| zuul: semaphore: max-semaphores: 6000 routes: order-web: path: /order-web/** service-id: order-web cart-web: path: /cart-web/** service-id: cart-web
host: connect-timeout-millis: 10000 socket-timeout-millis: 1000 sensitive-headers:
ribbon: ReadTimeout: 3500 ConnectTimeout: 1500 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 10000
|
路由熔断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| @Component public class SvcServiceAFallback implements FallbackProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(SvcServiceAFallback.class.getName());
@Autowired private DingTalkUtils dingTalkUtils;
@Override public String getRoute() { return SvcEnum.SVC_SERVICE_A.getName();
}
@Override public ClientHttpResponse fallbackResponse(String route, Throwable throwable) { LOGGER.error("service-zuul fallback error:", throwable);
RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); String method = request.getMethod(); String requestURI = request.getRequestURI(); String requestParams = getRequestParams(request);
JSONObject jsonObject = new JSONObject(); jsonObject.put("status", 500); jsonObject.put("data", null); String message = "服务器繁忙,请稍后再试~";
String reason = ""; if (throwable != null && throwable.getCause() != null) { reason = throwable.getCause().getMessage(); }
if (throwable instanceof HystrixTimeoutException) { message = "服务器繁忙,请稍后再试"; } else { } jsonObject.put("msg", message);
return fallbackResponse(jsonObject.toJSONString()); }
private String getRequestParams(HttpServletRequest request) { String requestParams = ""; String requestMethod = request.getMethod(); if ("GET".equals(requestMethod)) { requestParams = request.getQueryString(); } else if ("POST".equals(requestMethod)) { if (request.getContentType() != null && request.getContentType().contains("application/json")) { try { java.util.Scanner s = new java.util.Scanner(request.getInputStream()).useDelimiter("\\A"); requestParams = s.hasNext() ? s.next() : ""; } catch (IOException e) { LOGGER.error("getRequestParams error:", e); } } else if (request.getContentType() != null && request.getContentType().contains("application/x-www-form-urlencoded")) { Map<String, String[]> parameterMap = request.getParameterMap(); StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) { String paramName = entry.getKey(); String[] paramValues = entry.getValue(); for (String paramValue : paramValues) { sb.append(paramName).append("=").append(paramValue).append("&"); } } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } requestParams = sb.toString(); } } return requestParams; }
private ClientHttpResponse fallbackResponse(String jsonStr) { 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(jsonStr.getBytes()); }
@Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return headers; } }; } }
|
Filter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| @Component public class TokenFilter extends ZuulFilter {
@Autowired private RedissonClient redissonClient;
@Override public String filterType() { return "pre"; }
@Override public int filterOrder() { return 0; }
@Override public boolean shouldFilter() { return true; }
@Override public Object run() throws ZuulException { RequestContext currentContext = RequestContext.getCurrentContext(); HttpServletRequest request = currentContext .getRequest();
String url = request.getRequestURL().toString(); return null;
}
}
|