编写。
- 本文仅仅是一个示例项目,后续加入springcloud等环境,将对其进行二次开发。
- 项目具体实施:
- 新建一个springboot项目,名称为prometheus-service
- 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!--自动重启 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
- 配置application.yml
server:
port: 9090
spring:
application:
name: prometheus-service
management:
endpoint:
metrics:
enabled: true
prometheus:
enabled: true
endpoints:
web:
exposure:
include: '*'
metrics:
export:
prometheus:
enabled: true
tags:
application: ${spring.application.name}
- 新建一个包com.yuji.job,新建JobMetrics类,完整代码如下:
package com.yuji.job;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.MeterBinder;
@Component
public class JobMetrics implements MeterBinder {
public Counter job1Counter;
public Counter job2Counter;
public Map<String, Double> map;
JobMetrics() {
map = new HashMap<>();
}
@Override
public void bindTo(MeterRegistry meterRegistry) {
this.job1Counter = Counter.builder("counter_builder_job_counter1")
.tags(new String[]{"name", "tag_job_counter1"})
.description("description-Job counter1 execute count").register(meterRegistry);
this.job2Counter = Counter.builder("counter_builder_job_counter2")
.tags(new String[]{"name", "tag_job_counter2"})
.description("description-Job counter2 execute count ").register(meterRegistry);
Gauge.builder("gauge_builder_job_gauge", map, x -> x.get("x"))
.tags("name", "tag_job_gauge")
.description("description-Job gauge")
.register(meterRegistry);
}
}
- 在该包里模拟job任务,新建MyJob类,完整内容如下:
package com.yuji.job;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
public class MyJob {
private Integer count1 = 0;
private Integer count2 = 0;
@Autowired
private JobMetrics jobMetrics;
@Async("main")
@Scheduled(fixedDelay = 1000)
public void doSomething() {
count1++;
jobMetrics.job1Counter.increment();
jobMetrics.map.put("x", Double.valueOf(count1));
System.out.println("task1 count:" + count1);
if(count1%2==0){
System.out.println("%5==0");
jobMetrics.map.put("x", Double.valueOf(1));
}
}
@Async
@Scheduled(fixedDelay = 10000)
public void doSomethingOther() {
count2++;
jobMetrics.job2Counter.increment();
System.out.println("task2 count:" + count2);
}
}
- 新建一个controller包,新建CounterController类,代码如下:
package com.yuji.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.yuji.job.JobMetrics;
@RestController
public class CounterController {
@Autowired
private JobMetrics jobMetrics;
@RequestMapping(value = "/counter1", method = RequestMethod.GET)
public void counter1() {
jobMetrics.job2Counter.increment();
}
@RequestMapping(value = "/counter2", method = RequestMethod.GET)
public void counter2() {
jobMetrics.job2Counter.increment();
}
@RequestMapping(value = "/gauge", method = RequestMethod.GET)
public void gauge(@RequestParam(value = "x") String x) {
System.out.println("gauge controller x" + x);
jobMetrics.map.put("x", Double.valueOf(x));
}
}
- 启动项目,在浏览器输入:http://localhost:9090/actuator/prometheus,如果执行成功,则springboot与prometheus集成成功。
- 将其放置于prometheus监控环境下实现grafana图表显示。
- prometheus环境搭建参考:
https://blog.csdn.net/qq_25337221/article/details/106206042
- 在prometheus配置该应用
# vim /etc/prometheus/prometheus.yml
添加如下代码:
- job_name: 'springboot'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['ip:9090']
执行:
# ps -aux | grep prome
#kill -HUP 进程号
然后登陆前端prometheus查看prometheus界面,springboot是否添加成功。
- 使用grafana模板显示springboot
- 下载https://grafana.com/grafana/dashboards/6756模板,
- 在grafana中添加数据源(之前的文档已经添加,未添加可以参考下面的参考文档或者本文摘要中提到的上一个文档)。
- 导入模板
- 根据自己需要修改变量名,参照模板里的教程。
至此,springboot与prometheus入门示例完毕。
本文文本材料:https://download.csdn.net/download/qq_25337221/12445404
- 参考链接:
- Prometheus+Grafana可视化监控SpringBoot项目 https://www.jianshu.com/p/762fa72ffe79
2. 为springboot中自定义prometheus:https://www.cnblogs.com/xidianzxm/p/11542135.html
3. prometheus在java里的基础知识:https://www.cnblogs.com/xiezhengcai/p/prometheus.html
本内容由安康学院“雨季”原创。