本文首發於我的網站:Spring Boot應用的健康監控html
在以前的系列文章中咱們學習瞭如何進行Spring Boot應用的功能開發,以及如何寫單元測試、集成測試等,然而,在實際的軟件開發中須要作的不只如此:還包括對應用程序的監控和管理。java
正如飛行員不喜歡盲目飛行,程序員也須要實時看到本身的應用目前的運行狀況。若是給定一個具體的時間,咱們但願知道此時CPU的利用率、內存的利用率、數據庫鏈接是否正常以及在給定時間段內有多少客戶請求等指標;不只如此,咱們但願經過圖表、控制面板來展現上述信息。最重要的是:老闆和業務人員但願看到的是圖表,這些比較直觀易懂。程序員
首先,這篇文章講介紹如何定製本身的health indicator。web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
info.build.artifact=@project.artifactId@ info.build.name=@project.name@ info.build.description=@project.description@ info.build.version=@project.version@
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources>
而後在<plugins>節點裏面增長對應的插件:面試
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <configuration> <delimiters> <delimiter>@</delimiter> </delimiters> <useDefaultDelimiters>false</useDefaultDelimiters> </configuration> </plugin>
public class DbCountHealthIndicator implements HealthIndicator { private CrudRepository crudRepository; public DbCountHealthIndicator(CrudRepository crudRepository) { this.crudRepository = crudRepository; } @Override public Health health() { try { long count = crudRepository.count(); if (count >= 0) { return Health.up().withDetail("count", count).build(); } else { return Health.unknown().withDetail("count", count).build(); } } catch (Exception e) { return Health.down(e).build(); } } }
@Autowired private HealthAggregator healthAggregator; @Bean public HealthIndicator dbCountHealthIndicator(Collection<CrudRepository> repositories) { CompositeHealthIndicator compositeHealthIndicator = new CompositeHealthIndicator(healthAggregator); for (CrudRepository repository: repositories) { String name = DbCountRunner.getRepositoryName(repository.getClass()); compositeHealthIndicator.addHealthIndicator(name, new DbCountHealthIndicator(repository)); } return compositeHealthIndicator; }
Spring Boot Autuator這個庫包括不少自動配置,對外開放了不少endpoints,經過這些endpoints能夠訪問應用的運行時狀態:spring
上述各個endpoint是Spring Boot Actuator提供的接口和方法,接下來看看咱們本身定製的HealthIndicator,咱們只須要實現HealthIndicator接口,Spring Boot會收集該接口的實現,並加入到*/health*這個endpoint中。數據庫
在咱們的例子中,咱們爲每一個CrudRepository實例都建立了一個HealthIndicator實例,爲此咱們建立了一個CompositeHealthIndicator實例,由這個實例管理全部的DbHealthIndicator實例。做爲一個composite,它會提供一個內部的層次關係,從而能夠返回JSON格式的數據。apache
代碼中的HealthAggregator實例的做用是:它維護一個map,告訴CompositeHealthIndicator如何決定全部HealthIndicator表明的總體的狀態。例如,除了一個repository返回DOWN其餘的都返回UP,這時候這個composite indicator做爲一個總體應該返回UP仍是DOWN,HealthAggregator實例的做用就在這裏。後端
Spring Boot使用的默認的HealthAggregator實現是OrderedHealthAggregator,它的策略是手機全部的內部狀態,而後選出在DOWN、OUT_OF_SERVICE、UP和UNKNOWN中間具備最低優先級的那個狀態。這裏使用策略設計模式,所以具體的狀態斷定策略能夠改變和定製,例如咱們能夠建立定製的HealthAggregator:設計模式
最後須要考慮下安全問題,經過這些endpoints暴露出不少應用的信息,固然,Spring Boot也提供了配置項,能夠關閉指定的endpoint——在application.properties中配置*<name>.enable=false*;
還能夠經過設置management.port=-1關閉endpoint的HTTP訪問接口,或者是設置其餘的端口,供內部的admin服務訪問;除了控制端口,還能夠設置僅僅讓本地訪問,只須要設置management.address=127.0.0.1;經過設置management.context-path=/admin,能夠設置指定的根路徑。綜合下,通過上述設置,在本地訪問http://127.0.0.1/admin/health來訪問健康狀態。
能夠在防火牆上屏蔽掉不是/admin/*的endpoints訪問請求,更進一步,利用Spring Security能夠配置驗證信息,這樣要訪問當前應用的endpoints必須使用用戶名和密碼登錄。
本號專一於後端技術、JVM問題排查和優化、Java面試題、我的成長和自我管理等主題,爲讀者提供一線開發者的工做和成長經驗,期待你能在這裏有所收穫。