SpringBoot:強大的Actuator服務(wù)監(jiān)控與管理

SpringBoot?是為了簡化?Spring?應(yīng)用的創(chuàng)建、運行、調(diào)試、部署等一系列問題而誕生的產(chǎn)物,自動裝配的特性讓我們可以更好的關(guān)注業(yè)務(wù)本身而不是外部的XML配置,我們只需遵循規(guī)范,引入相關(guān)的依賴就可以輕易的搭建出一個 WEB 工程

創(chuàng)新互聯(lián)建站是專業(yè)的沙依巴克網(wǎng)站建設(shè)公司,沙依巴克接單;提供網(wǎng)站建設(shè)、網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行沙依巴克網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

actuatorspring boot項目中非常強大一個功能,有助于對應(yīng)用程序進行監(jiān)視和管理,通過?restful api?請求來監(jiān)管、審計、收集應(yīng)用的運行情況,針對微服務(wù)而言它是必不可少的一個環(huán)節(jié)…

Endpoints

actuator?的核心部分,它用來監(jiān)視應(yīng)用程序及交互,spring-boot-actuator中已經(jīng)內(nèi)置了非常多的?Endpoints(health、info、beans、httptrace、shutdown等等),同時也允許我們自己擴展自己的端點

Spring Boot 2.0?中的端點和之前的版本有較大不同,使用時需注意。另外端點的監(jiān)控機制也有很大不同,啟用了不代表可以直接訪問,還需要將其暴露出來,傳統(tǒng)的management.security管理已被標記為不推薦。

內(nèi)置Endpoints

iddescSensitive
auditevents 顯示當前應(yīng)用程序的審計事件信息 Yes
beans 顯示應(yīng)用Spring Beans的完整列表 Yes
caches 顯示可用緩存信息 Yes
conditions 顯示自動裝配類的狀態(tài)及及應(yīng)用信息 Yes
configprops 顯示所有 @ConfigurationProperties 列表 Yes
env 顯示 ConfigurableEnvironment 中的屬性 Yes
flyway 顯示 Flyway 數(shù)據(jù)庫遷移信息 Yes
health 顯示應(yīng)用的健康信息(未認證只顯示status,認證顯示全部信息詳情) No
info 顯示任意的應(yīng)用信息(在資源文件寫info.xxx即可) No
liquibase 展示Liquibase 數(shù)據(jù)庫遷移 Yes
metrics 展示當前應(yīng)用的 metrics 信息 Yes
mappings 顯示所有 @RequestMapping 路徑集列表 Yes
scheduledtasks 顯示應(yīng)用程序中的計劃任務(wù) Yes
sessions 允許從Spring會話支持的會話存儲中檢索和刪除用戶會話。 Yes
shutdown 允許應(yīng)用以優(yōu)雅的方式關(guān)閉(默認情況下不啟用) Yes
threaddump 執(zhí)行一個線程dump Yes
httptrace 顯示HTTP跟蹤信息(默認顯示最后100個HTTP請求 - 響應(yīng)交換) Yes

導(dǎo)入依賴

在?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中的屬性內(nèi)容請記得添加如下內(nèi)容

<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的相關(guān)配置,其中info開頭的屬性,就是訪問info端點中顯示的相關(guān)內(nèi)容,值得注意的是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

# 可以關(guān)閉制定的端點
management.endpoint.shutdown.enabled=false

# 路徑映射,將 health 路徑映射成 rest_health 那么在訪問 health 路徑將為404,因為原路徑已經(jīng)變成 rest_health 了,一般情況下不建議使用
# management.endpoints.web.path-mapping.health=rest_health

簡單測試

啟動項目,訪問?http://localhost:8080/actuator/info?看到如下內(nèi)容代表配置成功

{
  "blog-url": "http://blog.battcn.com",
  "author": "Levin",
  "version": "0.0.1-SNAPSHOT"
}

自定義 - 重點

上面講了很多都是配置相關(guān),以及自帶的一些端點,在實際應(yīng)用中有時候默認并不能滿足我們的要求,比如Spring Boot默認的健康端點就很有可能不能滿足

默認裝配 HealthIndicators

下列是依賴spring-boot-xxx-starter后相關(guān)HealthIndicator的實現(xiàn)(通過management.health.defaults.enabled?屬性可以禁用它們),但想要獲取一些額外的信息時,自定義的作用就體現(xiàn)出來了…

名稱描述
CassandraHealthIndicator 檢查?Cassandra?數(shù)據(jù)庫是否啟動。
DiskSpaceHealthIndicator 檢查磁盤空間不足。
DataSourceHealthIndicator 檢查是否可以獲得連接?DataSource
ElasticsearchHealthIndicator 檢查?Elasticsearch?集群是否啟動。
InfluxDbHealthIndicator 檢查?InfluxDB?服務(wù)器是否啟動。
JmsHealthIndicator 檢查?JMS?代理是否啟動。
MailHealthIndicator 檢查郵件服務(wù)器是否啟動。
MongoHealthIndicator 檢查?Mongo?數(shù)據(jù)庫是否啟動。
Neo4jHealthIndicator 檢查?Neo4j?服務(wù)器是否啟動。
RabbitHealthIndicator 檢查?Rabbit?服務(wù)器是否啟動。
redisHealthIndicator 檢查?Redis?服務(wù)器是否啟動。
SolrHealthIndicator 檢查?Solr?服務(wù)器是否已啟動。

健康端點(第一種方式)

實現(xiàn)HealthIndicator接口,根據(jù)自己的需要判斷返回的狀態(tài)是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?看到如下內(nèi)容代表配置成功

{
  "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?都是這種寫法,內(nèi)容回調(diào)中還做了異常的處理。

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?看到如下內(nèi)容代表配置成功

{
  "status": "UP",
  "details": {
    "my2": {
      "status": "UP",
      "details": {
        "code": 0,
        "version": "v1.0.0"
      }
    },
    "my1": {...},
    "diskSpace": {...}
  }
}

定義自己的端點

上面介紹的?info、health?都是spring-boot-actuator內(nèi)置的,真正要實現(xiàn)自己的端點還得通過@Endpoint、 @ReadOperation、@WriteOperation、@DeleteOperation。

注解介紹

不同請求的操作,調(diào)用時缺少必需參數(shù),或者使用無法轉(zhuǎn)換為所需類型的參數(shù),則不會調(diào)用操作方法,響應(yīng)狀態(tài)將為400(錯誤請求)

  • @Endpoint?構(gòu)建 rest api 的唯一路徑
  • @ReadOperation?GET請求,響應(yīng)狀態(tài)為 200 如果沒有返回值響應(yīng) 404(資源未找到)
  • @WriteOperation?POST請求,響應(yīng)狀態(tài)為 200 如果沒有返回值響應(yīng) 204(無響應(yīng)內(nèi)容)
  • @DeleteOperation?DELETE請求,響應(yīng)狀態(tài)為 200 如果沒有返回值響應(yīng) 204(無響應(yīng)內(nèi)容)
    
    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;

/**

  • <p>@Endpoint 是構(gòu)建 rest 的唯一路徑 </p>
  • 不同請求的操作,調(diào)用時缺少必需參數(shù),或者使用無法轉(zhuǎn)換為所需類型的參數(shù),則不會調(diào)用操作方法,響應(yīng)狀態(tài)將為400(錯誤請求)
  • <P>@ReadOperation = GET 響應(yīng)狀態(tài)為 200 如果沒有返回值響應(yīng) 404(資源未找到) </P>
  • <P>@WriteOperation = POST 響應(yīng)狀態(tài)為 200 如果沒有返回值響應(yīng) 204(無響應(yīng)內(nèi)容) </P>
  • <P>@DeleteOperation = DELETE 響應(yīng)狀態(tài)為 200 如果沒有返回值響應(yīng) 204(無響應(yīng)內(nèi)容) </P>
  • @author Levin
  • @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;
    }
    }

    
    以為這就大功告成了嗎,現(xiàn)實告訴我的是`spring-boot`默認是不認識這玩意的,得申明成一個`Bean`(請看?**主函數(shù)**)

主函數(shù)

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?看到如下內(nèi)容代表配置成功…

{
  "author": "Levin",
  "age": "24",
  "email": "1837307557@qq.com"
}

總結(jié)

參考文檔:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready

目前很多大佬都寫過關(guān)于?SpringBoot?的教程了,如有雷同,請多多包涵,本教程基于最新的?spring-boot-starter-parent:2.0.2.RELEASE編寫,包括新版本的特性都會一起介紹…

本文的重點是你有沒有收獲與成長,其余的都不重要,希望讀者們能謹記這一點。同時我經(jīng)過多年的收藏目前也算收集到了一套完整的學(xué)習(xí)資料,包括但不限于:分布式架構(gòu)、高可擴展、高性能、高并發(fā)、Jvm性能調(diào)優(yōu)、Spring,MyBatis,Nginx源碼分析,Redis,ActiveMQ、Mycat、Netty、Kafka、MySQL、Zookeeper、Tomcat、Docker、Dubbo、Nginx等多個知識點高級進階干貨,希望對想成為架構(gòu)師的朋友有一定的參考和幫助

感興趣的可以加一下三千人Java技術(shù)交流分享群:“708 701 457”免費獲取

文章名稱:SpringBoot:強大的Actuator服務(wù)監(jiān)控與管理
分享路徑:http://muchs.cn/article4/jchdie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、虛擬主機、定制開發(fā)動態(tài)網(wǎng)站、標簽優(yōu)化、

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計