javascript
springboot 接口404_资深架构带你学习Springboot集成普罗米修斯
這篇文章主要介紹了springboot集成普羅米修斯(Prometheus)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧!
Prometheus 是一套開(kāi)源的系統(tǒng)監(jiān)控報(bào)警框架。它由工作在 SoundCloud 的 員工創(chuàng)建,并在 2015 年正式發(fā)布的開(kāi)源項(xiàng)目。2016 年,Prometheus 正式加入 Cloud Native Computing Foundation,非常的受歡迎。
簡(jiǎn)介
Prometheus 具有以下特點(diǎn):
- 一個(gè)多維數(shù)據(jù)模型,其中包含通過(guò)度量標(biāo)準(zhǔn)名稱和鍵/值對(duì)標(biāo)識(shí)的時(shí)間序列數(shù)據(jù)
- PromQL,一種靈活的查詢語(yǔ)言,可利用此維度
- 不依賴分布式存儲(chǔ); 單服務(wù)器節(jié)點(diǎn)是自治的
- 時(shí)間序列收集通過(guò)HTTP上的拉模型進(jìn)行
- 通過(guò)中間網(wǎng)關(guān)支持推送時(shí)間序列
- 通過(guò)服務(wù)發(fā)現(xiàn)或靜態(tài)配置發(fā)現(xiàn)目標(biāo)
- 多種圖形和儀表板支持模式
Prometheus 組成及架構(gòu)
Prometheus 生態(tài)圈中包含了多個(gè)組件,其中許多組件是可選的:
- Prometheus Server: 用于收集和存儲(chǔ)時(shí)間序列數(shù)據(jù)。
- Client Library: 客戶端庫(kù),為需要監(jiān)控的服務(wù)生成相應(yīng)的 metrics 并暴露給 Prometheus server。當(dāng) Prometheus server 來(lái) pull 時(shí),直接返回實(shí)時(shí)狀態(tài)的 metrics。
- Push Gateway: 主要用于短期的 jobs。由于這類 jobs 存在時(shí)間較短,可能在 Prometheus 來(lái) pull 之前就消失了。為此,這次 jobs 可以直接向 Prometheus server 端推送它們的 metrics。這種方式主要用于服務(wù)層面的 metrics,對(duì)于機(jī)器層面的 metrices,需要使用 node exporter。
- Exporters: 用于暴露已有的第三方服務(wù)的 metrics 給 Prometheus。
- Alertmanager: 從 Prometheus server 端接收到 alerts 后,會(huì)進(jìn)行去除重復(fù)數(shù)據(jù),分組,并路由到對(duì)收的接受方式,發(fā)出報(bào)警。常見(jiàn)的接收方式有:電子郵件,pagerduty,OpsGenie, webhook 等一些其他的工具。
其大概的工作流程是:
1.Prometheus server 定期從配置好的 jobs 或者 exporters 中拉 metrics,或者接收來(lái)自 Pushgateway 發(fā)過(guò)來(lái)的 metrics,或者從其他的 Prometheus server 中拉 metrics。
2.Prometheus server 在本地存儲(chǔ)收集到的 metrics,并運(yùn)行已定義好的 alert.rules,記錄新的時(shí)間序列或者向 Alertmanager 推送警報(bào)。
3.Alertmanager 根據(jù)配置文件,對(duì)接收到的警報(bào)進(jìn)行處理,發(fā)出告警。
4.在圖形界面中,可視化采集數(shù)據(jù)。
springboot 集成prometheus
在spring boot工程中引入actuator的起步依賴,以及micrometer-registry-prometheus的依賴。
org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-actuatorio.micrometer micrometer-registry-prometheus暴露prometheus的接口;暴露metrics.tags,和spring.application.name一致。
server: port: 8081spring: application: name: my-prometheusmanagement: endpoints: web: exposure: include: 'prometheus' metrics: tags: application: ${spring.application.name}寫(xiě)一個(gè)API接口,用作測(cè)試。代碼如下:
@RestControllerpublic class TestController { Logger logger = LoggerFactory.getLogger(TestController.class); @GetMapping("/test") public String test() { logger.info("test"); return "ok"; } @GetMapping("") public String home() { logger.info("home"); return "ok"; }}在瀏覽器上訪問(wèn)http://localhost:8081/actuator/prometheus,展示的信息如下,這些信息都是actuator的一些監(jiān)控信息。
# HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool# TYPE jvm_gc_max_data_size_bytes gaugejvm_gc_max_data_size_bytes{application="my-prometheus",} 2.863661056E9# HELP http_server_requests_seconds # TYPE http_server_requests_seconds summaryhttp_server_requests_seconds_count{application="my-prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 1.0http_server_requests_seconds_sum{application="my-prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.018082327# HELP http_server_requests_seconds_max # TYPE http_server_requests_seconds_max gaugehttp_server_requests_seconds_max{application="my-prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.018082327# HELP jvm_threads_states_threads The current number of threads having NEW state# TYPE jvm_threads_states_threads gaugejvm_threads_states_threads{application="my-prometheus",state="waiting",} 12.0jvm_threads_states_threads{application="my-prometheus",state="runnable",} 8.0jvm_threads_states_threads{application="my-prometheus",state="timed-waiting",} 2.0jvm_threads_states_threads{application="my-prometheus",state="terminated",} 0.0jvm_threads_states_threads{application="my-prometheus",state="blocked",} 0.0jvm_threads_states_threads{application="my-prometheus",state="new",} 0.0# HELP process_files_open_files The open file descriptor count# TYPE process_files_open_files gauge...省略更多安裝Prometheus
安裝Prometheus很簡(jiǎn)單,在linux系統(tǒng)上安裝,執(zhí)行以下的安裝命令。其他的操作系統(tǒng),比如windows、mac等在官網(wǎng)上(https://prometheus.io/download/)下載并安裝。
23 wget https://github.com/prometheus/prometheus/releases/download/v2.19.2/prometheus-2.19.2.darwin-amd64.tar.gztar xvfz prometheus-*.tar.gzcd prometheus-*修改Prometheus的配置文件prometheus.yml,代碼如下:
global: scrape_interval: 15s # By default, scrape targets every 15 seconds. # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor' # A scrape configuration containing exactly one endpoint to scrape:# Here it's Prometheus itself.scrape_configs: # The job name is added as a label `job=` to any timeseries scraped from this config. - job_name: 'prometheus' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: ['localhost:9090'] - job_name: 'springboot_prometheus' scrape_interval: 5s metrics_path: '/actuator/prometheus' static_configs: - targets: ['127.0.0.1:8081']- config.job_name,配置job的名稱
- config.scrape_interval,配置多久抓一次監(jiān)控信息
- config.metrics_path,獲取監(jiān)控信息的接口
- config.static_configs.targets配置獲取監(jiān)控信息的地址。
使用以下的命令啟動(dòng)prometheus,并通過(guò)–config.file指定配置文件
./prometheus --config.file=prometheus.yml多次請(qǐng)求springboot項(xiàng)目的接口http://localhost:8081/test , 并訪問(wèn)prometheus的控制臺(tái)http://localhost:9090/,展示的界面如下:
prometheus提供了一些可視化圖,比如使用柱狀圖來(lái)展示每秒請(qǐng)求數(shù):
安裝grafana
grafana 是一款采用 go 語(yǔ)言編寫(xiě)的開(kāi)源應(yīng)用,主要用于大規(guī)模指標(biāo)數(shù)據(jù)的可視化展現(xiàn),是網(wǎng)絡(luò)架構(gòu)和應(yīng)用分析中最流行的時(shí)序數(shù)據(jù)展示工具,目前已經(jīng)支持絕大部分常用的時(shí)序數(shù)據(jù)庫(kù)。使用grafana去展示prometheus上的數(shù)據(jù)。先安裝,安裝命令如下:
wget https://dl.grafana.com/oss/release/grafana-7.0.4.darwin-amd64.tar.gztar -zxvf grafana-7.0.4.darwin-amd64.tar.gz./grafana-server訪問(wèn)http://localhost:3000/,初始密碼:admin/admin。
配置數(shù)據(jù)源,如圖:
配置prometheus的地址:http://localhost:9090 ,如圖所示:
在dashboard界面新建panel,展示的metrics為http_server_request_seconds_count,即展示了以時(shí)間為橫軸,請(qǐng)求數(shù)為縱軸的請(qǐng)求曲線,如圖所示:
到此這篇關(guān)于文章就結(jié)束了!
總結(jié)
另外本人整理了一些spring的視頻資料,一共有8集,以及一些Java的學(xué)習(xí)視頻以及資料,免費(fèi)分享給大家,想要資料的可以點(diǎn)贊關(guān)注私信 ‘資料’ 即可免費(fèi)領(lǐng)取。深入底層,剖析源碼。了解本質(zhì)。 愛(ài)編程,愛(ài)生活,愛(ài)分享!
希望對(duì)大家有所幫助,有用的話點(diǎn)贊給我支持!
總結(jié)
以上是生活随笔為你收集整理的springboot 接口404_资深架构带你学习Springboot集成普罗米修斯的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: tensorflow计算图_简单谈谈Te
- 下一篇: mybatis plus当月数据查询_S