1.官网下载安装 prometheus和grafana
promethus
1.下载windows版本安装包
2.双击启动
3.访问地址
http://localhost:9090
grafana
1.下载windows版本安装包
2.启动 ,默认windows安装完成自动启动
3.访问地址
默认第一次登录账户 admin admin,需要更改密码
更改之后登录账户:admin admin123
默认登录首页如下:
2. 搭建springcloudalibaba集成prometheus、grafana
-
引入依赖,springboot3.2之后引入如下
xml<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus-simpleclient</artifactId> <version>1.13.6</version> </dependency> <!--暴露指标数据端点--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
springboot3.2之前引入如下
xml<!--暴露指标数据端点--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.14.5</version> </dependency>
-
在yml文件配置监控端点暴露配置
yamlmanagement: endpoints: web: exposure: include: "*" health: show-details: always #暴露所有端点信息 enabled-by-default: true endpoint: prometheus: enabled: true prometheus: metrics: export: enabled: true
-
在当前启动的应用代码中添加,在prometheus显示的时候附加当前应用名称
java@EnableFeignClients @SpringBootApplication @EnableDiscoveryClient public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } /** *在prometheus显示的时候附加当前应用名 * @param applicationName * @return */ @Bean MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) { return (registry) -> registry.config().commonTags("application", applicationName); } }
4.访问微服务暴露的prometheus端点
-
修改nacos注册中心配置文件,暴露prometheus监控点,重启nacos server
-
修改nacos注册中心配置文件,暴露prometheus监控点,重启nacos server
properties
management.endpoints.web.exposure.include=* #打开prometheus暴露点
nacos.prometheus.metrics.enabled=true #开启nacos支持prometheus
访问nacos 暴露端点:
- 修改prometheus.yml配置文件如下:
yaml
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"] #静态目标列表,指定Prometheus自己
#以下内容为springboot应用配置
- job_name: "wemedia-oss-user" #配置抓取任务的名称
scrape_interval: 5s #间隔5秒拉取微服务总数据
metrics_path: '/actuator/prometheus' #设置微服务给prometheus暴露的端点
static_configs:
- targets: ["localhost:8890"] #指定微服务的地址
labels: #自定义的额外标签
app: "wemedia-oss-user"
instance: "wemedia-oss-user_server" #添加一个自定义标签,标记目标实例
-
在prometheus上查看暴露端点
-
在grafana上导入数据源,来自prometheus server的地址
导入jvm(micrometer)可视化看板
接着出现如下界面:
点击导入Import按钮,出现可视化jvm监控界面如下:
备注说明:
上面输入的4701为jvm可视化编号
其他的可视化模版ID都可以在这个地址里找到 https://grafana.com/grafana/dashboards -
配置nacos server监控
配置prometheus.yml文件中新增nacos server配置yaml#以下内容为nacos server配置 - job_name: "nacos-server" #配置抓取任务的名称 scrape_interval: 5s #间隔5秒拉取微服务总数据 metrics_path: '/nacos/actuator/prometheus' #设置nacos server给prometheus暴露的端点 static_configs: - targets: ["localhost:8848"] #指定nacos server的地址 labels: #自定义的额外标签 app: "nacos-server" instance: "nacos-server_instance" #添加一个自定义标签,标记目标实例
输入nacos 可视化ID:1322
查看nacos 指标监控
导入SpringBoot APM Dashboard(中文版本)可视化面板
导入Monitor Statistics dashboard:springboot 监控统计模版