Java應用系統監控看這篇就夠了

Java應用系統監控看這篇就夠了



本文主要介紹了系統監控的發展以及日誌監控、分佈式鏈路跟蹤和指標監控的技術方案。

業務背景


隨着互聯網行業的迅速發展,用戶增長越來越快,爲了保證用戶的體驗,對產品的性能和產品的體驗越來越高,產品的迭代速度越來越快,爲了保證用戶的體驗感和了解用戶的需求,一個全過程、規範化、自動化、智能化的監控管理管理系統非常迫切。

爲了更好、更有效的保障系統上線後的穩定的運行。對於服務器的硬件資源、性能、帶寬、端口、進程、服務等都必須有一個可靠和可持續的監測機制,統計分析每天的各種數據,從而能及時反映出服務器哪裏存在性能瓶頸、安全隱患等。另外是要有危機意識,就是了解服務器有可能出現哪些嚴重的問題,出現這些問題後該如何去迅速處理。比如數據庫的數據丟失,日誌容量過大,被黑客入侵等等。

平臺的設計要求

監控平臺上實現對系統運行狀態的集中管理(主要包含服務器、網絡、數據庫、中間件、操作系統、應用軟件),具體體現在:

1、服務器資源分析,實現服務器CPU、內存、磁盤、網絡出入流量等指標監控管理;

2、操作系統分析, 實現操作系統的進程、線程、TCP等指標管理

3、數據庫、中間件、應用系統分析, 實現可用性監控管理

4、應用系統的鏈路跟蹤分析,實現業務故障的快速定位


系統監控發展歷程

  1. 日誌監控階段

    本階段實現中間件、應用系統的運行日誌的採集和存儲,提供搜索功能,通過日誌來進故障排查。

  2. 鏈路監控階段

    日誌監控階段只是一行一行日誌 ,日誌之間沒有上下文關聯,無法掌握一次請求耗時的時長,以及較長耗時在那個環節。本階段實現將實現鏈路分析以及熱點鏈路的報表,針對報表可以進行閾值監控與故障排查,一旦有告警,可以通過點擊報表來詳細定位有問題的鏈路,順便找到對應的鏈路,查看詳細的信息。

  3. 指標監控階段

    鏈路監控對自定義指標支持的比較弱,也無法實現或者展現更加多樣的查詢聚合需求。

    本階段的實現支持豐富的Metric指標,將鏈路上的一些報表數據也可以劃分到指標中,交給專業的時序數據庫來做指標的存儲和查詢,對接或者自研豐富的指標看板。針對指標進行更加豐富的告警策略與故障排查,一旦有告警,可能需要到各個系統上查看指標看板,粗略定位根因,再結合鏈路總和分析。

  4. 深度分析階段

    指標監控雖然可以在一個系統中看到所有各個層面的監控數據了,但是每次排障時仍然要花很多的時間去查看各個層面是否有問題,一旦漏看一項可能就錯過了問題所在的根因沒有整個業務的全局監控視角,都停留在各自應用的角度。用戶查詢什麼指標就展示相應的數據,並不去關心用戶所存儲數據的內容。本階段需要主動去幫用戶分析存儲的數據內容,幫助用戶結合業務的需求和數據結構,進行模型抽象,爲用戶構建應用大盤和業務大盤,提供準確的數據,並做相關業務的分析。

    應用大盤:就是爲當前應用構建上下游應用依賴的監控、當前應用所關聯的機器監控、中間件、數據庫等監控,可以時刻爲應用做體檢,來主動暴露出問題,而不是等用戶去一個個查指標而後發現問題。

    業務大盤:就是根據業務來梳理或者利用鏈路來自動生成大盤,該大盤可以快速告訴用戶是哪些業務環節出的問題。

    趨勢報表分析:主動幫用戶發現一些逐漸惡化的問題點,比如用戶發佈之後,接口耗時增加,很可能用戶沒有發現,雖然當前沒有問題,但是很有可能在明天的高峯期就會暴露問題。

    告警方式:可以統一的針對各個層面的監控數據做統一化的告警。


技術方案

日誌監控技術方案
Grafana+阿里雲SLS日誌服務

圖片來自阿里雲 https://yq.aliyun.com/articles/227006

在這裏插入圖片描述
採集端:Logtail採集 日誌服務快速入門 https://helpcdn.aliyun.com/document_detail/54604.html?spm=a2c4g.11186623.6.574.6d556cc3n7eQ2X

接收端:阿里雲SLS日誌服務

展示端:Grafana

Grafana集成SLS日誌服務,需要安裝插件,通過插件配置SLS數據源,詳情請參閱 https://yq.aliyun.com/articles/227006

支持數據源:應用程序的程序輸出 ,訪問日誌 ,中間件日誌、數據庫日誌


分佈式鏈路追蹤技術方案
阿里雲jaeger方案

在這裏插入圖片描述

開源框架skywalking

在這裏插入圖片描述

skywalking部署有2種方式:

  • 獨立部署:可以在每個應用機器上單獨部署agent代理,請參閱 https://github.com/apache/skywalking/tree/master/docs
  • 應用程序集成:另外一種是與springboot、Springcloud集成,詳情請閱讀 https://www.jianshu.com/p/e81e35dc6406

指標監控技術方案
SpringBoot Actuator+Jolokia+Telegraf+Influxdb+Grafana 方案

Actuator

actuator是spring boot提供的對應用系統的自省和監控的集成功能,可以對應用系統進行配置查看、相關功能統計等。

Jolokia

Spring Boot Actuator對外暴露應用的監控信息,Jolokia提供使用HTTP接口獲取JSON格式 的數據。

Telegraf

收集系統和服務的統計數據,並支持寫入到 InfluxDB 數據庫。

Telegraf 詳情,請參閱 https://www.influxdata.com/

創建spring-boot-monitoring 工程

配置 pom.xml

		<dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
             <version>2.0.0</version>
        </dependency>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.0.0</version>
        </dependency>

配置 application.properties

spring.application.name=spring-boot-monitoring
management.endpoints.jmx.enabled=true
management.jolokia.enabled=true
management.jolokia.config.debug=true

telegraf配置

[[inputs.jolokia]]
	context = "/actuator/jolokia/read/"

[[inputs.jolokia.servers]]
     name = "spring-boot-monitoring"
     host = "127.0.0.1"
     port = "8080"

[[inputs.jolokia.metrics]]
     name = "heap_memory_usage"
     mbean  = "java.lang:type=Memory"
     attribute = "HeapMemoryUsage"

[[inputs.jolokia.metrics]]
     name = "thread_count"
     mbean  = "java.lang:type=Threading"
     attribute = "TotalStartedThreadCount,ThreadCount,DaemonThreadCount,PeakThreadCount"

[[inputs.jolokia.metrics]]
     name = "class_count"
     mbean  = "java.lang:type=ClassLoading"
     attribute = "LoadedClassCount,UnloadedClassCount,TotalLoadedClassCount"

[[inputs.jolokia.metrics]]
    name = "metrics"
    mbean  = "org.springframework.boot:name=metricsEndpoint,type=Endpoint"
    attribute = "Data"

[[inputs.jolokia.metrics]]
    name = "tomcat_max_threads"
    mbean  = "Tomcat:name=\"http-nio-8080\",type=ThreadPool"
    attribute = "maxThreads"

[[inputs.jolokia.metrics]]
    name = "tomcat_current_threads_busy"
    mbean  = "Tomcat:name=\"http-nio-8080\",type=ThreadPool"
    attribute = "currentThreadsBusy"   
[[outputs.influxdb]]
  ## The full HTTP or UDP URL for your InfluxDB instance.
  ##
  ## Multiple urls can be specified as part of the same cluster,
  ## this means that only ONE of the urls will be written to each interval.
  # urls = ["udp://localhost:8089"] # UDP endpoint example
  urls = ["http://localhost:8086"] # required
  ## The target database for metrics (telegraf will create it if not exists).
  database = "telegraf" # required

Influxdb

InfluxDB 是一個開源分佈式時序、事件和指標數據庫。它具備如下主要特性;

  • Time Series (時間序列):你以使用與時間有關的相關函數(如最大,最小,求和等)
  • Metrics(度量):你可以實時對大量數據進行計算
  • Eevents(事件):它支持任意的事件數據

介紹與SQL語法、常用命令使用方法

https://www.influxdata.com/

Grafana

Grafana 是一個開箱即用的可視化工具,具有功能齊全的度量儀表盤和圖形編輯器,有靈活豐富的圖形化選項,可以混合多種風格,支持多個數據源特點。通過Influxdb數據源配置,定製 Dashboard。

Grafana安裝與使用

https://grafana.com/docs/grafana/latest/installation/


Springboot+Springboot admin方案

Spring Boot Admin

Spring Boot Admin 是一個管理 和監控 Spring Boot 應用程序 的一款開源軟件。Spring Boot Admin 分爲 Server 端和 Client 端,Spring Boot Admin UI 部分使用 AngularJS 將數據展示在前端。

功能如下:

  • 顯示 name/id 和版本號
  • 顯示在線狀態
  • Logging 日誌級別管理
  • JMX beans 管理
  • Threads 會話和線程管理
  • Trace 應用請求跟蹤
  • 應用運行參數信息,如:Java 系統屬性、Java 環境變量屬性、內存信息、Spring 環境屬性

官方地址:https://github.com/codecentric/spring-boot-admin/

創建SpringBootAdminServer工程

添加依賴pom.xml

<dependency>
   <groupId>de.codecentric</groupId>
   <artifactId>spring-boot-admin-server</artifactId>
   <version>2.1.1</version>
</dependency>
<dependency>
   <groupId>de.codecentric</groupId>
   <artifactId>spring-boot-admin-server-ui</artifactId>
   <version>2.1.1</version>
</dependency>

SpringBootAdminApplication 類 添加*@EnableAdminServer*註解

@SpringBootApplication
@EnableAdminServer
public class SpringBootAdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminWebApplication.class, args);
    }
}

在application.properties文件中添加如下配置

spring.application.name=admin-server
server.port=8769

創建SpringBootAdminClient工程

添加依賴pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.1.1</version>
    </dependency>
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-client</artifactId>
        <version>2.1.1</version>
    </dependency>
</dependencies>

添加配置項 application.properties

spring.application.name=admin-client
server.port=8768
spring.boot.admin.client.url=http://localhost:8769
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

啓動adminserver與adminclient工程

訪問地址 http://localhost:8769/ 就可以看到監控信息

Springboot+ SOFA-Lookout + Prometheus +Grafana方案

SOFAStack

ScalableOpenFinancialArchitecture Stack 是螞蟻金服自主研發的金融級分佈式架構,包含了構建金融級雲原生架構所需的各個組件,是在金融場景裏錘鍊出來的最佳實踐。

SOFALookout 是螞蟻金服在 SOFAStack 體系內研發開源的一款解決系統的度量和監控問題的輕量級中間件服務。本文給大家介紹下 SOFALookout 服務器端主要提供的特性以及使用方式。

SOFALookout:https://github.com/sofastack/sofa-lookout

springboot工程

pom.xml 引入jar

		<dependency>
            <groupId>com.alipay.sofa.lookout</groupId>
            <artifactId>lookout-sofa-boot-starter</artifactId>
            <version>1.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.alipay.sofa.lookout</groupId>
            <artifactId>lookout-reg-prometheus</artifactId>
            <version>1.5.2</version>
        </dependency>

配置文件指定SOFALookout 暴露端口

spring.application.name=springboot2_sofa_lookout
com.alipay.sofa.lookout.prometheus-exporter-server-port=8081

Prometheus需要配置一下剛剛SOFA-Lookout的端口,如下:

- job_name: 'springboot2_sofa_lookout'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:8081']

granafa 配置Prometheus數據源,定製 Dashboard

源碼地址:

https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_sofa_lookout


SpringBoot Actuator+Prometheus+Grafana 方案

添加依賴

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>


啓動端點

啓用 /actuator/prometheus 端點,供 Prometheus 來抓取指標

management:
    endpoints:
        web:
        exposure:
            include: health,info,env,metrics,prometheus

啓動 SpringBoot 服務 查看 /actuator/prometheus

# 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 1.395654656E9
...

配置Prometheus 通過/actuator/prometheus 端點來抓取數據

prometheus.yml

# my global config
global:
scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).

# Alert manager configuration
alerting:
alertmanagers:
- static_configs:
    - targets:
    # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"


scrape_configs:
- job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']
- job_name: 'spring'
    metrics_path: '/actuator/prometheus'
    static_configs:
    - targets: ['localhost:8077']

思考題

上述方案的優缺點,如何改進,改進後的適合的對應場景是什麼?

通過本文的學習,讓你對系統監控有個宏觀上的認識和了解,實現的方案很多,具體選擇和落實需要紮實的功夫。

後續會持續更新,提供更詳細的參考方案和github的源碼。


羣溝通討論交流,添加微信號:nuanshoutx 。

如果覺得文章有幫助,關注下作者的公衆號,贊個人氣,不勝感謝。

在這裏插入圖片描述

發佈了147 篇原創文章 · 獲贊 41 · 訪問量 72萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章