自定义的prometheus自定义springboot-starter
创建 wlc-prometheus-starter maven模块,这是我在微服务中的新建的maven模块名字
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">
<parent>
<artifactId>common-starter</artifactId>
<groupId>com.wlc.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>wlc-prometheus-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--prometheus监控 https://prometheus.io/docs/introduction/overview/-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--prometheus监控 https://prometheus.io/docs/introduction/overview/-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies>
</project>
模块的代码结构
prometheus注册配置MeterRegistryConfig
package com.prometheus.config;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* 描述: prometheus注册配置 </br>
* 时间: 2020-05-23 15:42 </br>
* 作者:王林冲
*/
@Component
@Configuration
public class MeterRegistryConfig {
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}
META-INF下的自动配置文件spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.prometheus.config.MeterRegistryConfig
这样自定义的springboot-starter就定义好了
其他需要引入的微服务直接引入坐标即可
<!--自定义普罗米修斯spring-boot-starter-->
<dependency>
<groupId>com.wlc.cloud</groupId>
<artifactId>wlc-prometheus-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
这里统一的nacos的服务转consul,并且同步到prometheus,我选择网关,因为就配置两个不动的ip在prometheus.yaml中即可
gateway中需要引入 nacos-consul-adapter适配器,但是其他的微服务中则不需要引入nacos-consul-adapter
在gateway的pom.xml中新增
<!--nacos-consul-adapter适配器,为了让nacos的注册服务都注册到prometheus-->
<dependency>
<groupId>io.github.chen-gliu</groupId>
<artifactId>nacos-consul-adapter</artifactId>
<version>0.0.3.M</version>
</dependency>
<!--自定义普罗米修斯spring-boot-starter-->
<dependency>
<groupId>com.wlc.cloud</groupId>
<artifactId>wlc-prometheus-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
在我的授权服务auth-server中pom.xml引入自定义普罗米修斯spring-boot-starter
<!--自定义普罗米修斯spring-boot-starter-->
<dependency>
<groupId>com.wlc.cloud</groupId>
<artifactId>wlc-prometheus-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
auth-server中和gateway和所有引入自定义普罗米修斯spring-boot-starter进行监控的都要在application.yml新增如下配置
management:
#prometheus+grafana+springboot2监控集成配置
metrics:
export:
prometheus:
enabled: true
jmx:
enabled: true
endpoints:
web:
exposure:
include: '*'
我这里的分布式id服务 uid-generator-server 和springboot-admin都加入了prometheus监控。配置和auth-server一样,pom中引入自定义普罗米修斯spring-boot-starter
application.yml配置下
<!--自定义普罗米修斯spring-boot-starter-->
<dependency>
<groupId>com.wlc.cloud</groupId>
<artifactId>wlc-prometheus-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
application.yml配置下
management:
#prometheus+grafana+springboot2监控集成配置
metrics:
export:
prometheus:
enabled: true
jmx:
enabled: true
endpoints:
web:
exposure:
include: '*'
然后不能测试,因为没有安装prometheus和grafana
安装linux环境的步骤如下:
安装prometheus
1,下载Prometheus
下载到本地,后面可以上传到linux服务器上解压即可。然后配置下prometheus.yml就可以了,简单明了
我在 linux服务器的/opt下新建了monitor文件夹,防止一些监控的工具,在monitor新建了grafana和prometheus两个文件夹
把刚刚下载的prometheus-2.36.0.linux-amd64.tar.gz上传到 /opt/monitor/prometheus文件见下,然后解压即可,解压后的截图
然后cd 到 prometheus-2.36.0下,修改prometheus.yml
prometheus.yml如下
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
#这是新增的监控job,是配置nacos-consul-adapter适配器的服务,也就是我的网关
- job_name: 'consul-prometheus-test'
metrics_path: '/actuator/prometheus'
consul_sd_configs:
#这里是nacos-consul-adapter适配器的服务,也就是我的网关的ip和端口号
- server: '127.0.0.1:8080'
start.sh脚本
nohup ./prometheus-2.36.0/prometheus --config.file=/opt/monitor/prometheus/prometheus-2.36.0/prometheus.yml >/opt/monitor/prometheus/prometheus.log 2>&1 &
把start.sh服务执行权限,然后 ./start.sh即可启动,默认端口是9090
然访问浏览器地址:http://ip:port就行,我的是 http://127.0.0.1:9090就可以访问prometheus,前提是你的linux服务器要暴露9090端口
这样就注册到prometheus,到此成功,点击任意个服务就会有相信信息
下面Linux下安装grafana来对prometheus的数据可视化,不然不直观
安装grafana
下载grafana
下载地址:Download Grafana | Grafana Labs,打开连接,进入grafana的web界面,找到下载命令,在linux中执行即可
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.5.3.linux-amd64.tar.gz
我是在/opt/monitor/grafana目录下执行 wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.5.3.linux-amd64.tar.gz,就会下载到/opt/monitor/grafana目录下
然后解压即可
其他什么都不需要该,直接启动就行
start.sh脚本信息如下,后台启动
cd /opt/monitor/grafana/grafana-8.5.3/bin
nohup ./grafana-server start >/opt/monitor/grafana/grafana.log 2>&1 &
给start.sh执行权限,然后 ./start.sh 启动即可
启动后
linux暴露3000端口,就可以浏览器访问了
浏览器访问 http://localhost:3000/就可以,用户名/密码 admin/admin,成功页面
滚动下拉
这里prometheus数据源配置完成,也就是grafana可以访问到刚刚安装的prometheus服务了
下面进行grafana面板的可视化配置
我这里在https://grafana.com/grafana/dashboard 找的,
下面是我的找的一个喜欢的
用notepad打开,然后全选,copy,把内容粘贴到
然后点击【load】按钮即可
进入配置的面板
点击搜索,选择不同的面板,来查看不同的监控需求
流程:
更多精彩分享请移步:IT学习道场
更多分享关注公众号【IT学习道场】