spring-cloud开发Greenwich版:服务网关(十四)

上文中利用zuul组件实现了微服务的路由功能,本文继续探讨其过滤器、熔断器功能
zuul过滤器分不同类型,具体有error、post、pre、route过滤器,貌似zuul组件内置实现了一些默认的过滤器

本文参考网上的文章,实现pre过滤器
MyPreFilter实现ZuulFilter接口

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
package com.ralab.ralabzuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyPreFilter extends ZuulFilter {

private static final Logger logger= LoggerFactory.getLogger(MyPreFilter.class);
@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}

@Override
public int filterOrder() {
return 0;
}

@Override
public boolean shouldFilter() {
return true;
}

@Override
public Object run() throws ZuulException {

RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletRequest request = requestContext.getRequest();

//获取请求参数
String token = request.getParameter("token");

//校验token是否可以通过
if (StringUtils.isNotBlank(token) && token.equals("maomao")) {
requestContext.setSendZuulResponse(true);
requestContext.setResponseStatusCode(200);
requestContext.set("code", 1);
} else {
requestContext.setSendZuulResponse(false);
requestContext.setResponseStatusCode(401);
HttpServletResponse response = requestContext.getResponse();
response.setHeader("content-type", "text/html;charset=utf-8");
requestContext.setResponseBody("网关认证失败,停止路由");
requestContext.set("code", 0);
}

return null;
}
}

测试http://127.0.0.1:8040/api/nlpservice/nlp/api

接下来实现fallback机制,MyFallbackProvider实现FallbackProvider接口

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
package com.ralab.ralabzuul.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@Component
public class MyFallbackProvider implements FallbackProvider {


private final Logger logger = LoggerFactory.getLogger(FallbackProvider.class);

@Override
public String getRoute() {
return "*";
}

@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
if (cause != null && cause.getCause() != null) {
String reason = cause.getCause().getMessage();
logger.info("Excption {}",reason);
}
return fallbackResponse();
}

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("当前服务貌似不可用 The service is unavailable.".getBytes());
}

@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
}

停止微服务ralab-nlp,测试http://127.0.0.1:8040/api/nlpservice/nlp/api?token=maomao

坚持原创技术分享,您的支持是我前进的动力!