本文共8613个字(含代码),阅读时间预计20分钟,请知悉
目录
一、背景
二、简述
1、简介
2、几个核心概念
3、工作原理
三、基本功能使用
1、路由
2、断言
3、过滤器
四、进阶使用
1、定制化globalFilter
2、重写请求报文
3、重写响应报文
4、添加安全验签
5、节点分组
按照惯例,我们先唠叨几句写本篇文章的初衷。近期针对springcloud gateway进行了较多的定制化开发,包括针对网关实例的分组、定制请求报文转换和相应报文转换的过滤器,全局过滤器逻辑调整等等。每家公司针对网关这套应用几乎不会有什么太大的改动,能够修改这块应用的机会少之又少,借由这个机缘,在开发内容投产之后,决定针对网关应用进行相对完整的学习。中间会穿插一些个人的思考,如果有什么不正确的地方或者改进意见,欢迎大家联系我,我们共同进步!
springcloud gateway 是springcloud生态体系的第二代网关,(以下简称GW)其目标主要是替代Netflix zuul,GW不仅提供了统一的路由方式,还基于filter链,提供了各种丰富多样的功能,如限流、监控、安全、协议转发等。
1)路由(route):路由是网关最基础的部分,路由信息由一个ID、一个目的url、一组断言工厂和一组 Filter组成。如果路由断言为真,则说明请求的url和配置的路由匹配。
2)断言(Predicate):Java8中的断言函数。Spring Cloud Gateway中的断言函数输入类型是Spring5.0框架中的ServerWebExchange。Spring Cloud Gateway中的断言函数允许开发者去定义匹配来自于Http Request中的任何信息,比如请求头和参数等。
3)过滤器filter):一个标准的Spring webFilter。Spring Cloud Gateway中的Filter分为两种类型的Filter,分别是Gateway Filter和Global Filter。过滤器Filter将会对请求和响应进行修改处理。
4)过滤器链(filter chain):由一个或者多个filter组成的链式结构,按照filter设置的order顺序依次执行。
客户端向Spring Cloud Gateway 发起请求,请求首先会被HttpWebHandlerAdapter进行提取组装成网关的上下文,然后网关的上下文会传递到DispatcherHandler。DispatcherHandler是所有请求的分发处理器,DispatcherHandler主要负责分发请求对应的处理器,比如将请求分发到对应RoutePredicate-
HandlerMapping(路由断言处理映射器)。路由断言处理映射器主要用于路由的查找,以及找到路由后返回对应的FilteringWebHandler。FilteringWebHandler主要负责组装Filter链表并调用Filter执行一系列的Filter处理,然后把请求转到后端对应的代理服务处理,处理完毕之后,将Response 返回到客户端。
结合断言的配置,可以定制化任何指定路由转发的的规则,既可以手动创建RouteLocator,也可以直接在配置文件中编写,如下:
spring:
cloud:
gateway:
routes:
- id: customRoute
uri: www.venies.test.com.cn/
predicates:
- Path: /test
断言在gw的源码当中,有丰富的可选项,常用的有Header、Path,当然也支持定制化,只要继承AbstractRoutePredicateFactory 并定制个性化的逻辑即可。
附上源码中相关的可选断言:
- AbstractRoutePredicateFactory
- AfterRoutePredicateFactory
- BeforeRoutePredicateFactory
- BetweenRoutePredicateFactory
- CloudFoundryRouteServiceRoutePredicateFactory
- CookieRoutePredicateFactory
- HeaderRoutePredicateFactory
- HostRoutePredicateFactory
- MethodRoutePredicateFactory
- PathRoutePredicateFactory
- QueryRoutePredicateFactory
- ReadBodyRoutePredicateFactory
- RemoteAddrRoutePredicateFactory
- WeightRoutePredicateFactory
- XForwardedRemoteAddrRoutePredicateFactory
针对请求和相应进行处理,在gw的源码当中,预置了更加丰富的的可选项,以下列举几个经常会被我们用到的过滤器,例如:
- AddRequestHeaderGatewayFilterFactory
- AddRequestParameterGatewayFilterFactory
- AddResponseHeaderGatewayFilterFactory
- PrefixPathGatewayFilterFactory
- RequestHeaderToRequestUriGatewayFilterFactory
- StripPrefixGatewayFilterFactory
- RewritePathGatewayFilterFactory
其中StripPrefixGatewayFilterFactory这个常常会被我们用作去除前缀,即指定去除请求中的指定数量的前缀后再进行转发,RewritePathGatewayFilterFactory这个常常用作重写转发的uri地址,类似于zuul中的StripPrefix功能。
待补充
阅读gw的源码,发现在rewrite目录下,ModifyRequestBodyGatewayFilterFactory和RewriteFunction可以指导我们去进行重写请求报文的功能实现,再结合断言路由的强大功能,完全可以高效快速的选择需要被重写的请求。
@Override
@SuppressWarnings("unchecked")
public GatewayFilter apply(Config config) {
return new GatewayFilter() {
@Override
public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
Class inClass = config.getInClass();
ServerRequest serverRequest = ServerRequest.create(exchange, messageReaders);
// TODO: 主要可以重写这里的逻辑,针对请求头、请求体中的内容进行定制化
Mono modifiedBody = serverRequest.bodyToMono(inClass)
.flatMap(originalBody -> config.getRewriteFunction().apply(exchange, originalBody))
.switchIfEmpty(Mono.defer(() -> (Mono) config.getRewriteFunction().apply(exchange, null)));
BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, config.getOutClass());
HttpHeaders headers = new HttpHeaders();
headers.putAll(exchange.getRequest().getHeaders());
// the new content type will be computed by bodyInserter
// and then set in the request decorator
headers.remove(HttpHeaders.CONTENT_LENGTH);
// if the body is changing content types, set it here, to the bodyInserter
// will know about it
// 注意在处理的过程中要进行异常的捕获或者全局处理,提升体验以及一些不必要的麻烦
if (config.getContentType() != null) {
headers.set(HttpHeaders.CONTENT_TYPE, config.getContentType());
}
CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange, headers);
return bodyInserter.insert(outputMessage, new BodyInserterContext())
// .log("modify_request", Level.INFO)
.then(Mono.defer(() -> {
ServerHttpRequest decorator = decorate(exchange, headers, outputMessage);
return chain.filter(exchange.mutate().request(decorator).build());
})).onErrorResume((Function>) throwable -> release(exchange,
outputMessage, throwable));
}
@Override
public String toString() {
return filterToStringCreator(ModifyRequestBodyGatewayFilterFactory.this)
.append("Content type", config.getContentType()).append("In class", config.getInClass())
.append("Out class", config.getOutClass()).toString();
}
};
}
有了上述过程的请求重写,相应报文重写自然不在话下,瞄准关键类ModifyResponseBodyGatewayFilterFactory
// 针对响应的重写,依旧是要注意对于路由的选择和把控,同时注意性能的影响,重写后的响应头长度等等,注意对于异常的捕获和处理
protected class ModifiedServerHttpResponse extends ServerHttpResponseDecorator {
private final ServerWebExchange exchange;
private final Config config;
public ModifiedServerHttpResponse(ServerWebExchange exchange, Config config) {
super(exchange.getResponse());
this.exchange = exchange;
this.config = config;
}
@SuppressWarnings("unchecked")
@Override
public Mono writeWith(Publisher body) {
Class inClass = config.getInClass();
Class outClass = config.getOutClass();
String originalResponseContentType = exchange.getAttribute(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR);
HttpHeaders httpHeaders = new HttpHeaders();
// explicitly add it in this way instead of
// 'httpHeaders.setContentType(originalResponseContentType)'
// this will prevent exception in case of using non-standard media
// types like "Content-Type: image"
httpHeaders.add(HttpHeaders.CONTENT_TYPE, originalResponseContentType);
ClientResponse clientResponse = prepareClientResponse(body, httpHeaders);
// TODO: flux or mono
Mono modifiedBody = extractBody(exchange, clientResponse, inClass)
.flatMap(originalBody -> config.getRewriteFunction().apply(exchange, originalBody))
.switchIfEmpty(Mono.defer(() -> (Mono) config.getRewriteFunction().apply(exchange, null)));
BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, outClass);
CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange,
exchange.getResponse().getHeaders());
return bodyInserter.insert(outputMessage, new BodyInserterContext()).then(Mono.defer(() -> {
Mono messageBody = writeBody(getDelegate(), outputMessage, outClass);
HttpHeaders headers = getDelegate().getHeaders();
if (!headers.containsKey(HttpHeaders.TRANSFER_ENCODING)
|| headers.containsKey(HttpHeaders.CONTENT_LENGTH)) {
messageBody = messageBody.doOnNext(data -> headers.setContentLength(data.readableByteCount()));
}
// TODO: fail if isStreamingMediaType?
return getDelegate().writeWith(messageBody);
}));
}
@Override
public Mono writeAndFlushWith(Publisher> body) {
return writeWith(Flux.from(body).flatMapSequential(p -> p));
}
private ClientResponse prepareClientResponse(Publisher body, HttpHeaders httpHeaders) {
ClientResponse.Builder builder;
builder = ClientResponse.create(exchange.getResponse().getStatusCode(), messageReaders);
return builder.headers(headers -> headers.putAll(httpHeaders)).body(Flux.from(body)).build();
}
private Mono extractBody(ServerWebExchange exchange, ClientResponse clientResponse, Class inClass) {
// if inClass is byte[] then just return body, otherwise check if
// decoding required
if (byte[].class.isAssignableFrom(inClass)) {
return clientResponse.bodyToMono(inClass);
}
List encodingHeaders = exchange.getResponse().getHeaders().getOrEmpty(HttpHeaders.CONTENT_ENCODING);
for (String encoding : encodingHeaders) {
MessageBodyDecoder decoder = messageBodyDecoders.get(encoding);
if (decoder != null) {
return clientResponse.bodyToMono(byte[].class).publishOn(Schedulers.parallel()).map(decoder::decode)
.map(bytes -> exchange.getResponse().bufferFactory().wrap(bytes))
.map(buffer -> prepareClientResponse(Mono.just(buffer),
exchange.getResponse().getHeaders()))
.flatMap(response -> response.bodyToMono(inClass));
}
}
return clientResponse.bodyToMono(inClass);
}
private Mono writeBody(ServerHttpResponse httpResponse, CachedBodyOutputMessage message,
Class outClass) {
Mono response = DataBufferUtils.join(message.getBody());
if (byte[].class.isAssignableFrom(outClass)) {
return response;
}
List encodingHeaders = httpResponse.getHeaders().getOrEmpty(HttpHeaders.CONTENT_ENCODING);
for (String encoding : encodingHeaders) {
MessageBodyEncoder encoder = messageBodyEncoders.get(encoding);
if (encoder != null) {
DataBufferFactory dataBufferFactory = httpResponse.bufferFactory();
response = response.publishOn(Schedulers.parallel()).map(buffer -> {
byte[] encodedResponse = encoder.encode(buffer);
DataBufferUtils.release(buffer);
return encodedResponse;
}).map(dataBufferFactory::wrap);
break;
}
}
return response;
}
}
1)加密算法
2)签名算法
由于保密需要,只做简单阐述,节点分组主要是结合当前应用的技术栈,如使用feign自带的负载均衡调度的能力,那么可以针对feign的负载进行修改,添加分组的逻辑;如果是使用gw额外引入的loadbalance包,那么,要对引入的loadbalance算法进行调整,在loadbalance执行前,读取分组,并进行负载server的过滤。
文章到这里差不多就结束了,辛苦各位看官,中间有很多不便直接po出来而省略的内容,欢迎大家私下和我交流,相互提升。感谢大家!