<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Dropwizard 運行狀況檢查配置示例 > 原文: [https://howtodoinjava.com/dropwizard/health-check-configuration-example/](https://howtodoinjava.com/dropwizard/health-check-configuration-example/) 我們已經看到 [dropwizard](http://www.dropwizard.io/) 在開發[自包含的 REST API](//howtodoinjava.com/dropwizard/tutorial-and-hello-world-example/) 甚至 [REST 客戶端服務](//howtodoinjava.com/dropwizard/client-configuration-and-examples/)方面如此有效。 Dropwizard 包含幾乎所有必需的包,它們可以非常簡單地構建 API,而無需使事情復雜化。 dropwizard 的一項易于實現的功能是**運行狀況檢查服務**,該服務可用于在運行時監視正在創建的應用/組件的狀態。 ## 實現 Dropwizard 運行狀況檢查 DropWizard 運行狀況檢查是通過擴展`HealthCheck`類并在一切正常的情況下返回`Result.healthy()`以及在某些情況下無效的情況下返回`Result.unhealthy()`來實現的 - 符合預期。 #### 運行狀況檢查配置 ```java public class AppHealthCheck extends HealthCheck { @Override protected Result check() throws Exception { if(Check some condition == true){ return Result.healthy(); } return Result.unhealthy("Error message"); } } ``` 要在 dropwizard 應用中注冊此`AppHealthCheck`類,請使用`Environment.healthChecks()`注冊表。 ```java public void run(Configuration c, Environment e) throws Exception { //Application health check e.healthChecks().register("APIHealthCheck", new AppHealthCheck()); } ``` #### 驗證系統運行狀況 Dropwizard 將在管理端口上的`/healthcheck`上使用 HTTP 資源終結點(默認為 8081)。 在默認情況下,Dropwizard 還包括對死鎖的檢查以及您定義的自定義運行狀況檢查`AppHealthCheck`。 ```java http://localhost:8081/healthcheck ``` 上面的運行狀況檢查網址將返回一些結果,如下所示: ```java { "APIHealthCheck": { "healthy": true }, "deadlocks": { "healthy": true } } ``` ## 自定義 REST 資源以運行運行狀況檢查 如果您不想使用管理端口,則還可以創建一個自定義 REST 資源,該資源將為您運行運行狀況檢查,并以所需的響應格式返回結果。 要運行所有運行狀況檢查并獲取所有結果,您將在 REST 資源中調用`registry.runHealthChecks()`。 ```java @Produces(MediaType.APPLICATION_JSON) @Path("/status") public class HealthCheckController { private HealthCheckRegistry registry; public HealthCheckController(HealthCheckRegistry registry) { this.registry = registry; } @GET public Set<Entry<String, Result>> getStatus(){ return registry.runHealthChecks().entrySet(); } } ``` 現在,當我們使用`http://localhost:8080/status`調用此 REST API 時,我們將得到如下響應: ```java [ { "APIHealthCheck": { "healthy": true, "message": null, "error": null } }, { "deadlocks": { "healthy": true, "message": null, "error": null } } ] ``` 您可以根據需要自定義消息。 ## Dropwizard 運行狀況檢查示例 為了演示上述兩種功能,我修改了 [dropwizard HelloWorld 應用](//howtodoinjava.com/dropwizard/tutorial-and-hello-world-example/)中給出的代碼。 **AppHealthCheck.java** ```java package com.howtodoinjava.healthcheck; import java.util.ArrayList; import javax.ws.rs.client.Client; import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.codahale.metrics.health.HealthCheck; public class AppHealthCheck extends HealthCheck { private final Client client; public AppHealthCheck(Client client) { super(); this.client = client; } @Override protected Result check() throws Exception { WebTarget webTarget = client.target("http://localhost:8080/employees"); Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); Response response = invocationBuilder.get(); @SuppressWarnings("rawtypes") ArrayList employees = response.readEntity(ArrayList.class); if(employees !=null && employees.size() > 0){ return Result.healthy(); } return Result.unhealthy("API Failed"); } } ``` **HealthCheckController.java** ```java package com.howtodoinjava.healthcheck; import java.util.Map.Entry; import java.util.Set; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import com.codahale.metrics.health.HealthCheck.Result; import com.codahale.metrics.health.HealthCheckRegistry; @Produces(MediaType.APPLICATION_JSON) @Path("/status") public class HealthCheckController { private HealthCheckRegistry registry; public HealthCheckController(HealthCheckRegistry registry) { this.registry = registry; } @GET public Set<Entry<String, Result>> getStatus(){ return registry.runHealthChecks().entrySet(); } } ``` **App.java** ```java package com.howtodoinjava.rest; import io.dropwizard.Application; import io.dropwizard.Configuration; import io.dropwizard.client.JerseyClientBuilder; import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; import javax.ws.rs.client.Client; import com.howtodoinjava.healthcheck.AppHealthCheck; import com.howtodoinjava.healthcheck.HealthCheckController; import com.howtodoinjava.rest.controller.EmployeeRESTController; import com.howtodoinjava.rest.controller.RESTClientController; public class App extends Application<Configuration> { @Override public void initialize(Bootstrap<Configuration> b) { } @Override public void run(Configuration c, Environment e) throws Exception { e.jersey().register(new EmployeeRESTController(e.getValidator())); final Client client = new JerseyClientBuilder(e).build("DemoRESTClient"); e.jersey().register(new RESTClientController(client)); //Application health check e.healthChecks().register("APIHealthCheck", new AppHealthCheck(client)); //Run multiple health checks e.jersey().register(new HealthCheckController(e.healthChecks())); } public static void main(String[] args) throws Exception { new App().run(args); } } ``` ## 驗證運行狀況檢查 URL #### 1)`http://localhost:8081/healthcheck` ![Dropwizard health check on admin port](https://img.kancloud.cn/59/2a/592a07052b9b93d6f6204245ef25bda5_951x513.png) 在管理端口上進行 Dropwizard 運行狀況檢查 #### 2)`http://localhost:8080/status` ![Dropwizard health check on application port](https://img.kancloud.cn/ae/37/ae37b208f4e5629dbc4b142622ed7d8b_757x531.png) 在應用端口上進行 Dropwizard 運行狀況檢查 在評論部分讓我知道您的問題。 學習愉快! [源碼下載](//howtodoinjava.com/wp-content/downloads/DropWizardExample.zip) 參考: [Dropwizard 文檔](http://metrics.dropwizard.io/3.1.0/manual/healthchecks/)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看