最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中的Actuator詳解

 更新時(shí)間:2023年09月12日 08:45:23   作者:flydean程序那些事  
這篇文章主要介紹了SpringBoot中的Actuator詳解,Spring Boot Actuator 在Spring Boot第一個(gè)版本發(fā)布的時(shí)候就有了,它為Spring Boot提供了一系列產(chǎn)品級(jí)的特性,監(jiān)控應(yīng)用程序,收集元數(shù)據(jù),運(yùn)行情況或者數(shù)據(jù)庫(kù)狀態(tài)等,需要的朋友可以參考下

前言

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

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

開(kāi)始使用Spring Boot Actuator

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

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

開(kāi)始使用Actuator

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

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

啟動(dòng)應(yīng)用程序,訪問(wèn)//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)開(kāi)啟了兩個(gè)入口:/health和/info。

如果我們?cè)谂渲梦募锩孢@樣配置,則可以開(kāi)啟actuator所有的入口:

management.endpoints.web.exposure.include=*

重啟應(yīng)用程序,再次訪問(wèn)//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入口是用來(lái)監(jiān)控組件的狀態(tài)的,通過(guò)上面的入口,我們可以看到Health的入口如下:

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

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

我們?cè)L問(wèn)一下//localhost:8080/actuator/health:

{"status":"UP"}

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

management.endpoint.health.show-details=ALWAYS

來(lái)開(kāi)啟詳情,開(kāi)啟之后訪問(wèn)如下:

{"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,我們可以訪問(wèn)具體的某一個(gè)components如//localhost:8080/actuator/health/db:

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

就可以看到具體某一個(gè)component的信息。

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

我們看下怎么自定義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;
    }
}

再次查看//localhost:8080/actuator/health, 我們會(huì)發(fā)現(xiàn)多了一個(gè)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());
    }
}

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

"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

查看://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的目錄,訪問(wèn)://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"]}

訪問(wèn)其中具體的某一個(gè)組件如下//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是通過(guò)Micrometer來(lái)實(shí)現(xiàn)的,Spring Boot會(huì)自動(dòng)注冊(cè)MeterRegistry。 有關(guān)Micrometer和Spring Boot的結(jié)合使用我們會(huì)在后面的文章中詳細(xì)講解。

自定義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);
    }
}

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

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

@Selector后面對(duì)應(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}

注意,這里的請(qǐng)求BODY是以JSON形式提供的:

{"value":true}

請(qǐng)求URL:/actuator/features/abc 中的abc就是@Selector 中的 name。

我們?cè)倏匆幌翯ET請(qǐng)求:

//localhost:8080/actuator/features/

{"abc":"true"}

這個(gè)就是我們之前PUT進(jìn)去的值。

擴(kuò)展現(xiàn)有的Endpoints

我們可以使用@EndpointExtension (@EndpointWebExtension或者@EndpointJmxExtension)來(lái)實(shí)現(xiàn)對(duì)現(xiàn)有EndPoint的擴(kuò)展:

@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;
    }
}

上面的例子擴(kuò)展了InfoEndpoint。

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

相關(guān)文章

  • Java生成word文檔的示例詳解

    Java生成word文檔的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言生成word文檔,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以參考一下
    2022-12-12
  • openFeign服務(wù)之間調(diào)用保持請(qǐng)求頭信息處理方式

    openFeign服務(wù)之間調(diào)用保持請(qǐng)求頭信息處理方式

    這篇文章主要介紹了openFeign服務(wù)之間調(diào)用保持請(qǐng)求頭信息處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • springboot返回值轉(zhuǎn)成JSONString的處理方式

    springboot返回值轉(zhuǎn)成JSONString的處理方式

    這篇文章主要介紹了springboot返回值轉(zhuǎn)成JSONString的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java實(shí)現(xiàn)將每日新聞添加到自己博客中

    Java實(shí)現(xiàn)將每日新聞添加到自己博客中

    這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)將每日新聞添加到自己博客中并發(fā)送到微信群中,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-12-12
  • idea克隆maven項(xiàng)目的方法步驟(圖文)

    idea克隆maven項(xiàng)目的方法步驟(圖文)

    這篇文章主要介紹了idea克隆maven項(xiàng)目的方法步驟(圖文),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • SpringCloud學(xué)習(xí)筆記之SpringCloud搭建父工程的過(guò)程圖解

    SpringCloud學(xué)習(xí)筆記之SpringCloud搭建父工程的過(guò)程圖解

    SpringCloud是分布式微服務(wù)架構(gòu)的一站式解決方案,十多種微服務(wù)架構(gòu)落地技術(shù)的集合體,俗稱微服務(wù)全家桶,這篇文章主要介紹了SpringCloud學(xué)習(xí)筆記(一)搭建父工程,需要的朋友可以參考下
    2021-10-10
  • Springboot Cucumber測(cè)試配置介紹詳解

    Springboot Cucumber測(cè)試配置介紹詳解

    這篇文章主要介紹了Springboot Cucumber測(cè)試配置介紹詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Java算法真題詳解運(yùn)用單調(diào)棧

    Java算法真題詳解運(yùn)用單調(diào)棧

    一般使用單調(diào)棧無(wú)非兩個(gè)方向,單調(diào)遞減,單調(diào)遞增。單調(diào)遞增棧:存進(jìn)去的數(shù)據(jù)都是增加的,碰到減少的時(shí)候,這時(shí)就要進(jìn)行操作了。單調(diào)遞減棧:存進(jìn)去的數(shù)據(jù)都是減少的,碰到增加的時(shí)候,這時(shí)就要進(jìn)行操作了,下面我們?cè)谡骖}中運(yùn)用它
    2022-07-07
  • Mybatis中的mapper是如何和XMl關(guān)聯(lián)起來(lái)的

    Mybatis中的mapper是如何和XMl關(guān)聯(lián)起來(lái)的

    這篇文章主要介紹了Mybatis中的mapper是如何和XMl關(guān)聯(lián)起來(lái)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Java如何在沙箱環(huán)境中測(cè)試支付寶支付接口

    Java如何在沙箱環(huán)境中測(cè)試支付寶支付接口

    這篇文章主要介紹了Java如何在沙箱環(huán)境中測(cè)試支付寶支付接口,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10

最新評(píng)論

沙坪坝区| 辽宁省| 塘沽区| 唐河县| 平顺县| 静乐县| 东光县| 吉木萨尔县| 清原| 吉安县| 太湖县| 怀远县| 永吉县| 耿马| 虎林市| 泽普县| 平昌县| 娄底市| 建湖县| 陇西县| 布尔津县| 海盐县| 江达县| 南雄市| 牡丹江市| 杂多县| 荥经县| 弋阳县| 元氏县| 湘乡市| 奈曼旗| 金川县| 阳高县| 麟游县| 康保县| 商河县| 长寿区| 博湖县| 鞍山市| 邵武市| 金堂县|