Prometheus

Prometheus

Prometheus是一个根据应用的metrics来进行监控的开源工具。Prometheus是一个开源的完整监控解决方案,其对传统监控系统的测试和告警模型进行了彻底的颠覆,形成了基于中央化的规则计算、统一分析和告警的新模型。

SpringBoot集成Prometheus

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo</groupId>
    <artifactId>prometheus-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_spring_boot</artifactId>
            <version>0.10.0</version>
        </dependency>

        <!-- Hotspot JVM metrics-->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_hotspot</artifactId>
            <version>0.10.0</version>
        </dependency>
        
         <!--Exposition servlet -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_servlet</artifactId>
            <version>0.10.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

配置启动类

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

最后启动服务,访问浏览器127.0.0.1:8080/metrics就可以看到一系列的metrics信息(json的数据格式)
在这里插入图片描述

新增配置类

@Configuration
public class PrometheusConfig {

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        DefaultExports.initialize();
        return new ServletRegistrationBean(new MetricsServlet(), "/metrics");
    }
}

启动服务,访问浏览器127.0.0.1:8080/metrics。
在这里插入图片描述

自定义监控项

@RestController
public class SampleController {

    private static Random random = new Random();

    private static final Counter requestTotal = Counter.build()
            .name("my_sample_counter")
            .labelNames("status")
            .help("A simple Counter to illustrate custom Counters in Spring Boot and Prometheus").register();
            
    @RequestMapping("/endpoint")
    public void endpoint() {
        if (random.nextInt(2) > 0) {
            requestTotal.labels("success: "+ random.nextInt(100)).inc();
        } else {
            requestTotal.labels("error: "+ random.nextInt(100)).inc();
        }
    }
}

配置文件

这个配置文件我就试过了几个,等我后面在试试在备注下。

# actuator暴露接口的前缀
management:
  context-path: /admin
  # actuator暴露接口使用的端口,为了和api接口使用的端口进行分离
  port: 9194
  security:
    enabled: true
    roles: SUPERUSER
spring.application.name=mydemo
server.port=8080
management.security.enabled=false
management.metrics.export.prometheus.enabled = true
management.metrics.export.prometheus.step = 1m
management.metrics.export.prometheus.descriptions = true
management.web.server.auto-time-requests = true
management.endpoints.web.exposure.include=prometheus
#开启prometheus的数据推送模式
management.metrics.export.prometheus.pushgateway.enabled=true
#prometheus服务端地址
#management.metrics.export.prometheus.pushgateway.base-url=localhost:9091
#推送数据的频率,默认1m(单位分钟)
management.metrics.export.prometheus.pushgateway.push-rate=1m
#在jvm关闭之前将数据推送出去
management.metrics.export.prometheus.pushgateway.shutdown-operation=push

management.endpoints.web.base-path=/actuator
#应用可视化监控
spring.metrics.servo.enabled=false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值