system health_可重复使用的MicroProfile Health探针
system health
MicroProfile Health API是一種非常基本的API,它基于一個或多個Health Probe報告您的服務狀態。 在某些服務器或群集控制器需要決定是否以及何時重新啟動實例的情況下,這非常有用。
在應用程序中使用MicroProfile Health API就像實現一個(或多個) org.eclipse.microprofile.health.HealthCheck并使用@Health注釋類一樣@Health 。
HealthCheck接口具有您應該實現的一種方法,即HealthCheckResponse call() 。
因此,您可以確定在調用此方法時,實例是否正常。
您的回復( HealthCheckResponse )包含:
- 從其他探針識別此探針的名稱 。
- UP或DOWN標志,以指示狀態。
- 您想要在鍵值對中包含的任何其他元數據。
一個基本的例子。
假設我們有一個使用數據庫的應用程序,并且如果與數據庫的連接斷開(或非常慢),則應報告此應用程序不正常:
@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”,如果可以在一定時間內建立與數據庫的連接,則報告UP 。 它還包括數據庫的一些元數據字段。
/健康。
如果瀏覽到服務器上的/health ,您將看到來自所有探測的匯總響應以及服務器的總狀態(“啟動”或“關閉”)。
{"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"}}]}如果數據庫出現故障:
{"outcome":"DOWN","checks":[{"name":"membership","state":"DOWN","data":{"exceptionMessage":"No operations allowed after connection closed."}}]}使用MicroProfile配置創建可重復使用的探針。
您的任何應用程序都可以重復使用某些運行狀況探針,并且可以使用Microprofile Config API外部化設置。 例如,如果我們希望運行狀況探針檢查系統負載,則可以外部化系統負載應該在哪個階段開始報告下來。
@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();}}}(見完整的例子在這里 )
在上面,我們現在可以通過更改health.systemload.max配置值將默認的0.7系統負載覆蓋為我們自己的值。
其他示例可能包括:
- 堆內存
- 非堆內存
- 線程數
在項目中使用它
您可以在項目中使用以上所有內容,因為它們可以在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的合計結果如下所示:
{"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
總結
以上是生活随笔為你收集整理的system health_可重复使用的MicroProfile Health探针的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: apache ignite_Apache
- 下一篇: 唱山歌歌词 唱山歌歌词内容