Observability
配置Prometheus
- 在Spring Boot中(pom.xml)引入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.1.3</version> </dependency> </dependencies>
|
- 在application.properties中进行配置
1 2 3 4 5
| # Prometheus configuration spring.application.name=springboot_prometheus management.endpoints.web.exposure.include=* # 暴露的端口(这里所有端口都暴露) # 该Spring Boot程序上报server的metrics中都会带有这一tag management.metrics.tags.application=${spring.application.name}
|
- 在BackendApplication(主进程)中加入代码
1 2 3 4 5
| @Bean MeterRegistryCustomizer<MeterRegistry> configurer( @Value("${spring.application.name}") String applicationName) { return (registry) -> registry.config().commonTags("application", applicationName); }
|
这代表Prometheus会抓取该接口的数据(这正是我们Spring Boot中输出的信息)
1 2 3 4 5
| - job_name: 'springboot_prometheus' scrape_interval: 5s metrics_path: '/actuator/prometheus' static_configs: - targets: ['127.0.0.1:8080']
|
- 双击prometheus.exe运行,cheers! :beers:
- 登录localhost:9090,进入Prometheus UI界面
配置Grafana
- 在这里下载Windows版Grafana(Binaries版本)
- 下载之后解压到你要存放的文件夹中
- 运行bin目录下的grafana-server.exe
- 在conf文件夹下复制sample.ini文件,重命名为custom.ini,将http_port行取消注释(ini文件中 ‘;’ 即为注释,我们将 ‘;’ 删掉),将端口号改为你想要的端口号
- 进入localhost:${http_port}, 初始账户密码均为admin,登陆后按照指引修改密码
- 配置数据源,我们选择Prometheus,将URL补全为localhost:9090,DashBoard中import
Prometheus 2.0 Stats
和 Grafana metrics
,点击Save & test
,即可配置成功(前提是已经打开了Prometheus)
- 之后我们在DashBoard中即可看到图表