Prometheus+grafana監控SpringBoot2應用


micrometer

我們對springboot2監控,主要用的是SpringBoot Actuator這個項目。而他的底層就是micrometer

image-20201122153751510

micrometer號稱監控界的SLF4J,主要用來以極低極低的消耗來給Java程序提供對指標的監控。

micrometer支持接入多種數據庫,這里我使用的是prometheus。

集成prometheus

我這里boot版本是2.1.8,部分maven依賴:

        <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>

        <!--micrometer橋接prometheus-->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.2.0</version>
        </dependency>

        <!--micrometer獲取jvm相關信息-->
        <dependency>
            <groupId>io.github.mweirauch</groupId>
            <artifactId>micrometer-jvm-extras</artifactId>
            <version>0.1.4</version>
        </dependency>

properties配置:

spring.application.name=boot-micrometer
management.metrics.tags.application=${spring.application.name}
management.endpoints.web.exposure.include=*

#可以遠程關閉springboot服務
#curl -X POST http://localhost:8080/actuator/shutdown
management.endpoint.shutdown.enabled=true
management.metrics.export.simple.enabled=false

info.author=wen.jie

主啟動類:

@EnableScheduling
@SpringBootApplication
public class BootMicrometerApplication {

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

}

配置類:

@Component
public class MeterConfig implements MeterBinder {

    public Counter counter;

    public Map<String,Double> map;

    public MeterConfig() {
        map = new HashMap<>();
    }

    @Override
    public void bindTo(MeterRegistry registry) {
        this.counter = Counter.builder("demo.counter").tags("name","counter").description("this is counter").register(registry);

        Gauge.builder("demo.gauge",map,x->x.get("x")).tag("name","guage").description("this is gauge").register(registry);
    }
}

定時任務:

@Component
public class ScheduledConfig {

    @Autowired
    MeterConfig meterConfig;

    int count1 = 0;

    @Async("one")
    @Scheduled(fixedDelay = 1000)
    public void increment(){
        count1++;
        meterConfig.counter.increment();
        meterConfig.map.put("x", (double) count1);
        System.out.println(count1);
    }
}

訪問:http://localhost:8080/actuator/metrics

image-20201122160710722

訪問我剛在在配置類中設置的指標:http://localhost:8080/actuator/metrics/demo.counter,http://localhost:8080/actuator/metrics/demo.gauge

image-20201122160905784

image-20201122160920301

配置prometheus.yml:(添加springboot配置)

  - job_name: 'springboot'
    metrics_path: '/actuator/prometheus'
    static_configs:
    - targets: ['localhost:8080']

配置完成后,重啟prometheus,然后在target中就能看到springboot應用了

image-20201122162135874

接入Grafana

在Grafana頁面中點擊import

image-20201122162535962

輸入4701,點擊load

image-20201122162604327

稍等片刻,就會出來JVM (Micrometer)頁面

image-20201122162658161

選擇之前生成的Prometheus的數據源

就能看到jvm的dashboard了

image-20201122163607591


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM