SpringBoot?是為了簡化?Spring?應用的創建、運行、調試、部署等一系列問題而誕生的產物,自動裝配的特性讓我們可以更好的關注業務本身而不是外部的XML配置,我們只需遵循規范,引入相關的依賴就可以輕易的搭建出一個 WEB 工程
actuator是spring boot項目中非常強大一個功能,有助于對應用程序進行監視和管理,通過?restful api?請求來監管、審計、收集應用的運行情況,針對微服務而言它是必不可少的一個環節…
actuator?的核心部分,它用來監視應用程序及交互,spring-boot-actuator中已經內置了非常多的?Endpoints(health、info、beans、httptrace、shutdown等等),同時也允許我們自己擴展自己的端點
Spring Boot 2.0?中的端點和之前的版本有較大不同,使用時需注意。另外端點的監控機制也有很大不同,啟用了不代表可以直接訪問,還需要將其暴露出來,傳統的management.security管理已被標記為不推薦。
| id | desc | Sensitive |
|---|---|---|
| auditevents | 顯示當前應用程序的審計事件信息 | Yes |
| beans | 顯示應用Spring Beans的完整列表 | Yes |
| caches | 顯示可用緩存信息 | Yes |
| conditions | 顯示自動裝配類的狀態及及應用信息 | Yes |
| configprops | 顯示所有 @ConfigurationProperties 列表 | Yes |
| env | 顯示 ConfigurableEnvironment 中的屬性 | Yes |
| flyway | 顯示 Flyway 數據庫遷移信息 | Yes |
| health | 顯示應用的健康信息(未認證只顯示status,認證顯示全部信息詳情) |
No |
| info | 顯示任意的應用信息(在資源文件寫info.xxx即可) | No |
| liquibase | 展示Liquibase 數據庫遷移 | Yes |
| metrics | 展示當前應用的 metrics 信息 | Yes |
| mappings | 顯示所有 @RequestMapping 路徑集列表 | Yes |
| scheduledtasks | 顯示應用程序中的計劃任務 | Yes |
| sessions | 允許從Spring會話支持的會話存儲中檢索和刪除用戶會話。 | Yes |
| shutdown | 允許應用以優雅的方式關閉(默認情況下不啟用) | Yes |
| threaddump | 執行一個線程dump | Yes |
| httptrace | 顯示HTTP跟蹤信息(默認顯示最后100個HTTP請求 - 響應交換) | Yes |
在?pom.xml?中添加?spring-boot-starter-actuator?的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
注意事項
如果要訪問info接口想獲取maven中的屬性內容請記得添加如下內容
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在?application.properties?文件中配置actuator的相關配置,其中info開頭的屬性,就是訪問info端點中顯示的相關內容,值得注意的是Spring Boot2.x中,默認只開放了info、health兩個端點,剩余的需要自己通過配置management.endpoints.web.exposure.include屬性來加載(有include自然就有exclude,不做詳細概述了)。如果想單獨操作某個端點可以使用management.endpoint.端點.enabled屬性進行啟用或禁用
# 描述信息
info.blog-url=http://blog.battcn.com
info.author=Levin
info.version=@project.version@
# 加載所有的端點/默認只加載了 info / health
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
# 可以關閉制定的端點
management.endpoint.shutdown.enabled=false
# 路徑映射,將 health 路徑映射成 rest_health 那么在訪問 health 路徑將為404,因為原路徑已經變成 rest_health 了,一般情況下不建議使用
# management.endpoints.web.path-mapping.health=rest_health
簡單測試
啟動項目,訪問?http://localhost:8080/actuator/info?看到如下內容代表配置成功
{
"blog-url": "http://blog.battcn.com",
"author": "Levin",
"version": "0.0.1-SNAPSHOT"
}
上面講了很多都是配置相關,以及自帶的一些端點,在實際應用中有時候默認并不能滿足我們的要求,比如Spring Boot默認的健康端點就很有可能不能滿足
下列是依賴spring-boot-xxx-starter后相關HealthIndicator的實現(通過management.health.defaults.enabled?屬性可以禁用它們),但想要獲取一些額外的信息時,自定義的作用就體現出來了…
| 名稱 | 描述 |
|---|---|
| CassandraHealthIndicator | 檢查?Cassandra?數據庫是否啟動。 |
| DiskSpaceHealthIndicator | 檢查磁盤空間不足。 |
| DataSourceHealthIndicator | 檢查是否可以獲得連接?DataSource。 |
| ElasticsearchHealthIndicator | 檢查?Elasticsearch?集群是否啟動。 |
| InfluxDbHealthIndicator | 檢查?InfluxDB?服務器是否啟動。 |
| JmsHealthIndicator | 檢查?JMS?代理是否啟動。 |
| MailHealthIndicator | 檢查郵件服務器是否啟動。 |
| MongoHealthIndicator | 檢查?Mongo?數據庫是否啟動。 |
| Neo4jHealthIndicator | 檢查?Neo4j?服務器是否啟動。 |
| RabbitHealthIndicator | 檢查?Rabbit?服務器是否啟動。 |
| RedisHealthIndicator | 檢查?Redis?服務器是否啟動。 |
| SolrHealthIndicator | 檢查?Solr?服務器是否已啟動。 |
實現HealthIndicator接口,根據自己的需要判斷返回的狀態是UP還是DOWN,功能簡單。
package com.battcn.health;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
/**
* <p>自定義健康端點</p>
*
* @author Levin
* @since 2018/5/24 0024
*/
@Component("my1")
public class MyHealthIndicator implements HealthIndicator {
private static final String VERSION = "v1.0.0";
@Override
public Health health() {
int code = check();
if (code != 0) {
Health.down().withDetail("code", code).withDetail("version", VERSION).build();
}
return Health.up().withDetail("code", code)
.withDetail("version", VERSION).up().build();
}
private int check() {
return 0;
}
}
簡單測試
啟動項目,訪問?http://localhost:8080/actuator/health?看到如下內容代表配置成功
{
"status": "UP",
"details": {
"my1": {
"status": "UP",
"details": {
"code": 0,
"version": "v1.0.0"
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 100944310272,
"free": 55071866880,
"threshold": 10485760
}
}
}
}
繼承AbstractHealthIndicator抽象類,重寫doHealthCheck方法,功能比第一種要強大一點點,默認的DataSourceHealthIndicator 、 RedisHealthIndicator?都是這種寫法,內容回調中還做了異常的處理。
package com.battcn.health;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;
/**
* <p>自定義健康端點</p>
* <p>功能更加強大一點,DataSourceHealthIndicator / RedisHealthIndicator 都是這種寫法</p>
*
* @author Levin
* @since 2018/5/24 0024
*/
@Component("my2")
public class MyAbstractHealthIndicator extends AbstractHealthIndicator {
private static final String VERSION = "v1.0.0";
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
int code = check();
if (code != 0) {
builder.down().withDetail("code", code).withDetail("version", VERSION).build();
}
builder.withDetail("code", code)
.withDetail("version", VERSION).up().build();
}
private int check() {
return 0;
}
}
簡單測試
啟動項目,訪問?http://localhost:8080/actuator/health?看到如下內容代表配置成功
{
"status": "UP",
"details": {
"my2": {
"status": "UP",
"details": {
"code": 0,
"version": "v1.0.0"
}
},
"my1": {...},
"diskSpace": {...}
}
}
上面介紹的?info、health?都是spring-boot-actuator內置的,真正要實現自己的端點還得通過@Endpoint、 @ReadOperation、@WriteOperation、@DeleteOperation。
注解介紹
不同請求的操作,調用時缺少必需參數,或者使用無法轉換為所需類型的參數,則不會調用操作方法,響應狀態將為400(錯誤請求)
package com.battcn.endpoint;import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import java.util.HashMap;
import java.util.Map;
/**
@since 2018/5/24 0024
*/
@Endpoint(id = "battcn")
public class MyEndPoint {
@ReadOperation
public Map<String, String> hello() {
Map<String, String> result = new HashMap<>();
result.put("author", "Levin");
result.put("age", "24");
result.put("email", "1837307557@qq.com");
return result;
}
}
以為這就大功告成了嗎,現實告訴我的是`spring-boot`默認是不認識這玩意的,得申明成一個`Bean`(請看?**主函數**)
package com.battcn;
import com.battcn.endpoint.MyEndPoint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Levin
*/
@SpringBootApplication
public class Chapter13Application {
public static void main(String[] args) {
SpringApplication.run(Chapter13Application.class, args);
}
@Configuration
static class MyEndpointConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public MyEndPoint myEndPoint() {
return new MyEndPoint();
}
}
}
完成準備事項后,啟動Chapter13Application?訪問?http://localhost:8080/actuator/battcn?看到如下內容代表配置成功…
{
"author": "Levin",
"age": "24",
"email": "1837307557@qq.com"
}
參考文檔:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready
目前很多大佬都寫過關于?SpringBoot?的教程了,如有雷同,請多多包涵,本教程基于最新的?spring-boot-starter-parent:2.0.2.RELEASE編寫,包括新版本的特性都會一起介紹…
本文的重點是你有沒有收獲與成長,其余的都不重要,希望讀者們能謹記這一點。同時我經過多年的收藏目前也算收集到了一套完整的學習資料,包括但不限于:分布式架構、高可擴展、高性能、高并發、Jvm性能調優、Spring,MyBatis,Nginx源碼分析,Redis,ActiveMQ、Mycat、Netty、Kafka、Mysql、Zookeeper、Tomcat、Docker、Dubbo、Nginx等多個知識點高級進階干貨,希望對想成為架構師的朋友有一定的參考和幫助
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。