micrometer
我們對springboot2監控,主要用的是SpringBoot Actuator這個項目。而他的底層就是micrometer
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
訪問我剛在在配置類中設置的指標:http://localhost:8080/actuator/metrics/demo.counter,http://localhost:8080/actuator/metrics/demo.gauge

配置prometheus.yml:(添加springboot配置)
- job_name: 'springboot'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8080']
配置完成后,重啟prometheus,然后在target中就能看到springboot應用了
接入Grafana
在Grafana頁面中點擊import
輸入4701,點擊load
稍等片刻,就會出來JVM (Micrometer)頁面
選擇之前生成的Prometheus的數據源
就能看到jvm的dashboard了