Maven依赖(根据个人情况使用具体版本,我这儿因为框架关系,有默认版本,可不写)
<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-actuator</artifactId>
</dependency>
配置文件(yml为例)
management:
endpoints:
web:
exposure:
include: '*' # info, health, beans, env, metrics, mappings, scheduledtasks, threaddump,prometheus
base-path: /actuator
metrics:
tags:
application: ${artifactId} # 公共标签
全局标签
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
return registry -> registry.config().commonTags("application", applicationName);
}
自定义指标代码示例(DemoTable是个实体,DemoTableService是个借口,DemoTableServiceImpl是个接口实现类,缺的东西要么自行创建,要么根据情况忽略掉也可以)
TestMetric.java(代码中对指标的阐释-摘抄自互联网,,,忘了哪儿了。。。)
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.capitek.cloud.sub.server.entity.DemoTable;
import com.capitek.cloud.sub.server.service.DemoTableService;
import io.micrometer.core.instrument.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class TestMetric {
@Autowired
private DemoTableService demoTableService;
@Autowired
private MeterRegistry meterRegistry;
// 只增的指标
// 以下4个counter指标名称相同,标签不同,根据标签可区分彼此
public Counter boyJavaCounter;
public Counter boyCPlusCounter;
public Count