Spring Boot 2.X如何实现与Prometheus监控的深度整合?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1084个文字,预计阅读时间需要5分钟。
Spring Boot 2.x 暴露健康状态,通过 Prometheus 监控,添加依赖:
xml org.springframework.boot spring-boot-starter-actuator
springboot2.x暴露健康状况通过prometheus监控
加入依赖
<!--prometheus监控 prometheus.io/docs/introduction/overview/--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> <!--prometheus监控 prometheus.io/docs/introduction/overview/-->
application.yml加入相关配置
management: security: enabled: false #prometheus+grafana+springboot2监控集成配置 metrics: export: prometheus: enabled: true jmx: enabled: true endpoints: web: exposure: include: '*' base-path: /metrics #prometheus+grafana+springboot2监控集成配置
主启动类加入配置
package com.drore.saas; import com.drore.saas.services.service.StorageService; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; @SpringBootApplication @ServletComponentScan public class TenantappApplication { public static void main(String[] args) { SpringApplication.run(TenantappApplication.class, args); } @Bean CommandLineRunner init(StorageService storageService) { return (args) -> { storageService.init(); }; } #prometheus+grafana+springboot2监控集成配置 @Bean MeterRegistryCustomizer meterRegistryCustomizer(MeterRegistry meterRegistry) { return meterRegistry1 -> { meterRegistry.config() .commonTags("application", "Tenantapp"); }; } #prometheus+grafana+springboot2监控集成配置 }
启动之后通过路径访问查看健康状况
xxxxxx/metrics/prometheus
查看到的数据如下:
[root@saas98 /]$ curl "10.98.94.220:80/ts/metrics/prometheus" # HELP process_start_time_seconds Start time of the process since unix epoch. # TYPE process_start_time_seconds gauge process_start_time_seconds{application="Tenantapp",} 1.556068841226E9 # HELP tomcat_threads_busy_threads # TYPE tomcat_threads_busy_threads gauge tomcat_threads_busy_threads{application="Tenantapp",name="github.com/coreos/prometheus-operator.git
已经移植到
github.com/coreos/kube-prometheus.git
具体原因参照官网。
下面也有自己总结的文档的地址,大家可以参照下:
github.com/hkj123/kube-prometheus-manifests.git
具体的文件如下图展示,这里有很多文件,我们只介绍关注的几个文件,其他的可以参照官网了解:
构建springboot项目的时候需要注意的点:
--- kind: Service apiVersion: v1 metadata: labels: app: ms #prometheus配置监控需要通过app去监控,这个k8s-app和app都可以 namespace: drore-saas name: ms spec: ports: - name: github.com/coreos/kube-prometheus/manifests
提供了一套整合的例子:
需要修改的文件: prometheus-clusterRole.yaml
默认的文件内容:
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: prometheus-k8s rules: - apiGroups: - "" resources: - nodes/metrics verbs: - get - nonResourceURLs: - /metrics verbs: - get
可以发现这里的监控的resources:- nodes/metrics
我们需要监控自己构建的java应用需要扩大权限
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: prometheus-k8s rules: - apiGroups: - "" resources: - nodes - services - endpoints - pods - nodes/proxy verbs: - get - list - watch - apiGroups: - "" resources: - configmaps - nodes/metrics verbs: - get - nonResourceURLs: - /metrics verbs: - get
书写monitor监控文件,提供了一下参考的文件:prometheus-serviceMonitor.yaml
自己编写的 :prometheus-serviceMonitorSelfServiceMs.yaml
apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: drore-saas-ms namespace: monitoring spec: endpoints: - interval: 15s port: 118.31.17.205:31144/targets
报警规则可以通过 prometheus-rules.yaml 去配置
参考
github.com/coreos/prometheus-operator
补充:spring boot2.x暴露监控endpoint并配置prometheus及grafana对多个targets进行监控
1. build.gradle中添加依赖
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator' compile group: 'org.springframework.boot', name: 'spring-boot-starter-security' compile('io.micrometer:micrometer-registry-prometheus')
2. spring boot 2.x暴露信息
不同于之前的Actuator 1.x,Actuator 2.x 的大多数端点默认被禁掉。 Actuator 2.x 中的默认端点增加了/actuator前缀。
3. spring boot服务端改造
配置信息
server.port=8085 management.endpoints.web.exposure.include=prometheus management.endpoint.metrics.enabled=true spring.security.user.name=xxx spring.security.user.password=123456 spring.security.user.roles=ACTUATOR_ADMIN management.server.port=8090 management.endpoints.web.base-path=/admin
指定权限
@Configuration public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity 192.168.211.2:8090/admin/prometheus
输入用户名 密码
4. Prometheus监控多个spring boot服务
下载prometheus-2.11.1.linux-amd64.tar.gz
编写配置文件prometheus.yml
global: scrape_interval: 10s scrape_timeout: 10s evaluation_interval: 10m scrape_configs: - job_name: app-gateway scrape_interval: 5s scrape_timeout: 5s metrics_path: /admin/prometheus scheme: 192.168.211.101:9090/targets
可以看到
5. grafana可视化监控多个prometheus targets
下载grafana-6.2.5.linux-amd64.tar.gz
启动grafana
./grafana-server &
192.168.211.101:3000
在dashboard中配置时,要采用这样的方式
jvm_gc_pause_seconds_count{action="end of minor GC",cause="Metadata GC Threshold",instance="192.168.211.2:8090",job="search"}
通过instance和job来进行区分prometheus的targets
可以看到dashboard中有两个prometheus的targets
以上为个人经验,希望能给大家一个参考,也希望大家多多支持易盾网络。如有错误或未考虑完全的地方,望不吝赐教。
本文共计1084个文字,预计阅读时间需要5分钟。
Spring Boot 2.x 暴露健康状态,通过 Prometheus 监控,添加依赖:
xml org.springframework.boot spring-boot-starter-actuator
springboot2.x暴露健康状况通过prometheus监控
加入依赖
<!--prometheus监控 prometheus.io/docs/introduction/overview/--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> <!--prometheus监控 prometheus.io/docs/introduction/overview/-->
application.yml加入相关配置
management: security: enabled: false #prometheus+grafana+springboot2监控集成配置 metrics: export: prometheus: enabled: true jmx: enabled: true endpoints: web: exposure: include: '*' base-path: /metrics #prometheus+grafana+springboot2监控集成配置
主启动类加入配置
package com.drore.saas; import com.drore.saas.services.service.StorageService; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; @SpringBootApplication @ServletComponentScan public class TenantappApplication { public static void main(String[] args) { SpringApplication.run(TenantappApplication.class, args); } @Bean CommandLineRunner init(StorageService storageService) { return (args) -> { storageService.init(); }; } #prometheus+grafana+springboot2监控集成配置 @Bean MeterRegistryCustomizer meterRegistryCustomizer(MeterRegistry meterRegistry) { return meterRegistry1 -> { meterRegistry.config() .commonTags("application", "Tenantapp"); }; } #prometheus+grafana+springboot2监控集成配置 }
启动之后通过路径访问查看健康状况
xxxxxx/metrics/prometheus
查看到的数据如下:
[root@saas98 /]$ curl "10.98.94.220:80/ts/metrics/prometheus" # HELP process_start_time_seconds Start time of the process since unix epoch. # TYPE process_start_time_seconds gauge process_start_time_seconds{application="Tenantapp",} 1.556068841226E9 # HELP tomcat_threads_busy_threads # TYPE tomcat_threads_busy_threads gauge tomcat_threads_busy_threads{application="Tenantapp",name="github.com/coreos/prometheus-operator.git
已经移植到
github.com/coreos/kube-prometheus.git
具体原因参照官网。
下面也有自己总结的文档的地址,大家可以参照下:
github.com/hkj123/kube-prometheus-manifests.git
具体的文件如下图展示,这里有很多文件,我们只介绍关注的几个文件,其他的可以参照官网了解:
构建springboot项目的时候需要注意的点:
--- kind: Service apiVersion: v1 metadata: labels: app: ms #prometheus配置监控需要通过app去监控,这个k8s-app和app都可以 namespace: drore-saas name: ms spec: ports: - name: github.com/coreos/kube-prometheus/manifests
提供了一套整合的例子:
需要修改的文件: prometheus-clusterRole.yaml
默认的文件内容:
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: prometheus-k8s rules: - apiGroups: - "" resources: - nodes/metrics verbs: - get - nonResourceURLs: - /metrics verbs: - get
可以发现这里的监控的resources:- nodes/metrics
我们需要监控自己构建的java应用需要扩大权限
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: prometheus-k8s rules: - apiGroups: - "" resources: - nodes - services - endpoints - pods - nodes/proxy verbs: - get - list - watch - apiGroups: - "" resources: - configmaps - nodes/metrics verbs: - get - nonResourceURLs: - /metrics verbs: - get
书写monitor监控文件,提供了一下参考的文件:prometheus-serviceMonitor.yaml
自己编写的 :prometheus-serviceMonitorSelfServiceMs.yaml
apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: drore-saas-ms namespace: monitoring spec: endpoints: - interval: 15s port: 118.31.17.205:31144/targets
报警规则可以通过 prometheus-rules.yaml 去配置
参考
github.com/coreos/prometheus-operator
补充:spring boot2.x暴露监控endpoint并配置prometheus及grafana对多个targets进行监控
1. build.gradle中添加依赖
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator' compile group: 'org.springframework.boot', name: 'spring-boot-starter-security' compile('io.micrometer:micrometer-registry-prometheus')
2. spring boot 2.x暴露信息
不同于之前的Actuator 1.x,Actuator 2.x 的大多数端点默认被禁掉。 Actuator 2.x 中的默认端点增加了/actuator前缀。
3. spring boot服务端改造
配置信息
server.port=8085 management.endpoints.web.exposure.include=prometheus management.endpoint.metrics.enabled=true spring.security.user.name=xxx spring.security.user.password=123456 spring.security.user.roles=ACTUATOR_ADMIN management.server.port=8090 management.endpoints.web.base-path=/admin
指定权限
@Configuration public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity 192.168.211.2:8090/admin/prometheus
输入用户名 密码
4. Prometheus监控多个spring boot服务
下载prometheus-2.11.1.linux-amd64.tar.gz
编写配置文件prometheus.yml
global: scrape_interval: 10s scrape_timeout: 10s evaluation_interval: 10m scrape_configs: - job_name: app-gateway scrape_interval: 5s scrape_timeout: 5s metrics_path: /admin/prometheus scheme: 192.168.211.101:9090/targets
可以看到
5. grafana可视化监控多个prometheus targets
下载grafana-6.2.5.linux-amd64.tar.gz
启动grafana
./grafana-server &
192.168.211.101:3000
在dashboard中配置时,要采用这样的方式
jvm_gc_pause_seconds_count{action="end of minor GC",cause="Metadata GC Threshold",instance="192.168.211.2:8090",job="search"}
通过instance和job来进行区分prometheus的targets
可以看到dashboard中有两个prometheus的targets
以上为个人经验,希望能给大家一个参考,也希望大家多多支持易盾网络。如有错误或未考虑完全的地方,望不吝赐教。

