system health_可重复使用的MicroProfile Health探针
system health
MicroProfile Health API是一種非常基本的API,它基于一個或多個Health Probe報告您的服務(wù)狀態(tài)。 在某些服務(wù)器或群集控制器需要決定是否以及何時重新啟動實例的情況下,這非常有用。
在應(yīng)用程序中使用MicroProfile Health API就像實現(xiàn)一個(或多個) org.eclipse.microprofile.health.HealthCheck并使用@Health注釋類一樣@Health 。
HealthCheck接口具有您應(yīng)該實現(xiàn)的一種方法,即HealthCheckResponse call() 。
因此,您可以確定在調(diào)用此方法時,實例是否正常。
您的回復(fù)( HealthCheckResponse )包含:
- 從其他探針識別此探針的名稱 。
- UP或DOWN標(biāo)志,以指示狀態(tài)。
- 您想要在鍵值對中包含的任何其他元數(shù)據(jù)。
一個基本的例子。
假設(shè)我們有一個使用數(shù)據(jù)庫的應(yīng)用程序,并且如果與數(shù)據(jù)庫的連接斷開(或非常慢),則應(yīng)報告此應(yīng)用程序不正常:
@Health@ApplicationScopedpublic class MembershipHealthCheck implements HealthCheck {@Inject private DataSource datasource;@Overridepublic HealthCheckResponse call() {HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("membership");try {Connection connection = datasource.getConnection();boolean isValid = connection.isValid(timeout);DatabaseMetaData metaData = connection.getMetaData();responseBuilder = responseBuilder.withData("databaseProductName", metaData.getDatabaseProductName()).withData("databaseProductVersion", metaData.getDatabaseProductVersion()).withData("driverName", metaData.getDriverName()).withData("driverVersion", metaData.getDriverVersion()).withData("isValid", isValid);return responseBuilder.state(isValid).build();} catch(SQLException e) {log.log(Level.SEVERE, null, e);responseBuilder = responseBuilder.withData("exceptionMessage", e.getMessage());return responseBuilder.down().build();}}}(見完整的例子在這里 )
在上面的示例中,健康狀況探針名稱為“ membership”,如果可以在一定時間內(nèi)建立與數(shù)據(jù)庫的連接,則報告UP 。 它還包括數(shù)據(jù)庫的一些元數(shù)據(jù)字段。
/健康。
如果瀏覽到服務(wù)器上的/health ,您將看到來自所有探測的匯總響應(yīng)以及服務(wù)器的總狀態(tài)(“啟動”或“關(guān)閉”)。
{"outcome":"UP","checks":[{"name":"membership","state":"UP","data":{"databaseProductVersion":"5.5.5-10.1.35-MariaDB","databaseProductName":"MySQL","driverVersion":"mysql-connector-java-8.0.11 (Revision: 6d4eaa273bc181b4cf1c8ad0821a2227f116fedf)","isValid":"true","driverName":"MySQL Connector/J"}}]}如果數(shù)據(jù)庫出現(xiàn)故障:
{"outcome":"DOWN","checks":[{"name":"membership","state":"DOWN","data":{"exceptionMessage":"No operations allowed after connection closed."}}]}使用MicroProfile配置創(chuàng)建可重復(fù)使用的探針。
您的任何應(yīng)用程序都可以重復(fù)使用某些運行狀況探針,并且可以使用Microprofile Config API外部化設(shè)置。 例如,如果我們希望運行狀況探針檢查系統(tǒng)負(fù)載,則可以外部化系統(tǒng)負(fù)載應(yīng)該在哪個階段開始報告下來。
@Health@ApplicationScopedpublic class SystemLoadHealthCheck implements HealthCheck {@Inject @ConfigProperty(name = "health.systemload.max", defaultValue = "0.7")private double max;@Overridepublic HealthCheckResponse call() {OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();String arch = operatingSystemMXBean.getArch();String name = operatingSystemMXBean.getName();String version = operatingSystemMXBean.getVersion();int availableProcessors = operatingSystemMXBean.getAvailableProcessors();double systemLoadAverage = operatingSystemMXBean.getSystemLoadAverage();double systemLoadAveragePerProcessors = systemLoadAverage / availableProcessors;HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("system-load").withData("name", name).withData("arch", arch).withData("version", version).withData("processors", availableProcessors).withData("loadAverage", String.valueOf(systemLoadAverage)).withData("loadAverage per processor", String.valueOf(systemLoadAveragePerProcessors)).withData("loadAverage max", String.valueOf(max));if(systemLoadAverage>0){boolean status = systemLoadAveragePerProcessors < max;return responseBuilder.state(status).build();}else{// Load average not availablereturn responseBuilder.up().build();}}}(見完整的例子在這里 )
在上面,我們現(xiàn)在可以通過更改health.systemload.max配置值將默認(rèn)的0.7系統(tǒng)負(fù)載覆蓋為我們自己的值。
其他示例可能包括:
- 堆內(nèi)存
- 非堆內(nèi)存
- 線程數(shù)
在項目中使用它
您可以在項目中使用以上所有內(nèi)容,因為它們可以在maven Central和github中使用 :
在您的pom.xml :
<dependency><groupId>com.github.phillip-kruger.microprofile-extensions</groupId><artifactId>health-ext</artifactId><version>1.0.9</version></dependency>/health的合計結(jié)果如下所示:
{"outcome":"UP","checks":[{"name":"system-load","state":"UP","data":{"name":"Linux","arch":"amd64","processors":"8","loadAverage":"2.03","version":"4.18.1-arch1-1-ARCH","loadAverage max":"0.7","loadAverage per processor":"0.25375"}},{"name":"membership","state":"UP","data":{"databaseProductVersion":"5.5.5-10.1.35-MariaDB","databaseProductName":"MySQL","driverVersion":"mysql-connector-java-8.0.11 (Revision: 6d4eaa273bc181b4cf1c8ad0821a2227f116fedf)","isValid":"true","driverName":"MySQL Connector/J"}},{"name":"non-heap-memory","state":"UP","data":{"max %":"0.9","max":"-1","used":"132792064"}},{"name":"threads","state":"UP","data":{"max thread count":"-1","daemon thread count":"86","monitor deadlocked thread count":"0","thread count":"134","deadlocked thread count":"0","started thread count":"138","peak thread count":"136"}},{"name":"heap-memory","state":"UP","data":{"max %":"0.9","max":"14995161088","used":"207556800"}}]}翻譯自: https://www.javacodegeeks.com/2018/08/reusable-microprofile-health-probes.html
system health
總結(jié)
以上是生活随笔為你收集整理的system health_可重复使用的MicroProfile Health探针的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: apache ignite_Apache
- 下一篇: 文档在线签名_为什么需要为文档和合同切换