溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控

發(fā)布時(shí)間:2021-06-01 16:13:34 來源:億速云 閱讀:346 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

具體如下:

使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控

1、Spring Boot 應(yīng)用暴露監(jiān)控指標(biāo)【版本 1.5.7.RELEASE】

首先,添加依賴如下依賴:

<dependency>       
            <groupId>org.springframework.boot</groupId>     
            <artifactId>spring-boot-starter-actuator</artifactId>  
		</dependency>   
/*采集應(yīng)用的指標(biāo)信息,我們使用的是prometheus,相應(yīng)的我們引入包:*/
		<dependency>          
            <groupId>io.prometheus</groupId>    
            <artifactId>simpleclient_spring_boot</artifactId>            						<version>0.0.26</version>    
		</dependency>

然后,在啟動(dòng)類 Application.java 添加如下注解:

@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class Application {  
    public static void main(String[] args) {        SpringApplication.run(Application.class, args); 
	}
}

最后,配置默認(rèn)的登錄賬號(hào)和密碼,在 application.yml 中:

security:
  user:
    name:user
    password: pwd

啟動(dòng)應(yīng)用程序后,會(huì)看到如下一系列的 Mappings

使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控

利用賬號(hào)密碼訪問 http://localhost:8080/application/prometheus ,可以看到 Prometheus 格式的指標(biāo)數(shù)據(jù)

使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控

2、Prometheus 采集 Spring Boot 指標(biāo)數(shù)據(jù)

首先,獲取 Prometheus 的 Docker 鏡像:

$ docker pull prom/prometheus

然后,編寫配置文件 prometheus.yml

global:
  scrape_interval: 10s
  scrape_timeout: 10s
  evaluation_interval: 10m
scrape_configs:
  - job_name: spring-boot
    scrape_interval: 5s
    scrape_timeout: 5s
    metrics_path: /application/prometheus
    scheme: http
    basic_auth:
      username: admin
      password: 123456
    static_configs:
      - targets:
        - 192.168.11.54:8099 #此處填寫 Spring Boot 應(yīng)用的 IP + 端口號(hào)

接著,啟動(dòng) Prometheus :

docker run -d --name prometheus -p 9090:9090
-v D:\test\actuator\prometheus\prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

請(qǐng)注意,D:\test\actuator\prometheus\prometheus.yml ,是我的配置文件存放地址,我們需要將它放到容器里面去,所以用了-v來做文件映射。/etc/prometheus/prometheus.yml這個(gè)是容器啟動(dòng)的時(shí)候去取的默認(rèn)配置,這里我是直接覆蓋掉了它。prom/prometheus這是鏡像,如果本地沒有,就回去你設(shè)置好的鏡像倉庫去取。

啟動(dòng)完成后用**docker ps**看下是否已經(jīng)啟動(dòng)成功,之后打開瀏覽器輸入:
http://localhost:9090/targets,檢查 Spring Boot 采集狀態(tài)是否正常,如果看到下圖就是成功了。

使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控

3、Grafana 可視化監(jiān)控?cái)?shù)據(jù)

首先,獲取 Grafana 的 Docker 鏡像:

$ docker pull grafana/grafana

然后,啟動(dòng) Grafana:

$ docker run --name grafana -d -p 3000:3000 grafana/grafana

接著,訪問 http://localhost:3000/ 配置 Prometheus 數(shù)據(jù)源:

Grafana 登錄賬號(hào) admin 密碼 admin

先配置數(shù)據(jù)源.

使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控

2.配置單個(gè)指標(biāo)的可視化監(jiān)控面板:

使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控

使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控

使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控

prometh采集的數(shù)據(jù)

使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控

使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控

以上就是使用SpringBoot怎么實(shí)現(xiàn)可視化監(jiān)控,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI