Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter(默认GlobalFilter:按照默认执行顺序,从上到下排列) #9

Open
Maybrittnelson opened this issue Mar 6, 2019 · 2 comments

Comments

@Maybrittnelson
Copy link
Owner

Maybrittnelson commented Mar 6, 2019

AdaptCacheBodyGlobalFilter

Ordered.LOWEST_PRECEDENCE-2147482648

GatewayMetricsFilter

Ordered.LOWEST_PRECEDENCE - 1;

WebsocketRoutingFilter

Ordered.LOWEST_PRECEDENCE - 1;

NettyWriteResponseFilter

-1

.then(postfilter),then也就是后处理

ForwardPathFilter

0

RouteToRequestUrlFilter

10000

LoadBalancerClientFilter

10100

有lbclient才使用

NettyRoutingFilter

LOWEST_PRECEDENCE = 2147483647

发送请求后段服务

ForwardRoutingFilter

LOWEST_PRECEDENCE = 2147483647

NoLoadBalancerClientFilter

10100;

未使用(没有lbclientfilter才使用)

WebClientHttpRoutingFilter

LOWEST_PRECEDENCE = 2147483647;

未使用

WebClientWriteResponseFilter

WRITE_RESPONSE_FILTER_ORDER = -1;

未使用

@Maybrittnelson Maybrittnelson changed the title Filter Filter(默认GlobalFilter:按照默认执行顺序汇总) Mar 6, 2019
@Maybrittnelson Maybrittnelson changed the title Filter(默认GlobalFilter:按照默认执行顺序汇总) Filter(默认GlobalFilter:按照默认执行顺序,从上到下排列) Mar 6, 2019
@Maybrittnelson
Copy link
Owner Author

Maybrittnelson commented Mar 6, 2019

那么实现GatewayFilter接口的filter什么时候执行?
举个🌰

### filters
spring.cloud.gateway.routes[0].filters[0].name=Hystrix
spring.cloud.gateway.routes[0].filters[0].args.name=user-service-provider
spring.cloud.gateway.routes[0].filters[0].args.fallbackUri= forward:/fallback
spring.cloud.gateway.routes[0].filters[1].name=StripPrefix
spring.cloud.gateway.routes[0].filters[1].args.parts=2

答案是 Hystrix先执行,order分别是多少,Hystrix(1)、StripPrefix(2)

org.springframework.cloud.gateway.handler.FilteringWebHandler#handle debug查看排序后的combined,就可以知道filters的具体排序

@Maybrittnelson
Copy link
Owner Author

Maybrittnelson commented Mar 7, 2019

WeightCalculatorWebFilter 为什么在 DispatcherHandler之前执行?

org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter#getWeights

exchange.getAttributes().put(WEIGHT_ATTR, weights);
weights.put(group, routeId);

org.springframework.cloud.gateway.handler.predicate.WeightRoutePredicateFactory#apply

Map<String, String> weights = exchange.getAttributeOrDefault(WEIGHT_ATTR,
Collections.emptyMap());
String routeId = exchange.getAttribute(GATEWAY_PREDICATE_ROUTE_ATTR);
// all calculations and comparison against random num happened in
// WeightCalculatorWebFilter
String group = config.getGroup();
if (weights.containsKey(group)) {
String chosenRoute = weights.get(group);
return routeId.equals(chosenRoute);
}			

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant