欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot Actuator入門指南

 更新時間:2025年02月10日 11:39:56   作者:flydean程序那些事  
SpringBootActuator是SpringBoot提供的一系列產(chǎn)品級特性,用于監(jiān)控應(yīng)用程序、收集元數(shù)據(jù)和運行情況,通過添加依賴,可以通過HTTP或JMX與外界交互,本文介紹Spring Boot Actuator的相關(guān)知識,感興趣的朋友一起看看吧

Spring Boot Actuator

Spring Boot Actuator 在Spring Boot第一個版本發(fā)布的時候就有了,它為Spring Boot提供了一系列產(chǎn)品級的特性:監(jiān)控應(yīng)用程序,收集元數(shù)據(jù),運行情況或者數(shù)據(jù)庫狀態(tài)等。

使用Spring Boot Actuator我們可以直接使用這些特性而不需要自己去實現(xiàn),它是用HTTP或者JMX來和外界交互。

開始使用Spring Boot Actuator

要想使用Spring Boot Actuator,需要添加如下依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

開始使用Actuator

配好上面的依賴之后,我們使用下面的主程序入口就可以使用Actuator了:

@SpringBootApplication
public class ActuatorApp {
    public static void main(String[] args) {
        SpringApplication.run(ActuatorApp.class, args);
    }
}

啟動應(yīng)用程序,訪問http://localhost:8080/actuator:

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false}}}

我們可以看到actuator默認(rèn)開啟了兩個入口:/health和/info。

如果我們在配置文件里面這樣配置,則可以開啟actuator所有的入口:

management.endpoints.web.exposure.include=*

重啟應(yīng)用程序,再次訪問http://localhost:8080/actuator:

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},"caches-cache":{"href":"http://localhost:8080/actuator/caches/{cache}","templated":true},"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false},"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},"env":{"href":"http://localhost:8080/actuator/env","templated":false},"env-toMatch":{"href":"http://localhost:8080/actuator/env/{toMatch}","templated":true},"loggers-name":{"href":"http://localhost:8080/actuator/loggers/{name}","templated":true},"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},"heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},"threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},"metrics":{"href":"http://localhost:8080/actuator/metrics","templated":false},"metrics-requiredMetricName":{"href":"http://localhost:8080/actuator/metrics/{requiredMetricName}","templated":true},"scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false}}}

我們可以看到actuator暴露的所有入口。

Health Indicators

Health入口是用來監(jiān)控組件的狀態(tài)的,通過上面的入口,我們可以看到Health的入口如下:

"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},

有兩個入口,一個是總體的health,一個是具體的health-path。

我們訪問一下http://localhost:8080/actuator/health:

{"status":"UP"}

上面的結(jié)果實際上是隱藏了具體的信息,我們可以通過設(shè)置

management.endpoint.health.show-details=ALWAYS

來開啟詳情,開啟之后訪問如下:

{"status":"UP","components":{"db":{"status":"UP","details":{"database":"H2","result":1,"validationQuery":"SELECT 1"}},"diskSpace":{"status":"UP","details":{"total":250685575168,"free":12428898304,"threshold":10485760}},"ping":{"status":"UP"}}}

其中的components就是health-path,我們可以訪問具體的某一個components如http://localhost:8080/actuator/health/db:

{"status":"UP","details":{"database":"H2","result":1,"validationQuery":"SELECT 1"}}

就可以看到具體某一個component的信息。

這些Health components的信息都是收集實現(xiàn)了HealthIndicator接口的bean來的。

我們看下怎么自定義HealthIndicator:

@Component
public class CustHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down()
                    .withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }
    public int check() {
        // Our logic to check health
        return 0;
    }
}

再次查看http://localhost:8080/actuator/health, 我們會發(fā)現(xiàn)多了一個Cust的組件:

"components":{"cust":{"status":"UP"} }

在Spring Boot 2.X之后,Spring添加了React的支持,我們可以添加ReactiveHealthIndicator如下:

@Component
public class DownstreamServiceHealthIndicator implements ReactiveHealthIndicator {
    @Override
    public Mono<Health> health() {
        return checkDownstreamServiceHealth().onErrorResume(
                ex -> Mono.just(new Health.Builder().down(ex).build())
        );
    }
    private Mono<Health> checkDownstreamServiceHealth() {
        // we could use WebClient to check health reactively
        return Mono.just(new Health.Builder().up().build());
    }
}

再次查看http://localhost:8080/actuator/health,可以看到又多了一個組件:

"downstreamService":{"status":"UP"}

/info 入口

info顯示了App的大概信息,默認(rèn)情況下是空的。我們可以這樣自定義:

info.app.name=Spring Sample Application
info.app.description=This is my first spring boot application
info.app.version=1.0.0

查看:http://localhost:8080/actuator/info

{"app":{"name":"Spring Sample Application","description":"This is my first spring boot application","version":"1.0.0"}}

/metrics入口

/metrics提供了JVM和操作系統(tǒng)的一些信息,我們看下metrics的目錄,訪問:http://localhost:8080/actuator/metrics:

{"names":["jvm.memory.max","jvm.threads.states","jdbc.connections.active","process.files.max","jvm.gc.memory.promoted","system.load.average.1m","jvm.memory.used","jvm.gc.max.data.size","jdbc.connections.max","jdbc.connections.min","jvm.gc.pause","jvm.memory.committed","system.cpu.count","logback.events","http.server.requests","jvm.buffer.memory.used","tomcat.sessions.created","jvm.threads.daemon","system.cpu.usage","jvm.gc.memory.allocated","hikaricp.connections.idle","hikaricp.connections.pending","jdbc.connections.idle","tomcat.sessions.expired","hikaricp.connections","jvm.threads.live","jvm.threads.peak","hikaricp.connections.active","hikaricp.connections.creation","process.uptime","tomcat.sessions.rejected","process.cpu.usage","jvm.classes.loaded","hikaricp.connections.max","hikaricp.connections.min","jvm.classes.unloaded","tomcat.sessions.active.current","tomcat.sessions.alive.max","jvm.gc.live.data.size","hikaricp.connections.usage","hikaricp.connections.timeout","process.files.open","jvm.buffer.count","jvm.buffer.total.capacity","tomcat.sessions.active.max","hikaricp.connections.acquire","process.start.time"]}

訪問其中具體的某一個組件如下http://localhost:8080/actuator/metrics/jvm.memory.max:

{"name":"jvm.memory.max","description":"The maximum amount of memory in bytes that can be used for memory management","baseUnit":"bytes","measurements":[{"statistic":"VALUE","value":3.456106495E9}],"availableTags":[{"tag":"area","values":["heap","nonheap"]},{"tag":"id","values":["Compressed Class Space","PS Survivor Space","PS Old Gen","Metaspace","PS Eden Space","Code Cache"]}]}

Spring Boot 2.X 的metrics是通過Micrometer來實現(xiàn)的,Spring Boot會自動注冊MeterRegistry。 有關(guān)Micrometer和Spring Boot的結(jié)合使用我們會在后面的文章中詳細講解。

自定義Endpoint

Spring Boot的Endpoint也是可以自定義的:

@Component
@Endpoint(id = "features")
public class FeaturesEndpoint {
    private Map<String, String> features = new ConcurrentHashMap<>();
    @ReadOperation
    public Map<String, String> features() {
        return features;
    }
    @ReadOperation
    public String feature(@Selector String name) {
        return features.get(name);
    }
    @WriteOperation
    public void configureFeature(@Selector String name, String value) {
        features.put(name, value);
    }
    @DeleteOperation
    public void deleteFeature(@Selector String name) {
        features.remove(name);
    }
}

訪問http://localhost:8080/actuator/, 我們會發(fā)現(xiàn)多了一個入口: http://localhost:8080/actuator/features/ 。

上面的代碼中@ReadOperation對應(yīng)的是GET, @WriteOperation對應(yīng)的是PUT,@DeleteOperation對應(yīng)的是DELETE。

@Selector后面對應(yīng)的是路徑參數(shù), 比如我們可以這樣調(diào)用configureFeature方法:

POST /actuator/features/abc HTTP/1.1
Host: localhost:8080
Content-Type: application/json
User-Agent: PostmanRuntime/7.18.0
Accept: */*
Cache-Control: no-cache
Postman-Token: dbb46150-9652-4a4a-95cb-3a68c9aa8544,8a033af4-c199-4232-953b-d22dad78c804
Host: localhost:8080
Accept-Encoding: gzip, deflate
Content-Length: 15
Connection: keep-alive
cache-control: no-cache
{"value":true}

注意,這里的請求BODY是以JSON形式提供的:

{"value":true}

請求URL:/actuator/features/abc 中的abc就是@Selector 中的 name。

我們再看一下GET請求:

http://localhost:8080/actuator/features/

{"abc":"true"}

這個就是我們之前PUT進去的值。

擴展現(xiàn)有的Endpoints

我們可以使用@EndpointExtension (@EndpointWebExtension或者@EndpointJmxExtension)來實現(xiàn)對現(xiàn)有EndPoint的擴展:

@Component
@EndpointWebExtension(endpoint = InfoEndpoint.class)
public class InfoWebEndpointExtension {
    private InfoEndpoint delegate;
    // standard constructor
    @ReadOperation
    public WebEndpointResponse<Map> info() {
        Map<String, Object> info = this.delegate.info();
        Integer status = getStatus(info);
        return new WebEndpointResponse<>(info, status);
    }
    private Integer getStatus(Map<String, Object> info) {
        // return 5xx if this is a snapshot
        return 200;
    }
}

上面的例子擴展了InfoEndpoint。

本文所提到的例子可以參考:https://github.com/ddean2009/learn-springboot2/tree/master/springboot-actuator

到此這篇關(guān)于Spring Boot Actuator的文章就介紹到這了,更多相關(guān)Spring Boot Actuator內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java操作redis設(shè)置第二天凌晨過期的解決方案

    Java操作redis設(shè)置第二天凌晨過期的解決方案

    這篇文章主要介紹了Java操作redis設(shè)置第二天凌晨過期的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java正確比較浮點數(shù)的方法

    Java正確比較浮點數(shù)的方法

    這篇文章主要介紹了Java正確比較浮點數(shù)的方法,幫助大家更好的利用Java比較浮點數(shù)數(shù)據(jù),感興趣的朋友可以了解下
    2020-11-11
  • Java消息隊列JMS實現(xiàn)原理解析

    Java消息隊列JMS實現(xiàn)原理解析

    這篇文章主要介紹了Java消息隊列JMS實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • SpringBoot聲明式事務(wù)的簡單運用說明

    SpringBoot聲明式事務(wù)的簡單運用說明

    這篇文章主要介紹了SpringBoot聲明式事務(wù)的簡單運用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • IDEA2022創(chuàng)建Maven Web項目教程(圖文)

    IDEA2022創(chuàng)建Maven Web項目教程(圖文)

    本文主要介紹了IDEA2022創(chuàng)建Maven Web項目教程,文中通過圖文介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Java使用TCP協(xié)議發(fā)送和接收數(shù)據(jù)方式

    Java使用TCP協(xié)議發(fā)送和接收數(shù)據(jù)方式

    這篇文章詳細介紹了Java中使用TCP進行數(shù)據(jù)傳輸?shù)牟襟E,包括創(chuàng)建Socket對象、獲取輸入輸出流、讀寫數(shù)據(jù)以及釋放資源,通過兩個示例代碼TCPTest01.java和TCPTest02.java,展示了如何在客戶端和服務(wù)器端進行數(shù)據(jù)交換
    2024-12-12
  • java使用枚舉封裝錯誤碼及錯誤信息詳解

    java使用枚舉封裝錯誤碼及錯誤信息詳解

    這篇文章主要介紹了java使用枚舉封裝錯誤碼及錯誤信息,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java詳細分析Lambda表達式與Stream流的使用方法

    Java詳細分析Lambda表達式與Stream流的使用方法

    Lambda表達式,基于Lambda所帶來的函數(shù)式編程,又引入了一個全新的Stream概念,用于解決集合類庫既有的弊端,Lambda 允許把函數(shù)作為一個方法的參數(shù)(函數(shù)作為參數(shù)傳遞進方法中)。使用 Lambda 表達式可以使代碼變的更加簡潔緊湊
    2022-04-04
  • java 多線程的幾種實現(xiàn)方法總結(jié)

    java 多線程的幾種實現(xiàn)方法總結(jié)

    這篇文章主要介紹了java 多線程的幾種實現(xiàn)方法總結(jié)的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握java多線程的知識,需要的朋友可以參考下
    2017-10-10
  • MongoDB中ObjectId的誤區(qū)及引起的一系列問題

    MongoDB中ObjectId的誤區(qū)及引起的一系列問題

    這篇文章主要介紹了MongoDB中ObjectId的誤區(qū)及引起的一系列問題,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-12-12

最新評論