springboot集成prometheus

步骤:

添加依赖,注意版本号:



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    

    com.ryxx
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    


    

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.aspectj
            aspectjweaver
            1.9.6
        


        
            org.springframework.boot
            spring-boot-starter-actuator
            
                
                    io.micrometer
                    micrometer-core
                
            
        

        
            io.micrometer
            micrometer-core
            1.1.4
        

        
            io.micrometer
            micrometer-registry-prometheus
            1.1.4
            
                
                    io.micrometer
                    micrometer-core
                
            
        


    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



注册bean:

import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Springboot2PrometheusApplication {

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

    /**
     * 注意,这个是注册的核心代码块
     */
    @Bean
    MeterRegistryCustomizer configurer(
            @Value("${spring.application.name}") String applicationName) {
        return new MeterRegistryCustomizer() {
            @Override
            public void customize(MeterRegistry registry) {
                registry.config().commonTags("application", applicationName);
            }
        };
    }

}

添加网页指标测试类:

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

@RestController
@RequestMapping("/v1")
public class IndexController {

    @Autowired
    MeterRegistry registry;

    private Counter counter_core;
    private Counter counter_index;

    @PostConstruct
    private void init() {

        /**
         * Tag(标签)是Micrometer的一个重要的功能,严格来说,一个度量框架只有实现了标签的功能,
         * 才能真正地多维度进行度量数据收集。Tag的命名一般需要是有意义的,
         * 所谓有意义就是可以根据Tag的命名可以推断出它指向的数据到底代表什么维度或者什么类型的度量指标。
         * 假设我们需要监控数据库的调用和Http请求调用统计,
         * 一般推荐的做法是:
         * MeterRegistry registry = ...
         * registry.counter("database.calls", "db", "users")
         * registry.counter("http.requests", "uri", "/api/users")
         */
        counter_core = registry.counter("app_requests_method_count",
                "method", "IndexController.core");
        counter_index = registry.counter("app_requests_method_count",
                "method", "IndexController.index");
    }

    @RequestMapping(value = "/index")
    public Object index() {
        try {
            counter_index.increment();
        } catch (Exception e) {
            return e;
        }
        return counter_index.count() + " index of springboot2-prometheus.";
    }

    @RequestMapping(value = "/core")
    public Object coreUrl() {
        try {
            counter_core.increment();
        } catch (Exception e) {
            return e;
        }
        return counter_core.count() + " coreUrl Monitor by Prometheus.";
    }


}

application配置文件:

server.port=8090
server.tomcat.uri-encoding=UTF-8
spring.application.name=springboot2-prometheus

# 开放所有endpoints(不包含shutdown)
management.endpoints.web.exposure.include=*
# 开放指定的endpoint,此配置只会在额外开放/actuator/beans和/actuator/mappings
# management.endpoints.web.exposure.include=beans,mappings
# 这个应用所有上报的 metrics 都会带上 application 这个标签
management.metrics.tags.application=${spring.application.name}
# 上面配置完毕之后,会提供一个 /actuator/prometheus 的端点,供prometheus来拉取Metrics信息

在prometheus的配置文件prometheus.yml文件中添加新的job:

  # 新添加的job   
  - job_name: 'application'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    file_sd_configs:
      - files: ['D:/Environment/prometheus/prometheus-2.42.0.windows-amd64/groups/applicationgroups/*.json']  

file_sd_configs所指定的application01.json文件内容:

[
    {
        "targets": [
            "0.0.0.0:8090"
        ],
        "labels": {
            "instance": "springboot2-prometheus",
            "service": "springboot2-prometheus-service"
        }
    }
]

启动程序:

在浏览器访问:http://127.0.0.1:8090/actuator/prometheus

结果如下:

springboot集成prometheus_第1张图片

调用接口:

http://127.0.0.1:8090/v1/index

http://127.0.0.1:8090/v1/core 

 启动prometheus后访问 http://localhost:9090/执行以下步骤,执行以下步骤:

springboot集成prometheus_第2张图片

 箭头指向就是结果,数值代表该uri的访问次数。

使用Grafana展示:

下载并启动Grafana,访问:http://localhost:3000/

添加prometheus数据源,即可完成prometheus和Grafana的交互。

根据需求绘制需要的图表。具体参考Grafana网上教程。

springboot集成prometheus_第3张图片

你可能感兴趣的:(java)