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}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM