StripPrefix GatewayFilter Factory
StripPrefix GatewayFilter Factory采用一个参数,即部件。parts参数指示在向下游发送之前从请求中剥离的路径中的部分数。
application.yml
spring:
cloud:
gateway:
routes:
- id: nameRoot
uri: http://nameservice
predicates:
- Path=/name/**
filters:
- StripPrefix=2
当通过网关向/ name / bar / foo发出请求时,对nameservice的请求将类似于http:// nameservice / foo。
Retry GatewayFilter Factory
Retry GatewayFilter Factory将重试,状态,方法和系列作为参数。
- 重试次数:应尝试的重试次数
- 状态:应该重试的HTTP状态代码,使用org.springframework.http.HttpStatus表示
- 方法:应该重试的HTTP方法,使用org.springframework.http.HttpMethod表示
- series:要重试的一系列状态代码,使用org.springframework.http.HttpStatus.Series表示
application.yml
spring:
cloud:
gateway:
routes:
- id: retry_test
uri: http://localhost:8080/flakey
predicates:
- Host=*.retry.com
filters:
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY
重试过滤器当前不支持使用正文重试(例如,对于具有正文的POST或PUT请求)。
当使用带有转发前缀URL的重试过滤器时,应仔细编写目标端点,以便在出现错误时不会执行任何可能导致响应发送到客户端并提交的操作。例如,如果目标端点是带注释的控制器,则目标控制器方法不应返回带有错误状态代码的ResponseEntity。相反,它应抛出异常,或发出错误信号,例如通过Mono.error(ex)返回值,可以将重试过滤器配置为通过重试来处理。
RequestSize GatewayFilter Factory
当请求大小大于允许的限制时,RequestSize GatewayFilter Factory可以限制请求到达下游服务。过滤器将RequestSize作为参数,该参数是以字节为单位定义的请求的允许大小限制。
application.yml
spring:
cloud:
gateway:
routes:
- id: request_size_route
uri: http://localhost:8080/upload
predicates:
- Path=/upload
filters:
- name: RequestSize
args:
maxSize: 5000000
当请求因大小而被拒绝时,RequestSize GatewayFilter Factory将响应状态设置为413 Payload Too Large,并附加标头errorMessage。以下是此类errorMessage的示例。
errorMessage:请求大小大于允许的限制。请求大小为6.0 MB,允许的限制为5.0 MB
如果未在路由定义中将过滤参数提供,则默认请求大小将设置为5 MB。
Modify Request Body GatewayFilter Factory
此过滤器被视为BETA,API可能在将来发生变化此过滤器可用于在网关向下游发送请求主体之前对其进行修改。
此过滤器只能使用Java DSL进行配置
@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
return builder.routes()
.route("rewrite_request_obj", r -> r.host("*.rewriterequestobj.org")
.filters(f -> f.prefixPath("/httpbin")
.modifyRequestBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE,
(exchange, s) -> return Mono.just(new Hello(s.toUpperCase())))).uri(uri))
.build();
}
static class Hello {
String message;
public Hello() { }
public Hello(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Modify Response Body GatewayFilter Factory
此过滤器被视为BETA,API可能在将来发生变化此过滤器可用于在将响应主体发送回客户端之前对其进行修改。
此过滤器只能使用Java DSL进行配置
@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
return builder.routes()
.route("rewrite_response_upper", r -> r.host("*.rewriteresponseupper.org")
.filters(f -> f.prefixPath("/httpbin")
.modifyResponseBody(String.class, String.class,
(exchange, s) -> Mono.just(s.toUpperCase()))).uri(uri)
.build();
}
Default Filters
如果您想添加过滤器并将其应用于所有路线,可以使用spring.cloud.gateway.default-filters。此属性采用过滤器列表
application.yml
spring:
cloud:
gateway:
default-filters:
- AddResponseHeader=X-Response-Default-Foo, Default-Bar
- PrefixPath=/httpbin