<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # SpringBoot – 執行器 > 原文: [https://howtodoinjava.com/spring-boot/actuator-endpoints-example/](https://howtodoinjava.com/spring-boot/actuator-endpoints-example/) 在此 **Spring Boot 執行器教程**中,了解可用于任何運行應用程序的內置 HTTP 端點,以用于不同的**監視和管理目的**。 在 spring 框架之前,如果我們必須在應用程序中引入這種類型的監視功能,則必須手動開發所有這些組件,并且這些組件也非常符合我們的需求。 但是通過 Spring Boot,我們有了`Actuator`模塊,這非常容易。 我們只需要配置幾件事就可以完成 – 所有管理和監視相關信息都可以輕松獲得。 讓我們學習配置 **Spring Boot 執行器端點**。 ## 1\. SpringBoot 執行器模塊 使用 Spring Boot 的模塊`Actuator`,您可以監視和管理生產環境中的應用程序使用情況,而無需對其進行編碼和配置。 這些監視和管理信息通過 [REST](https://restfulapi.net)(如端點 URL)公開。 #### 1.1. 執行器 Maven 依賴 ```java <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` #### 1.2. 重要的執行器端點 一些重要且廣泛使用的執行器端點如下: | 端點 | 用法 | | --- | --- | | `/env` | 返回當前環境中的屬性列表 | | `/health` | 返響應用程序運行狀況信息。 | | `/auditevents` | 返回所有自動配置的候選對象以及應用它們“被”或“未被”的原因。 | | `/beans` | 返響應用程序中所有 Spring Bean 的完整列表。 | | `/trace` | 返回跟蹤日志(默認情況下,最近的 100 個 HTTP 請求)。 | | `/dump` | 它執行線程轉儲。 | | `/metrics` | 它顯示了一些有用的指標信息,例如 JVM 內存使用情況,系統 CPU 使用情況,打開的文件等等。 | #### 1.3. 與安全性相關的屬性 默認情況下,所有執行器端點均啟用 [SpringSecurity](https://howtodoinjava.com/spring-security-tutorial/)。它是內置的[基于表單的身份驗證](https://howtodoinjava.com/spring/spring-boot/role-based-security-jaxrs-annotations/),其中的用戶 ID 為用戶,并隨機生成一個密碼。 然后需要以下條目才能為您的敏感端點自定義[基本認證安全性](https://howtodoinjava.com/spring/spring-security/http-basic-authentication-example-using-spring-3/)。 ```java management.security.enabled = true management.security.roles = ADMIN security.basic.enabled = true security.user.name = admin security.user.password = admin ``` 請注意,默認情況下,要訪問執行器受限的端點,您必須具有`ACTUATOR`角色。 您需要通過`management.security.roles`屬性覆蓋此配置。 #### 1.4. 使用`WebSecurityConfigurerAdapter`的執行器安全性 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN"); } } ``` #### 1.5. CORS 支持 [CORS](https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/) 支持默認為禁用,并且僅在設置了`endpoints.cors.allowed-origins`屬性后才啟用。 ```java endpoints.cors.allowed-origins = http://example.com endpoints.cors.allowed-methods = GET,POST ``` ## 2\. Spring Boot 執行器端點示例 在此示例中,我們將創建一個簡單的字符串啟動應用程序,并訪問執行器端點以進一步了解它們。 #### 2.1. 開發環境 * JDK 1.8,Eclipse,Maven – 開發環境 * SpringBoot – 基礎應用程序框架 * SpringBoot 執行器 – 管理端點 #### 2.2. 創建 Maven 項目 首先從[ Spring 初始化器](https://start.spring.io/)站點創建一個具有`Web`,`Rest Repositories`和`Actuator`依賴項的 spring boot 項目。 以壓縮格式下載項目。 解壓縮,然后將 eclipse 中的項目導入為 maven 項目。 ![Spring boot actuator project](https://img.kancloud.cn/5a/06/5a06d115c5254dbc1a55981364fb1129_1345x701.jpg) Spring boot 執行器項目 #### 2.3. 添加簡單的 Rest 端點 現在,向應用程序添加一個簡單的 Rest 端點`/example`。 ```java package com.example.springbootmanagementexample; import java.util.Date; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class SimpleRestController { @GetMapping("/example") public String example() { return "Hello User !! " + new Date(); } } ``` ## 3\. Spring Boot 執行器端點演示 我在`application.properties`文件中添加了`management.security.enabled = false`條目以禁用執行器安全性。 在這里,我對執行器端點響應更感興趣。 使用`mvn clean install`進行 maven 構建,然后使用`java -jar target\spring-boot-management-example-0.0.1-SNAPSHOT.jar`命令啟動應用程序。 這將在默認端口`8080`中啟動一臺 tomcat 服務器,并將在其中部署應用程序。 在瀏覽器中訪問`/example` API,以在服務器上生成少量監視信息。 * `http://localhost:8080/env` 這將提供有關服務器的所有環境配置。 ![Endpoint env Output](https://img.kancloud.cn/95/8c/958c61460076a4759f4fa47de90cff79_779x419.jpg) `env`端點輸出 * `http://localhost:8080/beans` 這將在上下文中加載所有 SpringBean。 ![Endpoint beans Output](https://img.kancloud.cn/5f/fb/5ffb2e0014c78564ea40078bfaa3d677_734x333.jpg) `beans`端點輸出 * `http://localhost:8080/dump` 這將給出當前服務器線程轉儲。 ![Endpoint dump Output](https://img.kancloud.cn/fa/39/fa392d22d3b80adcaecbddf958dbd317_697x374.jpg) `dump`端點輸出 * `http://localhost:8080/health` 這將提供應用程序和服務器的一般運行狀況。 ![Endpoint health Output](https://img.kancloud.cn/1c/2f/1c2f58737b007ac8101e52082186ab63_796x195.jpg) `health`端點輸出 * `http://localhost:8080/metrics` `/metrics`端點列出了所有可用于跟蹤的指標。 ```java { "mem": 316656, "mem.free": 169495, "processors": 4, "instance.uptime": 1449726, "uptime": 1463662, "systemload.average": -1.0, "heap.committed": 263168, "heap.init": 131072, "heap.used": 93672, "heap": 1846272, "nonheap.committed": 54400, ........ } ``` 這些端點將在瀏覽器中提供標準信息。 這些是我們通常引用的基本重要端點,但是如[鏈接](https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html)所述,Spring Boot 提供了更多端點。 ## 4\. 執行器高級配置選項 #### 4.1. 更改管理端點上下文路徑 默認情況下,所有端點都位于應用程序的默認上下文路徑中。 仍然,如果我們需要將這些端點公開在不同的端點中,則需要在`application.properties`中指定它們。 ```java management.context-path=/manage ``` 現在,您將可以在新 URL 下訪問所有執行器端點。 例如 * `/manage/health` * `/manage/dump` * `/manage/env` * `/manage/bean` #### 4.2. 定制管理服務器端口 要自定義管理端點端口,我們需要將此條目添加到`application.properties`文件中。 ```java management.port=8081 ``` ## 5\. 總結 在此 **SpringBoot 執行器示例**中,我們學習了使用很少的簡單配置即可配置管理和監視端點。 因此,下次,您需要添加應用程序運行狀況檢查或添加監視支持,則應考慮添加 Spring 執行器項目并使用這些端點。 隨時在評論部分中提出您的問題。 [下載源碼](https://howtodoinjava.com/wp-content/uploads/2017/10/spring-boot-management-example.zip) 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看