基本流程: Spring boot 暴露統(tǒng)計(jì)端點(diǎn)缸榄,Prometheus采集處理,Grafana提供展示界面阶冈。
1. 添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.0.26</version>
</dependency>
2. 配置application.yml
security:
basic:
# 啟用基礎(chǔ)認(rèn)證
enabled: false
# 安全路徑列表傻谁,逗號(hào)分隔,此處只針對(duì)/admin路徑進(jìn)行認(rèn)證
path: /admin
user:
# 認(rèn)證使用的用戶名
name: admin
# 認(rèn)證使用的密碼进倍。 默認(rèn)情況下,啟動(dòng)時(shí)會(huì)記錄隨機(jī)密碼购对。
password: 123456
management:
# actuator暴露接口使用的端口猾昆,為了和api接口使用的端口進(jìn)行分離
port: 7778
# actuator暴露接口的前綴
context-path: /admin
security:
# actuator是否需要安全保證
enabled: true
# 可以訪問管理端點(diǎn)的用戶角色列表,逗號(hào)分隔
roles: SUPERUSER
endpoints:
metrics:
# actuator的metrics接口是否開啟
enabled: true
# actuator的metrics接口是否需要安全保證
sensitive: false
health:
# actuator的health接口是否開啟
enabled: true
# actuator的health接口是否需要安全保證
sensitive: false
#啟用shutdown
shutdown:
enabled: true
3. 通過注釋實(shí)現(xiàn)自定義Prometheus統(tǒng)計(jì)指標(biāo)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PrometheusMetrics {
/**
* 默認(rèn)為空,程序使用method signature作為Metric name
* 如果name有設(shè)置值,使用name作為Metric name
*/
String name() default "";
}
@Aspect
@Component
@Slf4j
public class PrometheusMetricsAspect {
private static final Counter requestTotal = Counter.build().name("couter_all").labelNames("api").help
("total request couter of api").register();
private static final Counter requestError = Counter.build().name("couter_error").labelNames("api").help
("response Error couter of api").register();
private static final Histogram histogram = Histogram.build().name("histogram_consuming").labelNames("api").help
("response consuming of api").register();
/**
* 自定義Prometheus注解的全路徑
*/
@Pointcut("@annotation(com.test.PrometheusMetrics)")
public void pcMethod() {
}
@Around(value="pcMethod() && @annotation(annotation)")
public Object metricsCollector(ProceedingJoinPoint joinPoint, PrometheusMetrics annotation) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
PrometheusMetrics prometheusMetrics = methodSignature.getMethod()
.getAnnotation(PrometheusMetrics.class);
if (null == prometheusMetrics) {
return joinPoint.proceed();
}
String name;
if (StringUtils.isNotEmpty(prometheusMetrics.name())) {
name = prometheusMetrics.name();
} else {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes())
.getRequest();
name = request.getRequestURI();
}
requestTotal.labels(name).inc();
Histogram.Timer requestTimer = histogram.labels(name).startTimer();
Object object;
try {
object = joinPoint.proceed();
} catch (Exception e) {
requestError.labels(name).inc();
throw e;
} finally {
requestTimer.observeDuration();
}
return object;
}
}
使用方法:
訪問/sign后骡苞,會(huì)自動(dòng)生成counter_status_200_sign垂蜗,counter_all等指標(biāo)
4. docker部署Prometheus
- 首先獲取Prometheusdocker鏡像
docker pull prom/prometheus
- 然后編寫配置文件 prometheus.yml
global:
scrape_interval: 10s
scrape_timeout: 10s
evaluation_interval: 10m
scrape_configs:
- job_name: spring-boot
scrape_interval: 5s
scrape_timeout: 5s
metrics_path: /admin/prometheus
scheme: http
basic_auth:
# 對(duì)應(yīng)application.yml security配置
username: admin
password: 123456
static_configs:
- targets:
- 127.0.0.1:7778
- 啟動(dòng)容器
docker run -d \
--name prometheus \
-p 10000:9090 \
-m 500M \
-v "$(pwd)/prometheus.yml":/etc/prometheus/prometheus.yml \
-v "$(pwd)/data":/data \
prom/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--log.level=info
- prometheus: error: unknown short flag '-c' 問題
解決辦法就是把-config.file和-log.level前加一個(gè)‘-’,--config和--log解幽。
參見:https://github.com/prometheus/prometheus/issues/2878
- 訪問http://yourIP:10000 即可見prometheus管理界面
5. docker安裝Grafana
- 首先獲取Grafana鏡像
https://hub.docker.com/r/grafana/grafana/
docker pull grafana/grafana
- 啟動(dòng)容器
docker run -d --name=grafana -p 3000:3000 grafana/grafana
-
配置prometheus數(shù)據(jù)源
-
添加統(tǒng)計(jì)panel
擴(kuò)展:
Prometheus指標(biāo)類型:http://licyhust.com/%E5%AE%B9%E5%99%A8%E6%8A%80%E6%9C%AF/2017/07/10/prometheus-metrics/
參考:
https://blog.csdn.net/fly910905/article/details/78618969