天天看點

【轉載】Spring Cloud Gateway監控

歡迎加入Spring Cloud Gateway監控豪華套餐——

隻要為Spring Cloud Gateway添加Spring Boot Actuator( ​

​spring-boot-starter-actuator​

​ )的依賴,并将 ​

​gateway​

​ 端點暴露,即可獲得若幹監控端點,監控 & 操作Spring Cloud Gateway的方方面面。

management:
  endpoints:
    web:
      exposure:
# 當然暴露'*' 更好啦..
        include: gateway
      

監控端點一覽表:

TIPS

以下所有端點都挂在​

​/actuator/gateway/​

​ 下面。

例如:​

​routes​

​ 的全路徑是 ​

​/actuator/gateway/routes​

​ ,以此類推。
ID HTTP Method Description

​globalfilters​

GET 展示所有的全局過濾器

​routefilters​

展示所有的過濾器工廠(GatewayFilter factories)

​refresh​

POST【無消息體】 清空路由緩存

​routes​

展示路由清單

​routes/{id}​

展示指定id的路由的資訊

​routes/{id}​

POST【消息體如下】 新增一個路由

​routes/{id}​

DELETE【無消息體】 删除一個路由

其中,要想動态添加路由配置,隻需發送POST請求,消息體如下:

{
"predicates": [
    {
"name": "Path",
"args": {
"_genkey_0": "/test"
      }
    }
  ],
"filters": [
    {
"name": "AddRequestHeader",
"args": {
"_genkey_0": "X-Request-Foo",
"_genkey_1": "Bar"
      }
    },
    {
"name": "PreLog",
"args": {
"_genkey_0": "a",
"_genkey_1": "b"
      }
    }
  ],
"uri": "https://www.itmuch.com",
"order": 0
}
      
技巧:消息體其實是有規律的,你可以先在配置檔案中配置一個路由規則,然後通路​

​${GATEWAY_URL}/actuator/gateway/routes​

​ 端點,每個路由id的對應段落,就是你的消息體啦。

如使用 ​

​POSTMAN​

​ 測試,可配置如下:

【轉載】Spring Cloud Gateway監控

操作完成後,可再次通路 ​

​${GATEWAY_URL}/actuator/gateway/routes​

​ 端點,可以看到,新的路由已被動态添加了。

如果沒有實時生效,使用refresh端點重新整理一下路由資訊即可。