15. Actuator API
执行/gateway器端点允许您监视 Spring Cloud Gateway 应用程序并与之交互。要远程访问,必须在应用程序属性中启用并通过 HTTP 或 JMX 公开端点。以下清单显示了如何执行此操作:
示例 71.application.properties
management.endpoint.gateway.enabled=true # default value
management.endpoints.web.exposure.include=gateway
15.1. 详细执行器格式
Spring Cloud Gateway 中添加了一种新的、更详细的格式。它为每个路由添加了更多详细信息,让您可以查看与每个路由关联的谓词和过滤器以及任何可用配置。以下示例配置/actuator/gateway/routes
:
[
{
"predicate": "(Hosts: [**.addrequestheader.org] && Paths: [/headers], match trailing slash: true)",
"route_id": "add_request_header_test",
"filters": [
"[[AddResponseHeader X-Response-Default-Foo = 'Default-Bar'], order = 1]",
"[[AddRequestHeader X-Request-Foo = 'Bar'], order = 1]",
"[[PrefixPath prefix = '/httpbin'], order = 2]"
],
"uri": "lb://testservice",
"order": 0
}
]
默认情况下启用此功能。要禁用它,请设置以下属性:
示例 72.application.properties
spring.cloud.gateway.actuator.verbose.enabled=false
这将true
在未来的版本中默认使用。
15.2. 检索路由过滤器
本节详细介绍如何检索路由过滤器,包括:
全局过滤器
[网关路由过滤器]
15.2.1. 全局过滤器
要检索应用于所有路由的全局过滤器GET,请向/actuator/gateway/globalfilters. 生成的响应类似于以下内容:
{
"org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter@77856cc5": 10100,
"org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@4f6fd101": 10000,
"org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@32d22650": -1,
"org.springframework.cloud.gateway.filter.ForwardRoutingFilter@106459d9": 2147483647,
"org.springframework.cloud.gateway.filter.NettyRoutingFilter@1fbd5e0": 2147483647,
"org.springframework.cloud.gateway.filter.ForwardPathFilter@33a71d23": 0,
"org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@135064ea": 2147483637,
"org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@23c05889": 2147483646
}
响应包含已到位的全局过滤器的详细信息。对于每个全局过滤器,都有过滤器对象的字符串表示形式(例如,org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter@77856cc5)和过滤器链中的相应顺序。}
15.2.2. 路由过滤器
要检索应用于路由的GatewayFilter工厂GET,请向/actuator/gateway/routefilters. 生成的响应类似于以下内容:
{
"[AddRequestHeaderGatewayFilterFactory@570ed9c configClass = AbstractNameValueGatewayFilterFactory.NameValueConfig]": null,
"[SecureHeadersGatewayFilterFactory@fceab5d configClass = Object]": null,
"[SaveSessionGatewayFilterFactory@4449b273 configClass = Object]": null
}
响应包含GatewayFilter应用于任何特定路由的工厂的详细信息。对于每个工厂,都有对应对象的字符串表示形式(例如,[SecureHeadersGatewayFilterFactory@fceab5d configClass = Object])。请注意,该null值是由于端点控制器的实现不完整,因为它试图设置对象在过滤器链中的顺序,这不适用于GatewayFilter工厂对象。
15.3. 刷新路由缓存
要清除路由缓存,POST请向/actuator/gateway/refresh. 该请求返回没有响应正文的 200。
15.4. 检索网关中定义的路由
要检索网关中定义的路由,GET请向/actuator/gateway/routes. 生成的响应类似于以下内容:
[{
"route_id": "first_route",
"route_object": {
"predicate": "org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$432/1736826640@1e9d7e7d",
"filters": [
"OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.PreserveHostHeaderGatewayFilterFactory$$Lambda$436/674480275@6631ef72, order=0}"
]
},
"order": 0
},
{
"route_id": "second_route",
"route_object": {
"predicate": "org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$432/1736826640@cd8d298",
"filters": []
},
"order": 0
}]
响应包含网关中定义的所有路由的详细信息。下表描述了响应的每个元素(每个元素都是一个路由)的结构:
Path | Type | Description |
---|---|---|
route_id | String | The route ID. |
route_object.predicate | Object | The route predicate. |
route_object.filters | Array | The GatewayFilter factories applied to the route. |
order | Number | The route order. |
15.5. 检索有关特定路线的信息
要检索有关单个路由的信息,GET请向/actuator/gateway/routes/(例如,/actuator/gateway/routes/first_route)发出请求。生成的响应类似于以下内容:
{
"id": "first_route",
"predicates": [{
"name": "Path",
"args": {"_genkey_0":"/first"}
}],
"filters": [],
"uri": "https://www.uri-destination.org",
"order": 0
}
下表描述了响应的结构:
Path | Type | Description |
---|---|---|
id | String | The route ID. |
predicates | Array | The collection of route predicates. Each item defines the name and the arguments of a given predicate. |
filters | Array | The collection of filters applied to the route. |
uri | String | The destination URI of the route. |
order | Number | The route order. |
15.6. 创建和删除特定路线
要创建路由,请使用指定路由字段的 JSON 正文POST发出请求(请参阅检索有关特定路由的信息)。/gateway/routes/
要删除路线,DELETE请向/gateway/routes/。
15.7. 回顾:所有端点的列表
下表总结了 Spring Cloud Gateway 执行器端点(请注意,每个端点都有/actuator/gateway作为基本路径):
ID | HTTP Method | Description |
---|---|---|
globalfilters | GET | Displays the list of global filters applied to the routes. |
routefilters | GET | Displays the list of GatewayFilter factories applied to a particular route. |
refresh | POST | Clears the routes cache. |
routes | GET | Displays the list of routes defined in the gateway. |
routes/ | GET | Displays information about a particular route. |
routes/ | POST | Adds a new route to the gateway. |
routes/ | DELETE | Removes an existing route from the gateway. |
15.8. 在多个网关实例之间共享路由
Spring Cloud Gateway 提供了两种RouteDefinitionRepository实现。第一个是 InMemoryRouteDefinitionRepository只存在于一个网关实例的内存中。这种类型的存储库不适合跨多个网关实例填充路由。
为了在 Spring Cloud Gateway 实例的集群中共享路由,RedisRouteDefinitionRepository可以使用。要启用这种存储库,必须将以下属性设置为 true:spring.cloud.gateway.redis-route-definition-repository.enabled 与 RedisRateLimiter Filter Factory 类似,它需要使用 spring-boot-starter-data-redis-reactive Spring Boot 启动器。