1. 程式人生 > 程式設計 >Spring Boot Admin實踐詳解

Spring Boot Admin實踐詳解

在Spring Boot Actuator中提供很多像health、metrics等實時監控介面,可以方便我們隨時跟蹤服務的效能指標。Spring Boot預設是開放這些介面提供呼叫的,那麼就問題來了,如果這些介面公開在外網中,很容易被不法分子所利用,這肯定不是我們想要的結果。在這裡我們提供一種比較好的解決方案。

被監控的服務配置

為被保護的http請求新增請求字首

management:
 context-path: /example-context 
eureka:
 instance:
  status-page-url-path: ${management.context-path}/info 
  health-check-url-path: ${management.context-path}/health

新增請求字首

Spring Boot Admin在啟動的時候會去eureka拉去服務資訊,其中health與info需要特殊處理,這兩者的地址是根據status-page-url-path和health-check-url-path的值。

zuul閘道器配置

zuul保護內部服務http介面

zuul:
 ignoredPatterns: /*/example-context/** 

這裡之所以不是/example-context/**,由於閘道器存在專案字首,需要往前一級,大家可以具體場景具體配置

Spring Boot Admin配置

配置監控的指標引數

spring:< 大專欄 Spring Boot Admin最佳實踐/span>
 application:
  name: monitor
 boot:
  admin:
   discovery:
    converter:
     management-context-path: /example-context 
   routes:
    endpoints: env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream
   turbine:
    clusters: default
    location: monitor

turbine:
 aggregator:
  clusterConfig: default
 appConfig: monitor-example #<2>
 clusterNameExpression: metadata['cluster']

與應用配置的management.context-path相同

新增需要被監控的應用Service-Id,以逗號分隔

講解一下,通過建立一個請求字首,可以在閘道器處使用字首的方式將其排除,也就是外網將無法訪問這些監控API,同時,內網還是可以進行加字首的方式進行訪問,為Spring Boot Admin提供了支援條件。management還支援port和ip的方式,但這兩種方式有侷限性,如果在同一臺機器上部署多個服務,就會存在端口占用或者其他問題。這種方案還有一個好處,以上配置一旦確定以後,所有服務都不需要進行特殊化處理,可以直接使用。

問答:

問題:Full authentication is required to access this resource

參考連結:https://github.com/joshiste/spring-boot-admin-samples

以上就是本次介紹的全部知識點,感謝大家對我們的支援。