亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring Boot實現微服務的監控

發布時間:2020-06-12 14:09:57 來源:億速云 閱讀:1330 作者:元一 欄目:系統運維

創建項目

本文的主要目的是實現微服務的監控,簡單了解了上述工具的概念后,我們就來動手實踐一下。首先創建一個簡單的Spring Boot項目,其主要依賴如下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
  • Tips:這里如果想要對接其他的監控系統,只需要更改依賴的包名。例如想對接 Influx ,則將依賴改為 micrometer-registry-influx 即可。

編輯項目配置:

server:
  port: 9562
spring:
  application:
    # 指定應用名
    name: prometheus-demo
management:
  endpoints:
    web:
      exposure:
        # 將 Actuator 的 /actuator/prometheus 端點暴露出來
        include: 'prometheus'
  metrics:
    tags:
      # 為指標設置一個Tag,這里設置為應用名,Tag是Prometheus提供的一種能力,從而實現更加靈活的篩選
      application: ${spring.application.name}

完成以上步驟后,進行一個簡單的測試,看看端點是否能正常返回監控數據。啟動項目,訪問/actuator/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="prometheus-demo",} 1.577697308142E9
# HELP jvm_buffer_memory_used_bytes An estimate of the memory that the Java virtual machine is using for this buffer pool
# TYPE jvm_buffer_memory_used_bytes gauge
jvm_buffer_memory_used_bytes{application="prometheus-demo",id="mapped",} 0.0
jvm_buffer_memory_used_bytes{application="prometheus-demo",id="direct",} 16384.0
# HELP tomcat_sessions_expired_sessions_total  
# TYPE tomcat_sessions_expired_sessions_total counter
tomcat_sessions_expired_sessions_total{application="prometheus-demo",} 0.0
# HELP jvm_gc_pause_seconds Time spent in GC pause
# TYPE jvm_gc_pause_seconds summary
jvm_gc_pause_seconds_count{action="end of minor GC",application="prometheus-demo",cause="Metadata GC Threshold",} 1.0
jvm_gc_pause_seconds_sum{action="end of minor GC",application="prometheus-demo",cause="Metadata GC Threshold",} 0.006
jvm_gc_pause_seconds_count{action="end of major GC",application="prometheus-demo",cause="Metadata GC Threshold",} 1.0
jvm_gc_pause_seconds_sum{action="end of major GC",application="prometheus-demo",cause="Metadata GC Threshold",} 0.032
jvm_gc_pause_seconds_count{action="end of minor GC",application="prometheus-demo",cause="Allocation Failure",} 1.0
jvm_gc_pause_seconds_sum{action="end of minor GC",application="prometheus-demo",cause="Allocation Failure",} 0.008
# HELP jvm_gc_pause_seconds_max Time spent in GC pause
# TYPE jvm_gc_pause_seconds_max gauge
jvm_gc_pause_seconds_max{action="end of minor GC",application="prometheus-demo",cause="Metadata GC Threshold",} 0.006
jvm_gc_pause_seconds_max{action="end of major GC",application="prometheus-demo",cause="Metadata GC Threshold",} 0.032
jvm_gc_pause_seconds_max{action="end of minor GC",application="prometheus-demo",cause="Allocation Failure",} 0.008
# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{application="prometheus-demo",area="heap",id="PS Survivor Space",} 0.0
jvm_memory_used_bytes{application="prometheus-demo",area="heap",id="PS Old Gen",} 1.3801776E7
jvm_memory_used_bytes{application="prometheus-demo",area="nonheap",id="Metaspace",} 3.522832E7
jvm_memory_used_bytes{application="prometheus-demo",area="nonheap",id="Code Cache",} 6860800.0
jvm_memory_used_bytes{application="prometheus-demo",area="heap",id="PS Eden Space",} 1.9782928E7
jvm_memory_used_bytes{application="prometheus-demo",area="nonheap",id="Compressed Class Space",} 4825568.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="prometheus-demo",level="info",} 7.0
logback_events_total{application="prometheus-demo",level="trace",} 0.0
logback_events_total{application="prometheus-demo",level="warn",} 0.0
logback_events_total{application="prometheus-demo",level="debug",} 0.0
logback_events_total{application="prometheus-demo",level="error",} 0.0
# HELP process_uptime_seconds The uptime of the Java virtual machine
# TYPE process_uptime_seconds gauge
process_uptime_seconds{application="prometheus-demo",} 30.499
# HELP jvm_buffer_count_buffers An estimate of the number of buffers in the pool
# TYPE jvm_buffer_count_buffers gauge
jvm_buffer_count_buffers{application="prometheus-demo",id="mapped",} 0.0
jvm_buffer_count_buffers{application="prometheus-demo",id="direct",} 2.0
# HELP system_cpu_count The number of processors available to the Java virtual machine
# TYPE system_cpu_count gauge
system_cpu_count{application="prometheus-demo",} 6.0
# HELP jvm_threads_peak_threads The peak live thread count since the Java virtual machine started or peak was reset
# TYPE jvm_threads_peak_threads gauge
jvm_threads_peak_threads{application="prometheus-demo",} 22.0
# HELP tomcat_sessions_alive_max_seconds  
# TYPE tomcat_sessions_alive_max_seconds gauge
tomcat_sessions_alive_max_seconds{application="prometheus-demo",} 0.0
# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{application="prometheus-demo",area="heap",id="PS Survivor Space",} 1.5204352E7
jvm_memory_committed_bytes{application="prometheus-demo",area="heap",id="PS Old Gen",} 1.31596288E8
jvm_memory_committed_bytes{application="prometheus-demo",area="nonheap",id="Metaspace",} 3.7879808E7
jvm_memory_committed_bytes{application="prometheus-demo",area="nonheap",id="Code Cache",} 6881280.0
jvm_memory_committed_bytes{application="prometheus-demo",area="heap",id="PS Eden Space",} 1.76685056E8
jvm_memory_committed_bytes{application="prometheus-demo",area="nonheap",id="Compressed Class Space",} 5373952.0
# HELP jvm_buffer_total_capacity_bytes An estimate of the total capacity of the buffers in this pool
# TYPE jvm_buffer_total_capacity_bytes gauge
jvm_buffer_total_capacity_bytes{application="prometheus-demo",id="mapped",} 0.0
jvm_buffer_total_capacity_bytes{application="prometheus-demo",id="direct",} 16384.0
# HELP jvm_gc_live_data_size_bytes Size of old generation memory pool after a full GC
# TYPE jvm_gc_live_data_size_bytes gauge
jvm_gc_live_data_size_bytes{application="prometheus-demo",} 1.3801776E7
# HELP jvm_memory_max_bytes The maximum amount of memory in bytes that can be used for memory management
# TYPE jvm_memory_max_bytes gauge
jvm_memory_max_bytes{application="prometheus-demo",area="heap",id="PS Survivor Space",} 1.5204352E7
jvm_memory_max_bytes{application="prometheus-demo",area="heap",id="PS Old Gen",} 2.841116672E9
jvm_memory_max_bytes{application="prometheus-demo",area="nonheap",id="Metaspace",} -1.0
jvm_memory_max_bytes{application="prometheus-demo",area="nonheap",id="Code Cache",} 2.5165824E8
jvm_memory_max_bytes{application="prometheus-demo",area="heap",id="PS Eden Space",} 1.390411776E9
jvm_memory_max_bytes{application="prometheus-demo",area="nonheap",id="Compressed Class Space",} 1.073741824E9
# HELP jvm_threads_daemon_threads The current number of live daemon threads
# TYPE jvm_threads_daemon_threads gauge
jvm_threads_daemon_threads{application="prometheus-demo",} 18.0
# HELP jvm_threads_states_threads The current number of threads having NEW state
# TYPE jvm_threads_states_threads gauge
jvm_threads_states_threads{application="prometheus-demo",state="runnable",} 8.0
jvm_threads_states_threads{application="prometheus-demo",state="new",} 0.0
jvm_threads_states_threads{application="prometheus-demo",state="timed-waiting",} 2.0
jvm_threads_states_threads{application="prometheus-demo",state="blocked",} 0.0
jvm_threads_states_threads{application="prometheus-demo",state="waiting",} 12.0
jvm_threads_states_threads{application="prometheus-demo",state="terminated",} 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="prometheus-demo",} 8296848.0
# HELP tomcat_sessions_active_max_sessions  
# TYPE tomcat_sessions_active_max_sessions gauge
tomcat_sessions_active_max_sessions{application="prometheus-demo",} 0.0
# HELP tomcat_sessions_created_sessions_total  
# TYPE tomcat_sessions_created_sessions_total counter
tomcat_sessions_created_sessions_total{application="prometheus-demo",} 0.0
# HELP jvm_gc_memory_allocated_bytes_total Incremented for an increase in the size of the young generation memory pool after one GC to before the next
# TYPE jvm_gc_memory_allocated_bytes_total counter
jvm_gc_memory_allocated_bytes_total{application="prometheus-demo",} 1.36924824E8
# HELP process_cpu_usage The "recent cpu usage" for the Java Virtual Machine process
# TYPE process_cpu_usage gauge
process_cpu_usage{application="prometheus-demo",} 0.10024585094452443
# HELP system_cpu_usage The "recent cpu usage" for the whole system
# TYPE system_cpu_usage gauge
system_cpu_usage{application="prometheus-demo",} 0.38661791030714154
# HELP tomcat_sessions_active_current_sessions  
# TYPE tomcat_sessions_active_current_sessions gauge
tomcat_sessions_active_current_sessions{application="prometheus-demo",} 0.0
# HELP jvm_classes_loaded_classes The number of classes that are currently loaded in the Java virtual machine
# TYPE jvm_classes_loaded_classes gauge
jvm_classes_loaded_classes{application="prometheus-demo",} 7195.0
# HELP http_server_requests_seconds  
# TYPE http_server_requests_seconds summary
http_server_requests_seconds_count{application="prometheus-demo",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 1.0
http_server_requests_seconds_sum{application="prometheus-demo",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.012429856
# HELP http_server_requests_seconds_max  
# TYPE http_server_requests_seconds_max gauge
http_server_requests_seconds_max{application="prometheus-demo",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.012429856
# HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool
# TYPE jvm_gc_max_data_size_bytes gauge
jvm_gc_max_data_size_bytes{application="prometheus-demo",} 2.841116672E9
# HELP jvm_threads_live_threads The current number of live threads including both daemon and non-daemon threads
# TYPE jvm_threads_live_threads gauge
jvm_threads_live_threads{application="prometheus-demo",} 22.0
# HELP jvm_classes_unloaded_classes_total The total number of classes unloaded since the Java virtual machine has started execution
# TYPE jvm_classes_unloaded_classes_total counter
jvm_classes_unloaded_classes_total{application="prometheus-demo",} 1.0
# HELP tomcat_sessions_rejected_sessions_total  
# TYPE tomcat_sessions_rejected_sessions_total counter
tomcat_sessions_rejected_sessions_total{application="prometheus-demo",} 0.0

該端點返回的數據是Prometheus需要使用的。每一項都有相應的注釋解釋其含義,相信不難看懂。例如:

# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{application="prometheus-demo",area="heap",id="PS Survivor Space",} 0.0

表示:prometheus-demo 應用堆內存中的 PS Survivor Space 區域占用的空間是 0.0 個字節。

Actuator:

對Spring Boot監控能力有過了解的小伙伴都應該知道Spring Boot Actuator這個子項目,它為應用提供了強大的監控能力。從Spring Boot 2.x開始,Actuator將底層改為Micrometer,提供了更強、更靈活的監控能力。Micrometer是一個監控門面,可以類比成監控界的 Slf4j 。借助Micrometer,應用能夠對接各種監控系統,例如本文所要介紹的:Prometheus

Prometheus :

Prometheus是一個由SoundCloud開發的開源系統監控+告警+時序列數據庫(TSDB),Prometheus大部分組件使用Go語言編寫,是Google BorgMon監控系統的開源版本。目前在CNCF基金會托管,并已成功孵化。在開源社區Prometheus目前也是相當活躍,在性能上Prometheus也足夠支撐上萬臺規模的集群。

Prometheus的功能:

  • 用度量名和鍵值對識別時間序列數據的多維數據模型
  • 擁有靈活的查詢語言:PromQL
  • 不依賴分布式存儲,單個服務器節點是自治的
  • 通過基于HTTP的pull方式采集時序數據
  • 可以通過中間網關進行時序列數據的推送
  • 支持通過服務發現或者靜態配置來發現目標服務對象
  • 支持多種多樣的圖表和界面展示,比如Grafana等

更多內容參考:官方文檔,GitHub倉庫

Grafana:

Grafana 是一款采用 GO 語言編寫的開源應用,支持跨平臺度量分析和可視化 + 告警。可以通過將采集的數據查詢然后可視化地展示,并及時通知。Grafana  支持多種數據源和展示方式,總而言之是一款強大酷炫的監控指標可視化工具。

更多內容參考:官方文檔,GitHub倉庫


安裝Prometheus服務

接下來就是需要在服務器上安裝Prometheus服務,用于從微服務暴露的監控端點中采集監控數據。為了簡單起見,我這里采用docker的安裝方式,其他安裝方式可以參考 官方安裝文檔。

首先為Prometheus準備一個配置文件:

[root@localhost ~]# mkdir /etc/prometheus
[root@localhost ~]# vim /etc/prometheus/prometheus.yml
scrape_configs:
# 任意寫,建議英文,不要包含特殊字符
- job_name: 'spring'
  # 多久采集一次數據
  scrape_interval: 15s
  # 采集時的超時時間
  scrape_timeout: 10s
  # 采集的端點
  metrics_path: '/actuator/prometheus'
  # 被采集的服務地址,即微服務的ip及端口
  static_configs:
  - targets: ['192.168.1.252:9562']

該配置文件的目的是讓Prometheus服務自動每隔15秒請求 http://192.168.1.252:9562/actuator/prometheus 。更多配置項參考:Prometheus Configuration官方文檔

最后通過docker啟動Prometheus服務,命令如下:

[root@localhost ~]# docker run -d -p 9090:9090 -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml

啟動成功后,正常情況下訪問http://{ip}:9090,就可以看到Prometheus的首頁:
Spring Boot實現微服務的監控

點擊 Insert metric at cursor ,即可選擇監控指標;點擊 Graph ,即可讓指標以圖表方式展示;點擊Execute 按鈕,即可看到類似下圖的結果:
Spring Boot實現微服務的監控

功能說明:

  • Insert metric at cursor:選擇展示的指標
  • Graph:讓指標以圖形展示
  • Execute:繪制指標圖表信息
  • Add Graph:繪制更多指標圖表

Grafana可視化

上一小節我們已經成功搭建了Prometheus服務,并簡單介紹了Prometheus自帶的監控數據可視化界面,然而使用體驗并不好,功能也比較少。下面我們來集成Grafana實現更友好、更貼近生產的監控數據可視化平臺。

同樣需要在服務器上安裝Grafana服務,為了簡單起見,我這里依舊采用docker的安裝方式。其他安裝方式可以參考 官方安裝文檔。

使用docker只需要一行命令就可以啟動Grafana,如下:

[root@localhost ~]# docker run -d -p 3000:3000 grafana/grafana
配置監控數據源

Grafana啟動成功后,訪問http://{ip}:3000/login進行登錄,默認賬戶密碼均為admin
Spring Boot實現微服務的監控

登錄成功后,首頁如下:
Spring Boot實現微服務的監控

首先需要添加監控數據的來源,點擊首頁中的Add data source ,即可看到類似如下的界面:
Spring Boot實現微服務的監控

這里點擊Prometheus,即可看到類似如下界面,在這里配置Prometheus服務相關的信息:
Spring Boot實現微服務的監控

保存成功后會有如下提示:
Spring Boot實現微服務的監控


創建監控Dashboard

點擊導航欄上的 + 按鈕,并點擊Dashboard,將會看到類似如下的界面:
Spring Boot實現微服務的監控

點擊 Add Query ,即可看到類似如下的界面:
Spring Boot實現微服務的監控

在紅框標記的位置添加指標查詢,指標的取值詳見Spring Boot應用的 /actuator/prometheus 端點,例如jvm_memory_used_bytesjvm_threads_states_threadsjvm_threads_live_threads 等。

Grafana會給你較好的提示,并且支持較為復雜的計算,例如聚合、求和、平均等。如果想要繪制多個線條,可點擊Add Query 按鈕。如上圖所示,筆者為圖表繪制了兩條線,分別代表daemon以及peak線程。

點擊下圖的按鈕,并填入Title,即可設置圖表標題:
Spring Boot實現微服務的監控

若需要為Dashboard添加新的圖表則點擊上圖中的左上角按鈕:
Spring Boot實現微服務的監控

并按下圖步驟操作即可:
Spring Boot實現微服務的監控

如果需要保存該Dashboard,則點擊右上角的保存按鈕即可:
Spring Boot實現微服務的監控
Spring Boot實現微服務的監控


Dashboard市場

至此,我們已經成功將Grafana與Prometheus集成,實現了較為豐富的圖表展示——將關心的監控指標放置到Dashboard上,并且非常靈活!然而,這個配置的操作雖然不難,但還是挺費時間的。

那么是否有配置好的又強大、又通用、拿來即用的Dashboard呢?答案是肯定的!前往 Grafana Lab - Dashboards ,輸入關鍵詞即可搜索指定Dashboard:
Spring Boot實現微服務的監控

如上圖所示,可以找到若干款以 Prometheus 作為數據源,支持Micrometer的Dashboard。下面,簡單演示一下如何使用 JVM(Micrometer) 這個Dashboard。點擊 JVM(Micrometer) 進入Dashboard詳情介紹頁,如下圖所示:
Spring Boot實現微服務的監控

如圖已詳細描述了該Dashboard的特性、配置。其中的management.metrics.tags.application ,前面安裝Prometheus服務時已經配置過了。該頁的右上角用紅框標注的 4701 是一個非常重要的數字,因為這是該Dashboard的id。

回到Grafana的首頁,我們來導入這個Dashboard,按下圖步驟操作:
Spring Boot實現微服務的監控

輸入后即可看到類似如下的界面,選擇數據源,并點擊Import:
Spring Boot實現微服務的監控

此時,即可看到類似如下的界面,我們常關心的指標該Dashboard均已支持:
Spring Boot實現微服務的監控

在上方的選項欄中可以選擇查看不同的服務/應用:
Spring Boot實現微服務的監控

此外,還有一些比較好用的Dashboard。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

冷水江市| 祁连县| 达州市| 无棣县| 遵义市| 定兴县| 磐安县| 顺义区| 宝鸡市| 虎林市| 西安市| 微山县| 麻江县| 陈巴尔虎旗| 玉门市| 稷山县| 阳山县| 东城区| 光山县| 舟曲县| 花莲县| 通化市| 湘潭县| 富民县| 平凉市| 三河市| 嘉荫县| 敦化市| 衡阳县| 新乡县| 通许县| 怀宁县| 寻甸| 广汉市| 横峰县| 伊金霍洛旗| 西华县| 绩溪县| 思茅市| 宜昌市| 南皮县|