溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Spring Boot Actuator 整合 Prometheus

發(fā)布時(shí)間:2020-07-15 11:29:49 來(lái)源:網(wǎng)絡(luò) 閱讀:208 作者:程序員果果 欄目:編程語(yǔ)言

簡(jiǎn)介

Spring Boot 自帶監(jiān)控功能 Actuator,可以幫助實(shí)現(xiàn)對(duì)程序內(nèi)部運(yùn)行情況監(jiān)控,比如監(jiān)控狀況、Bean加載情況、環(huán)境變量、日志信息、線(xiàn)程信息等。這一節(jié)結(jié)合 Prometheus 、Grafana 來(lái)更加直觀的展示這些信息。

實(shí)驗(yàn)

說(shuō)明

服務(wù)名 地址 端口
Prometheus 172.16.2.101 9090
Grafana 172.16.2.101 3000
Spring Boot Demo 172.16.2.204 8080

創(chuàng)建項(xiàng)目

創(chuàng)建用于測(cè)試的 Spring Boot 項(xiàng)目,主要代碼如下。

pom.xml
<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>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
application.yml
management:
  endpoints:
    web:
      exposure:
        include: '*'

  endpoint:
    health:
      show-details: always

  metrics:
    tags:
      application: actuator-demo
  • management.endpoints.web.exposure.include:大多數(shù)actuator的端口都不會(huì)通過(guò)http公開(kāi),* 代表公開(kāi)所有這些端點(diǎn)。對(duì)于生產(chǎn)環(huán)境,應(yīng)該仔細(xì)選擇要公開(kāi)的端點(diǎn)。
  • management.metrics.tags.application:為應(yīng)用設(shè)置 tag ,方便區(qū)分不同的應(yīng)用。
啟動(dòng)類(lèi)
@SpringBootApplication
@RestController
public class SpringbootActuatorPrometheusDemoApplication {

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

    @RequestMapping(value = "/hello")
    public String  sayHello() {
        for (int i = 1 ; i <= 10 ; i++) {
            Thread t = new Thread(() -> {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } , "HelloThread - " + i);
            t.start();
        }
        return "ok";
    }

    /**
    @Bean
    MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
        return registry -> registry.config().commonTags("application", "springboot-actuator-prometheus-demo");
    }
    */

}

配置 Prometheus 和 Grafana

在 prometheus.yml 中添加針對(duì)該 Spring Boot 應(yīng)用 的監(jiān)控 job

- job_name: 'actuator-demo'
    metrics_path: '/prometheus'
    static_configs:
    - targets: ['172.16.2.204:8080']

運(yùn)行 Prometheus 和 Grafana:

docker start prometheus grafana

訪(fǎng)問(wèn) Prometheus UI http://172.16.2.101:9090 ,查看 targets ,可以看到 job 處于 UP 狀態(tài),說(shuō)明配置成功了。

Spring Boot Actuator 整合 Prometheus

Grafana UI http://172.16.2.101:3000,通過(guò)Grafana的 + 圖標(biāo)導(dǎo)入(Import) JVM (Micrometer) dashboard:

  • grafana id = 4701
  • 注意選中prometheus數(shù)據(jù)源

查看JVM (Micormeter) dashboard:

Spring Boot Actuator 整合 Prometheus

可以看到應(yīng)用的 JVM 的 堆棧、 線(xiàn)程、 IO 等等信息。

源碼

https://github.com/gf-huanchupk/SpringBootLearning/tree/master/springboot-actuator-prometheus

參考

https://micrometer.io/docs/registry/prometheus
https://prometheus.io/docs/prometheus

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI