Spring Cloud Gateway Actuator API

Spring Cloud Gateway 管理API

Spring Cloud Gateway提供了一些API,请求路径以/gateway开始,用于外部获取一些网关内部的信息或修改一些状态,可以监控或与网关交互。要使用此功能,需要提前配置开放/gateway端点,如下application.yml所示:

management.endpoint.gateway.enabled=true # default value
management.endpoints.web.exposure.include=gateway

查询路由过滤器

查询全局过滤器

有时候,我们想看一起网关中加载了哪些过滤器,可能向网关发送Get请求:/actuator/gateway/globalfilters,网关会返回json数据,如下所示:

{
  "org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@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
}

返回的数据中,每一条代表一个全局过滤器的类对象信息,以及它的Order

查询路由中的过滤器

如果要查询网关成功加载的GatewayFilter factories,可以发送Get请求:/actuator/gateway/routefilters。返回的JSON数据如下所示:

{
  "[AddRequestHeaderGatewayFilterFactory@570ed9c configClass = AbstractNameValueGatewayFilterFactory.NameValueConfig]": null,
  "[SecureHeadersGatewayFilterFactory@fceab5d configClass = Object]": null,
  "[SaveSessionGatewayFilterFactory@4449b273 configClass = Object]": null
}

返回的数据是GatewFilter factories的加载列表,这里面的null是没有意义的,因为这里本是用来设置过滤器的Order的,但是GatewayFilter factory没有Order。

刷新路由缓存

为了清空路由的缓存,可以发送Post方法:/actuator/gateway/refresh,它返回状态码为200的响应,不携带任何内容。

查询所有的路由

获取网关中定义的路由配置,可以发送Get请求:/actuator/gateway/routes,返回的信息如下所示:

[
{
"order":
0,
"route_id":
"custom-1",
"route_object":
{
"filters":
[
"OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.AddResponseHeaderGatewayFilterFactory$$Lambda$356/1904600593@5a92457d, order=0}",
"OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RedirectToGatewayFilterFactory$$Lambda$357/206142037@2b209579, order=0}"
],
"predicate":
"org.springframework.cloud.gateway.handler.AsyncPredicate$$Lambda$353/735796751@2ba4173c"
}
},
{
"order":
0,
"route_id":
"custom-2",
"route_object":
{
"filters":
[
"OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.AddResponseHeaderGatewayFilterFactory$$Lambda$356/1904600593@2b9119f, order=0}"
],
"predicate":
"org.springframework.cloud.gateway.support.ServerWebExchangeUtils$$Lambda$343/1719990258@92fea71"
}
}
]

每个路由定义包括的内容有:

  • route_id 字符串 路由的ID
  • route_object.predicate Object 路由中包括的Predicate
  • route_object.filters 数组 路由中包括的GatewayFilter factory
  • order 整数 路由的order值

获取单个路由的信息

查询某个路径的信息,可以发送Get请求:/actuator/gateway/routes/{id} ,{id} 表示是查询的路由的id值,返回数据如下所示:

{
"filters":
[
{
"args":
{
"_genkey_0":
"301",
"_genkey_1":
"http://www.xinyues.com"
},
"name":
"RedirectTo"
}
],
"id":
"prefixpath_route",
"order":
0,
"predicates":
[
{
"args":
{
"_genkey_0":
"/redirect_test"
},
"name":
"Path"
}
],
"uri":
"http://localhost:8080"
}

注意,这个方法好像只会返回application.yml中配置的路由信息,如果路由信息是使用Java代码添加的,好像获取不到。

创建和删除路由

创建路由,发送Post请求:/gateway/routes/{id_route_to_create},然后消息体添加创建的路由的JSON串,内容格式与上面查询时的一样即可。
删除路由,发送Delete请求:/gateway/routes/{id_route_to_delete}

原文地址:https://www.cnblogs.com/wgslucky/p/11758958.html