文章26 | 阅读 10671 | 点赞0
本文主要分享 RouteToRequestUrlFilter 的代码实现。
RouteToRequestUrlFilter 根据匹配的 Route ,计算请求的地址。注意,这里的地址指的是 URL ,而不是 URI 。
😈 RouteToRequestUrlFilter 的代码十分少,所以这会是一篇简单的文章。
org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter
,代码如下 :
1: public class RouteToRequestUrlFilter implements GlobalFilter, Ordered {
2:
3: private static final Log log = LogFactory.getLog(RouteToRequestUrlFilter.class);
4: public static final int ROUTE_TO_URL_FILTER_ORDER = 10000;
5:
6: @Override
7: public int getOrder() {
8: return ROUTE_TO_URL_FILTER_ORDER;
9: }
10:
11: @Override
12: public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
13: // 获得 Route
14: Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
15: if (route == null) {
16: return chain.filter(exchange);
17: }
18: log.trace("RouteToRequestUrlFilter start");
19: // 拼接 requestUrl
20: URI requestUrl = UriComponentsBuilder.fromHttpRequest(exchange.getRequest())
21: .uri(route.getUri())
22: .build(true) // encoded=true
23: .toUri();
24: // 设置 requestUrl 到 GATEWAY_REQUEST_URL_ATTR {@link RewritePathGatewayFilterFactory}
25: exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUrl);
26: // 提交过滤器链继续过滤
27: return chain.filter(exchange);
28: }
29:
30: }
#getOrder()
方法,返回顺序为 10000 。在 《Spring-Cloud-Gateway 源码解析 —— 过滤器 (4.1) 之 GatewayFilter 一览》「3. GlobalFilter」 ,我们列举了所有 GlobalFilter 的顺序。requestUrl
。这里有一点要注意下,如果 Route.uri
属性配置带有 Path ,则会覆盖请求的 Path 。我们来举个例子 :Route.uri | Request.uri | requestUrl |
---|---|---|
http://bin.org:80 | http://127.0.0.1:8080/test/segment | http://httpbin.org:80/123 |
http://bin.org:80/123 | http://127.0.0.1:8080/test/segment | http://httpbin.org:80/test/segment |
UriComponentsBuilder#uri(URI)
方法,代码如下 :1: public UriComponentsBuilder uri(URI uri) {
2: Assert.notNull(uri, "URI must not be null");
3: this.scheme = uri.getScheme();
4: if (uri.isOpaque()) {
5: this.ssp = uri.getRawSchemeSpecificPart();
6: resetHierarchicalComponents();
7: }
8: else {
9: if (uri.getRawUserInfo() != null) {
10: this.userInfo = uri.getRawUserInfo();
11: }
12: if (uri.getHost() != null) {
13: this.host = uri.getHost();
14: }
15: if (uri.getPort() != -1) {
16: this.port = String.valueOf(uri.getPort());
17: }
18: if (StringUtils.hasLength(uri.getRawPath())) {
19: this.pathBuilder = new CompositePathComponentBuilder();
20: this.pathBuilder.addPath(uri.getRawPath());
21: }
22: if (StringUtils.hasLength(uri.getRawQuery())) {
23: this.queryParams.clear();
24: query(uri.getRawQuery());
25: }
26: resetSchemeSpecificPart();
27: }
28: if (uri.getRawFragment() != null) {
29: this.fragment = uri.getRawFragment();
30: }
31: return this;
32: }
uri
参数有 Path 时,新建一个 CompositePathComponentBuilder ,因此原有的 this.pathBuilder
被覆盖了。requestUrl
到 GATEWAY_REQUEST_URL_ATTR
。后面 Routing 相关的 GatewayFilter 会通过该属性,发起请求。版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_42073629/article/details/106934489
内容来源于网络,如有侵权,请联系作者删除!