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();
// return "*";
}

@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)) {
// 获取 GET 请求参数
requestParams = request.getQueryString();
} else if ("POST".equals(requestMethod)) {
// 获取 POST 请求参数
if (request.getContentType() != null &&
request.getContentType().contains("application/json")) {
// 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")) {
// Form 表单请求
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;

/**
* 过滤器类型 pre 表示在请求之前进行逻辑操作
* @return
*/
@Override
public String filterType() {
return "pre";
}

/**
* 过滤器执行顺序
* 当一个请求在同一个阶段存在多个过滤器的时候,
* 过滤器的执行顺序
* @return
*/
@Override
public int filterOrder() {
return 0;
}

/**
* 是否开启过滤
* @return
*/
@Override
public boolean shouldFilter() {
return true;
}


/**
* 编写过滤器拦截业务逻辑代码
* @return
* @throws ZuulException
*/
@Override
public Object run() throws ZuulException {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext .getRequest();

// 访问路径
String url = request.getRequestURL().toString();
return null;

}

}