新聞中心
朋友們好,在本教程中,我們將了解 Spring 執(zhí)行器及其對(duì)我們的幫助。

成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括三臺(tái)網(wǎng)站建設(shè)、三臺(tái)網(wǎng)站制作、三臺(tái)網(wǎng)頁(yè)制作以及三臺(tái)網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,三臺(tái)網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到三臺(tái)省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
1.什么是彈簧執(zhí)行器?
2. Maven項(xiàng)目或Gradle項(xiàng)目如何添加Spring actuator?
3、創(chuàng)建一個(gè)Spring Boot項(xiàng)目,依賴Spring Actuator。
4. 使用 Spring Actuator Endpoints 監(jiān)控應(yīng)用程序。
什么是彈簧執(zhí)行器?
開(kāi)發(fā)應(yīng)用程序并將其部署到生產(chǎn)環(huán)境后,檢查已啟動(dòng)和運(yùn)行的應(yīng)用程序的運(yùn)行狀況非常重要,特別是對(duì)于銀行應(yīng)用程序等任務(wù)關(guān)鍵型應(yīng)用程序,如果面向客戶的應(yīng)用程序存在下降,將直接影響銀行的業(yè)務(wù)。
在傳統(tǒng)的方式中,在 Spring Actuator 之前,我們需要編寫(xiě)代碼來(lái)檢查應(yīng)用程序的 Health,但是使用 Spring Actuator,我們不需要編寫(xiě)任何 Health Check 代碼,但是 Spring Actuator 提供了一些開(kāi)箱即用的端點(diǎn),可以對(duì)于應(yīng)用程序的健康檢查非常有用。
如何將 Spring actuator 添加到 Maven 項(xiàng)目或 Gradle 項(xiàng)目?
馬文
<依賴項(xiàng)>
<依賴>
org.springframework.boot
spring-boot-starter-actuator
依賴>
依賴>
搖籃
依賴{
編譯(“org.springframework.boot:spring-boot-starter-actuator”)
}使用 Spring Actuator 依賴項(xiàng)創(chuàng)建 Spring Boot 項(xiàng)目
讓我們繼續(xù)使用 Spring Initializer https://start.spring.io/創(chuàng)建一個(gè)具有 Spring Actuator 依賴項(xiàng)(以及 Web 和 DevTools)的 Spring Boot 項(xiàng)目。
請(qǐng)注意,在編寫(xiě)本教程時(shí),Spring Boot 版本為 2.1.0。
在 Eclipse 或您選擇的任何其他 IDE 中導(dǎo)入項(xiàng)目并運(yùn)行SpringActuatorApplication.java。
您將在 Eclipse 控制臺(tái)中看到以下內(nèi)容:
這表明嵌入式 Tomcat 已在 8080 端口啟動(dòng),SpringActuatorApplication 已在 Tomcat 上啟動(dòng)。同樣在控制臺(tái)日志中,您可以看到執(zhí)行器端點(diǎn)通過(guò) /actuator URI 公開(kāi)。
018-11-09 20:00:29.346 INFO 8338 — [restartedMain]。
osbwembedded.tomcat.TomcatWebServer:Tomcat 在端口上啟動(dòng):8080 (http) 與上下文路徑”。
2018-11-09 20:00:29.354 INFO 8338 — [restartedMain]。
nbjsSpringActuatorApplication:在 9.273 秒內(nèi)啟動(dòng) SpringActuatorApplication(JVM 運(yùn)行時(shí)間為 11.823)。
2018-11-09 20:00:29.190INFO 8338 — [restartedMain]。
osbaeweb.EndpointLinksResolver:在基本路徑“/actuator”下暴露 2 個(gè)端點(diǎn)。
使用 Spring Actuator Endpoints 監(jiān)控應(yīng)用程序。
正如我們上面所討論的,Spring 執(zhí)行器提供了一些開(kāi)箱即用的端點(diǎn),我們可以使用它們來(lái)監(jiān)控應(yīng)用程序的健康狀況。
啟用端點(diǎn)
默認(rèn)情況下,除關(guān)閉之外的所有端點(diǎn)都已啟用。要啟用端點(diǎn),請(qǐng)?jiān)?application.properties 文件中使用以下屬性。
management.endpoint..enabled
例子:
要啟用關(guān)閉端點(diǎn),我們需要在 application.properties 文件中創(chuàng)建以下條目:
management.endpoint.shutdown.enabled=true
或者,我們可以禁用所有端點(diǎn),然后有選擇地啟用我們想要的端點(diǎn)。使用以下配置,除了 info 之外的所有端點(diǎn)都將被禁用。
management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true
端點(diǎn)執(zhí)行器
讓我們點(diǎn)擊 URLhttp://localhost:8080/actuator 并查看端點(diǎn)。
注意:我正在使用 Postman 測(cè)試端點(diǎn),因?yàn)樗越Y(jié)構(gòu)良好的格式顯示 JSON。您可以自由使用任何其他此類工具或僅使用瀏覽器。
在 Spring Actuator 中暴露端點(diǎn)
正如您已經(jīng)注意到的那樣,這里只能看到健康和信息端點(diǎn)。這是因?yàn)檫@些是默認(rèn)公開(kāi)的唯一端點(diǎn)。出于安全原因,默認(rèn)情況下不會(huì)公開(kāi)所有端點(diǎn),因?yàn)樗鼈兛赡馨恍┟舾行畔?,因此可能?huì)受到損害。
暴露特定端點(diǎn)
如果我們想通過(guò) Web(Http) 公開(kāi)其他端點(diǎn),我們需要在 application.properties 文件中創(chuàng)建以下條目。
management.endpoints.web.exposure.include=
例子
management.endpoints.web.exposure.include= health,info,env
現(xiàn)在在 application.properties 中添加上述條目后,讓我們?cè)俅吸c(diǎn)擊 URLhttp://localhost:8080/actuator。
正如我們?cè)谙旅娴钠聊唤貓D中看到的,env 端點(diǎn)也被啟用。
暴露所有端點(diǎn)
如果我們想啟用所有端點(diǎn),我們可以在 application.properties 中使用通配符 *,如下所示。
management.endpoints.web.exposure.include=*。
暴露除少數(shù)特定端點(diǎn)外的所有端點(diǎn)
以下兩個(gè)條目將啟用所有端點(diǎn),但僅禁用 env 端點(diǎn)。
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env
禁用 HTTP 端點(diǎn)
如果您不想通過(guò) HTTP 公開(kāi)端點(diǎn),可以通過(guò)在 application.properties 中配置以下內(nèi)容來(lái)完成:
management.server.port=-1
或者,您可以在 application.properties 中配置以下內(nèi)容:
management.endpoints.web.exposure.exclude=*
自定義執(zhí)行器 URL 以訪問(wèn)各種端點(diǎn)
默認(rèn)情況下,所有 Web 端點(diǎn)都在 /actuator 下可用,其 URL 格式為 /actuator/{id}。
但是,可以通過(guò)在 application.properties 中配置以下屬性來(lái)配置基本路徑 /actuator。
management.endpoints.web.base-path
例如,如果要將基本 URL 設(shè)為 /monitor 而不是 /actuator。
則可以在 application.properties 中進(jìn)行如下配置:
management.endpoints.web.base-path=/monitor
這樣,所有端點(diǎn)都可以作為 /monitor/{id} 而不是 /actuator/{id} 訪問(wèn)。
Spring Boot 執(zhí)行器端點(diǎn)
讓我們討論一些最重要的端點(diǎn)。
健康
健康端點(diǎn)提供應(yīng)用程序的狀態(tài),如果它是啟動(dòng)和運(yùn)行與否。這對(duì)于在生產(chǎn)中監(jiān)控應(yīng)用程序的運(yùn)行狀況非常重要。該端點(diǎn)可以與監(jiān)控應(yīng)用程序集成,將非常有助于告知應(yīng)用程序的實(shí)時(shí)運(yùn)行狀況。
健康信息
將暴露多少健康端點(diǎn)信息取決于 application.properties 文件中屬性management.endpoint.health.show-details 的配置。
如果management.endpoint.health.show-details=never,則從不顯示詳細(xì)信息。在這種情況下,您只會(huì)看到以下信息。這也是默認(rèn)行為。
如果management.endpoint.health.show-details=always,詳細(xì)信息會(huì)顯示給所有用戶。所以我們可以在下面的響應(yīng)中看到,我們也有磁盤空間信息。如果您的應(yīng)用程序連接到數(shù)據(jù)庫(kù),那么您還將獲得有關(guān)數(shù)據(jù)庫(kù)運(yùn)行狀況的信息。
如果management.endpoint.health.show-details=when-authorized,詳細(xì)信息僅顯示給授權(quán)用戶。授權(quán)角色可以使用management.endpoint.health.roles 屬性進(jìn)行配置。
自動(dòng)配置的健康指標(biāo)
Spring Boot Actuator 有許多自動(dòng)配置的 HeathIndicators 來(lái)檢查應(yīng)用程序各個(gè)部分的健康狀況。例如,Spring Boot Actuator 提供了 DiskspaceHealthIndicator,它提供了有關(guān)應(yīng)用程序使用的磁盤空間健康狀況的信息。同樣,如果您使用的是 MongoDB,那么 MongoHealthIndicator 將檢查 Mongo DB 的健康狀況(是否為 UP)并顯示相關(guān)信息。默認(rèn)情況下,最終應(yīng)用程序狀態(tài)由 HealthAggregator 派生,它基本上根據(jù)狀態(tài)的有序列表對(duì)來(lái)自每個(gè) HealthIndicator 的狀態(tài)進(jìn)行排序。排序列表中的第一個(gè)狀態(tài)用作應(yīng)用程序的最終狀態(tài)。
禁用所有自動(dòng)配置的運(yùn)行狀況指標(biāo)
這些健康指標(biāo)默認(rèn)啟用,但是,可以使用以下屬性禁用它們:
management.health.defaults.enabled=false
禁用單個(gè)自動(dòng)配置的運(yùn)行狀況指示器或者,也可以禁用單個(gè) HealthIndicator,如下所示,例如禁用磁盤空間的健康檢查:
management.health.diskspace.enabled=false
注意:任何 HealthIndicator 的標(biāo)識(shí)符都是沒(méi)有 HealthIndicator 后綴的 bean 的名稱。
例如 :
DiskSpaceHealthIndicator diskspace
MongoHealthIndicator mongo
CassandraHealthIndicator cassandra
DataSourceHealthIndicator datasource
等等…
自定義健康指標(biāo)
除了 Spring Boot Actuator 提供的內(nèi)置 HealthIndicators 之外,我們還可以創(chuàng)建自己的自定義 Health Indicators。為此,您需要?jiǎng)?chuàng)建一個(gè)實(shí)現(xiàn) HealthIndicator 接口并實(shí)現(xiàn)其 health() 方法的類,并將 Health 作為響應(yīng)返回,相關(guān)信息如下:
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class CustomHealthIndicator implements HealthIndicator {
@Override
public Health health() {
int errorCode = 0;
// In the above line,I am simple assigning zero,but you can call Health check related code like below commented line and that method can return the appropriate code.
// int errorCode = performHealthCheck();
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
}
return Health.up().build();
}
}
現(xiàn)在讓我們?cè)俅吸c(diǎn)擊健康端點(diǎn),看看我們的自定義健康指標(biāo)是否得到反映。
正如我們?cè)谏厦娴钠聊唤貓D中看到的那樣,已包含自定義健康檢查。
每個(gè)組件的健康狀況
也可以檢查單個(gè)組件的健康狀態(tài)。在上面的示例中,我們看到了自定義健康狀態(tài)以及 diskSpace 健康狀態(tài)。
如果我們只想查看 diskSpace 的健康狀況,那么我們可以執(zhí)行以下操作:http://localhost:8080/actuator/health/diskSpace。
信息
info 端點(diǎn)提供有關(guān)應(yīng)用程序的一般信息,它從 build-info.properties 或 git.properties 等文件或從 application.properties 中的關(guān)鍵信息下的任何屬性中獲取。
在我們的項(xiàng)目中,沒(méi)有這樣的文件,所以如果我們點(diǎn)擊 info 端點(diǎn),它將只顯示空響應(yīng),如下所示:
如果存在META-INF/build-info.properties 文件,Spring Boot Actuator 會(huì)顯示與構(gòu)建相關(guān)的信息。 build-info目標(biāo)生成帶有項(xiàng)目坐標(biāo)和構(gòu)建時(shí)間的此類文件。它還允許您添加任意數(shù)量的附加屬性。
讓我們?cè)谖覀冺?xiàng)目的 pom.xml 中添加一個(gè) build-info 目標(biāo),如下所示在 spring-boot-maven-plugin 插件中。
org.springframework.boot
spring-boot-maven-plugin
2.1.0.RELEASE
build-info
UTF-8
UTF-8
${maven.compiler.source}
${maven.compiler.target}
現(xiàn)在讓我們?cè)俅吸c(diǎn)擊 info 端點(diǎn),我們可以看到構(gòu)建信息如下:
此外,我們可以在 application.properties 中的 info 鍵下添加應(yīng)用程序信息,如下所示,同樣將顯示在 /info 端點(diǎn)中。
info.application.name=spring-actuator
info.application.description=spring boot actuator application
info.application.version=0.0.1-SNAPSHOT
豆子
beans 端點(diǎn)為 Spring bean 容器中定義的所有 bean 提供了有關(guān)每個(gè) bean 的以下信息:
aliases : Names of any aliases
Scope : Scope of bean
type : Fully qualified type of a bean.
resource : Resource(class) in which bean is defined.
dependencies :names of dependent beans.
例如,我創(chuàng)建了一個(gè)名為 TestController.java 的 RestController 并注入了一個(gè)名為 TestService.java 的 bean
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/messages")
public String getMessage() {
return "Hello";
}
}
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestService {
}
你可以看到它是如何反映在下面帶有 id testController 的截圖中的。
configprops
configProps 端點(diǎn)為您提供所有使用 @ConfigurationProperties 注釋的 bean。
在上面的屏幕截圖中,我們可以看到 Spring 框架本身預(yù)定義的兩個(gè) bean,并使用 @ConfigurationProperties 進(jìn)行注釋,因此顯示在此端點(diǎn)下。
下面的屏幕截圖顯示了帶有 @ConfigurationProperties 注釋的 HttpTraceProperties 的源代碼。
env
env 端點(diǎn)按以下順序?yàn)槟峁┧刑囟ㄓ诃h(huán)境的信息:
System Properties - JVM specific(Platform Independent)
System Env. or Env. Variables - Operating System specific(Platform Dependent)
application level configuration - Defined in application.properties
堆轉(zhuǎn)儲(chǔ)
heapdump 端點(diǎn)從應(yīng)用程序 JVM 提供堆轉(zhuǎn)儲(chǔ)。此端點(diǎn)以 HPROF 格式返回二進(jìn)制數(shù)據(jù)。由于返回的數(shù)據(jù)通常很大,您應(yīng)該將其保存并分析。
記錄器
loggers 端點(diǎn)提供應(yīng)用程序的記錄器及其配置級(jí)別、有效級(jí)別(如果此記錄器的配置級(jí)別為空并且它也是父級(jí)的,則有效級(jí)別將是根記錄器的記錄器級(jí)別)。
levels 屬性告訴日志框架支持哪些所有級(jí)別。
特定記錄器的記錄器信息
要獲取特定記錄器的記錄器信息,請(qǐng)?jiān)?/loggers 端點(diǎn)之后的 URL 中傳遞記錄器的名稱/ID,如下所示:
http://localhost:8080/actuator/loggers/nl.blogpsot.javasolutionsguide.springactuator.SpringActuatorApplication。
指標(biāo)
指標(biāo)端點(diǎn)為您提供可以為您的應(yīng)用程序跟蹤的所有指標(biāo)。
檢查單個(gè)指標(biāo)
您可以通過(guò)將特定指標(biāo)傳遞到 /metrics 端點(diǎn)之后的 URL 來(lái)跟蹤單個(gè)指標(biāo),如下所示:
http://localhost:8080/actuator/metrics/jvm.memory.used。
以上就是 Spring Actuator 的全部?jī)?nèi)容。
網(wǎng)頁(yè)標(biāo)題:SpringBoot執(zhí)行器教程
文章源于:http://fisionsoft.com.cn/article/dhojiog.html


咨詢
建站咨詢
