springboot2.X整合prometheus監(jiān)控的實(shí)例講解
springboot2.x暴露健康狀況通過(guò)prometheus監(jiān)控
加入依賴(lài)
<!--prometheus監(jiān)控 https://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監(jiān)控 https://prometheus.io/docs/introduction/overview/-->
application.yml加入相關(guān)配置
management: security: enabled: false #prometheus+grafana+springboot2監(jiān)控集成配置 metrics: export: prometheus: enabled: true jmx: enabled: true endpoints: web: exposure: include: '*' base-path: /metrics #prometheus+grafana+springboot2監(jiān)控集成配置
主啟動(dòng)類(lèi)加入配置
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監(jiān)控集成配置 @Bean MeterRegistryCustomizer meterRegistryCustomizer(MeterRegistry meterRegistry) { return meterRegistry1 -> { meterRegistry.config() .commonTags("application", "Tenantapp"); }; } #prometheus+grafana+springboot2監(jiān)控集成配置 }
啟動(dòng)之后通過(guò)路徑訪問(wèn)查看健康狀況
http://xxxxxx/metrics/prometheus
查看到的數(shù)據(jù)如下:
[root@saas98 /]$ curl "http://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="http-nio-9081",} 1.0 # HELP tomcat_sessions_expired_sessions_total # TYPE tomcat_sessions_expired_sessions_total counter tomcat_sessions_expired_sessions_total{application="Tenantapp",} 0.0 # HELP tomcat_sessions_active_current_sessions # TYPE tomcat_sessions_active_current_sessions gauge tomcat_sessions_active_current_sessions{application="Tenantapp",} 0.0 # HELP jvm_gc_memory_promoted_bytes_total Count of positive increases in the size of the old generation memory pool before GC to after GC # TYPE jvm_gc_memory_promoted_bytes_total counter jvm_gc_memory_promoted_bytes_total{application="Tenantapp",} 1.18894656E8 # HELP tomcat_global_request_max_seconds # TYPE tomcat_global_request_max_seconds gauge tomcat_global_request_max_seconds{application="Tenantapp",name="http-nio-9081",} 3.366 # HELP jvm_memory_used_bytes The amount of used memory # TYPE jvm_memory_used_bytes gauge jvm_memory_used_bytes{application="Tenantapp",area="heap",id="Survivor Space",} 653880.0 jvm_memory_used_bytes{application="Tenantapp",area="nonheap",id="Metaspace",} 1.36445248E8 jvm_memory_used_bytes{application="Tenantapp",area="heap",id="Eden Space",} 7511504.0 jvm_memory_used_bytes{application="Tenantapp",area="nonheap",id="Code Cache",} 3.8031424E7 jvm_memory_used_bytes{application="Tenantapp",area="heap",id="Tenured Gen",} 1.3880212E8 jvm_memory_used_bytes{application="Tenantapp",area="nonheap",id="Compressed Class Space",} 1.7220968E7 # HELP tomcat_sessions_created_sessions_total # TYPE tomcat_sessions_created_sessions_total counter tomcat_sessions_created_sessions_total{application="Tenantapp",} 0.0 # HELP system_cpu_count The number of processors available to the Java virtual machine # TYPE system_cpu_count gauge system_cpu_count{application="Tenantapp",} 1.0 # HELP tomcat_global_sent_bytes_total # TYPE tomcat_global_sent_bytes_total counter tomcat_global_sent_bytes_total{application="Tenantapp",name="http-nio-9081",} 8168269.0 # HELP jvm_threads_daemon_threads The current number of live daemon threads # TYPE jvm_threads_daemon_threads gauge jvm_threads_daemon_threads{application="Tenantapp",} 34.0 # HELP logback_events_total Number of error level events that made it to the logs # TYPE logback_events_total counter logback_events_total{application="Tenantapp",level="debug",} 0.0 logback_events_total{application="Tenantapp",level="error",} 965.0 logback_events_total{application="Tenantapp",level="warn",} 4.0 logback_events_total{application="Tenantapp",level="info",} 1047.0 logback_events_total{application="Tenantapp",level="trace",} 0.0 # HELP tomcat_cache_access_total # TYPE tomcat_cache_access_total counter tomcat_cache_access_total{application="Tenantapp",} 0.0 # HELP tomcat_servlet_error_total # TYPE tomcat_servlet_error_total counter tomcat_servlet_error_total{application="Tenantapp",name="dispatcherServlet",} 0.0 tomcat_servlet_error_total{application="Tenantapp",name="statViewServlet",} 0.0 tomcat_servlet_error_total{application="Tenantapp",name="default",} 0.0 # HELP jvm_buffer_total_capacity_bytes An estimate of the total capacity of the buffers in this pool
prometheus-operator監(jiān)控java應(yīng)用整合
目前的prometheus-operator目錄下的相關(guān)文件
https://github.com/coreos/prometheus-operator.git
已經(jīng)移植到
https://github.com/coreos/kube-prometheus.git
具體原因參照官網(wǎng)。
下面也有自己總結(jié)的文檔的地址,大家可以參照下:
https://github.com/hkj123/kube-prometheus-manifests.git
具體的文件如下圖展示,這里有很多文件,我們只介紹關(guān)注的幾個(gè)文件,其他的可以參照官網(wǎng)了解:
構(gòu)建springboot項(xiàng)目的時(shí)候需要注意的點(diǎn):
--- kind: Service apiVersion: v1 metadata: labels: app: ms #prometheus配置監(jiān)控需要通過(guò)app去監(jiān)控,這個(gè)k8s-app和app都可以 namespace: drore-saas name: ms spec: ports: - name: http-ms #prometheus配置監(jiān)控需要通過(guò)port name去識(shí)別,當(dāng)然也可以直接配置端口,不過(guò)這樣看起來(lái)更專(zhuān)業(yè) nodePort: 31945 protocol: TCP port: 80 targetPort: 80 selector: app: ms type: NodePort ---
部署prometheus需要注意的點(diǎn)
https://github.com/coreos/kube-prometheus/manifests
提供了一套整合的例子:
需要修改的文件: prometheus-clusterRole.yaml
默認(rèn)的文件內(nèi)容:
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: prometheus-k8s rules: - apiGroups: - "" resources: - nodes/metrics verbs: - get - nonResourceURLs: - /metrics verbs: - get
可以發(fā)現(xiàn)這里的監(jiān)控的resources:- nodes/metrics
我們需要監(jiān)控自己構(gòu)建的java應(yīng)用需要擴(kuò)大權(quán)限
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
書(shū)寫(xiě)monitor監(jiān)控文件,提供了一下參考的文件:prometheus-serviceMonitor.yaml
自己編寫(xiě)的 :prometheus-serviceMonitorSelfServiceMs.yaml
apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: drore-saas-ms namespace: monitoring spec: endpoints: - interval: 15s port: http-ms #prometheus配置注意這個(gè)port的名稱(chēng) path: /ms/metrics/prometheus #prometheus配置注意這個(gè)路徑 jobLabel: app #prometheus配置注意我們用的是app在springboot構(gòu)建的時(shí)候 namespaceSelector: # 表示去匹配某一命名空間中的service,如果想從所有的namespace中匹配用any: true matchNames: - drore-saas selector: matchLabels: app: ms #prometheus配置注意我們用的是app在springboot構(gòu)建的時(shí)候
配置完畢之后使用
kubectl apply -f prometheus-clusterRole.yaml -f prometheus-serviceMonitorSelfServiceMs.yaml
就可以登錄管理界面去看了
http://118.31.17.205:31144/targets
報(bào)警規(guī)則可以通過(guò) prometheus-rules.yaml 去配置
參考
https://github.com/coreos/prometheus-operator
補(bǔ)充:spring boot2.x暴露監(jiān)控endpoint并配置prometheus及grafana對(duì)多個(gè)targets進(jìn)行監(jiān)控
1. build.gradle中添加依賴(lài)
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 的大多數(shù)端點(diǎn)默認(rèn)被禁掉。 Actuator 2.x 中的默認(rèn)端點(diǎn)增加了/actuator前綴。
3. spring boot服務(wù)端改造
配置信息
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
指定權(quán)限
@Configuration public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests().antMatchers("/actuator/*").hasRole( "ACTUATOR_ADMIN").and().httpBasic(); } }
這樣我們可以看到:
這個(gè)spring boot服務(wù)啟動(dòng)了兩個(gè)端口:
8085端口,進(jìn)行正常的業(yè)務(wù)處理
8090端口,admin,暴露出監(jiān)控信息,并通過(guò)spring security進(jìn)行權(quán)限控制
打開(kāi)瀏覽器訪問(wèn)http://192.168.211.2:8090/admin/prometheus
輸入用戶(hù)名 密碼
4. Prometheus監(jiān)控多個(gè)spring boot服務(wù)
下載prometheus-2.11.1.linux-amd64.tar.gz
編寫(xiě)配置文件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: http basic_auth: username: root password: 1230456 static_configs: - targets: - 192.168.211.2:8099 - job_name: search scrape_interval: 5s scrape_timeout: 5s metrics_path: /admin/prometheus scheme: http basic_auth: username: root password: 1230456 static_configs: - targets: - 192.168.211.2:8090
其中,我的spring boot服務(wù)部署在192.168.211.2上,而prometheus和grafana都部署在192.168.211.101上
配置了兩個(gè)服務(wù)
執(zhí)行以下命令啟動(dòng) ./prometheus --config.file=prometheus.yml &
訪問(wèn)http://192.168.211.101:9090/targets
可以看到
5. grafana可視化監(jiān)控多個(gè)prometheus targets
下載grafana-6.2.5.linux-amd64.tar.gz
啟動(dòng)grafana
./grafana-server &
http://192.168.211.101:3000
在dashboard中配置時(shí),要采用這樣的方式
jvm_gc_pause_seconds_count{action="end of minor GC",cause="Metadata GC Threshold",instance="192.168.211.2:8090",job="search"}
通過(guò)instance和job來(lái)進(jìn)行區(qū)分prometheus的targets
可以看到dashboard中有兩個(gè)prometheus的targets
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- SpringBoot使用Prometheus采集自定義指標(biāo)數(shù)據(jù)的方法詳解
- Springboot搭建JVM監(jiān)控(Springboot + Prometheus + Grafana)
- SpringBoot 使用Prometheus采集自定義指標(biāo)數(shù)據(jù)的方案
- SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟
- Prometheus 入門(mén)教程之SpringBoot 實(shí)現(xiàn)自定義指標(biāo)監(jiān)控
- springboot集成普羅米修斯(Prometheus)的方法
- 使用Prometheus+Grafana的方法監(jiān)控Springboot應(yīng)用教程詳解
- springboot整合prometheus實(shí)現(xiàn)資源監(jiān)控的詳細(xì)步驟
相關(guān)文章
SpringBoot中添加監(jiān)聽(tīng)器及創(chuàng)建線程的代碼示例
這篇文章主要介紹了SpringBoot中如何添加監(jiān)聽(tīng)器及創(chuàng)建線程,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考下2023-06-06Java class文件格式之?dāng)?shù)據(jù)類(lèi)型_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java class文件格式之?dāng)?shù)據(jù)類(lèi)型的相關(guān)資料,需要的朋友可以參考下2017-06-06java中抽象類(lèi)、抽象方法、接口與實(shí)現(xiàn)接口實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于java中抽象類(lèi)、抽象方法、接口與實(shí)現(xiàn)接口的相關(guān)資料,文中通過(guò)示例代碼將四者介紹的非常詳細(xì),并且簡(jiǎn)單介紹了抽象類(lèi)和接口的區(qū)別,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11Java設(shè)計(jì)模式以虹貓藍(lán)兔的故事講解代理模式
代理模式是Java常見(jiàn)的設(shè)計(jì)模式之一。所謂代理模式是指客戶(hù)端并不直接調(diào)用實(shí)際的對(duì)象,而是通過(guò)調(diào)用代理,來(lái)間接的調(diào)用實(shí)際的對(duì)象2022-04-04Java Builder模式構(gòu)建MAP/LIST的實(shí)例講解
下面小編就為大家?guī)?lái)一篇Java Builder模式構(gòu)建MAP/LIST的實(shí)例講解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10java中創(chuàng)建寫(xiě)入文件的6種方式詳解與源碼實(shí)例
這篇文章主要介紹了java中創(chuàng)建寫(xiě)入文件的6種方式詳解與源碼實(shí)例,Files.newBufferedWriter(Java 8),Files.write(Java 7 推薦),PrintWriter,File.createNewFile,FileOutputStream.write(byte[] b) 管道流,需要的朋友可以參考下2022-12-12Eclipse+Java+Swing+Mysql實(shí)現(xiàn)工資管理系統(tǒng)
這篇文章主要介紹了Eclipse+Java+Swing+Mysql實(shí)現(xiàn)工資管理系統(tǒng),對(duì)正在工作或者學(xué)習(xí)的你有一定的參考價(jià)值,需要的朋友可以參考一下2022-01-01大數(shù)據(jù) java hive udf函數(shù)的示例代碼(手機(jī)號(hào)碼脫敏)
這篇文章主要介紹了大數(shù)據(jù) java hive udf函數(shù)(手機(jī)號(hào)碼脫敏),的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Spring?JPA使用CriteriaBuilder動(dòng)態(tài)構(gòu)造查詢(xún)方式
這篇文章主要介紹了Spring?JPA使用CriteriaBuilder動(dòng)態(tài)構(gòu)造查詢(xún)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12