SpringBoot第二十四篇:應用監控之Admin


作者:追夢1819
原文:https://www.cnblogs.com/yanfei1819/p/11457867.html
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


## 引言

  前一章(SpringBoot第二十二篇:應用監控之Actuator)介紹了 SpringBoot 應用使用 Actuctor 進行監控。文章最后也提出了一個問題,是否可以將監控結果更好的展示個運維同學?

  本章解答這個問題。

  Spring Boot Admin 是一個管理和監控 Spring Boot 應用程序的開源項目。分為 admin-server 與 admin-client 兩個組件,admin-server通過采集 actuator 端點數據,顯示在 spring-boot-admin-ui 上,已知的端點幾乎都有進行采集,通過 spring-boot-admin 可以動態切換日志級別、導出日志、導出 heapdump、監控各項指標 等。

  Spring Boot Admin 在對單一應用服務監控的同時也提供了集群監控方案,支持通過eurekaconsulzookeeper等注冊中心的方式實現多服務監控與管理。


SpringBoot Admin的使用


創建 admin-server

首先,引入 maven 依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui</artifactId>
</dependency>

為了便於區分,定義端口號為 8081:

server.port=8081

在啟動類上加注解 @EnableAdminServer .

即:

package com.yanfei1819.adminserver;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

我們先來感受一下 SpringBoot Admin 的界面:

頁面中沒有內容,下面創建要監控的客戶端。


創建 admin-client

引入 maven 依賴:

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

設置其端口號為 8082:

server.port=8082
spring.boot.admin.client.url=http://localhost:8081
# 激活所有的端點的web方式請求。如果不激活,將看不到詳細的監控信息。
management.endpoints.web.exposure.include=*

先啟動 admin-server,再啟動 admin-client。

以下是相關的一些界面:

監控的首頁:

Applications:

執行日志:

詳細信息:


在以上的界面中,幾乎囊括了所有的監控信息。讀者感興趣可以逐個詳細了解。此處因篇幅所限,不作展開處理。

如果是需要監控多個客戶端的信息,只需要在客戶端配置文件 application.properties 中配置:

spring.boot.admin.client.url=http://localhost:8081
# 激活所有的端點的web方式請求。如果不激活,將看不到詳細的監控信息。
management.endpoints.web.exposure.include=*

本章源碼寫了兩個客戶端,文中為簡化只演示了一個。


總結

  上述文章只是介紹了 SpringBoot Admin 的相關應用,在實際項目中,可以做安全認證,權限設置,甚至消息通知等。

  到目前為止,本系列已經寫了二十四篇。一路使用的下來的感受是,用比理解簡單。最重要的是理解,理解思想,原理(當然,本系列由於針對的是 SpringBoot 入門的學習者,在原理方面未作深入的詳解。后續將另開一個系列,專攻原理解析),方能使用起來得心應手,碰到問題而不是只會復制、粘貼、問度娘。

  說一句題外話。這一篇與上一篇的時間相距較長。主要是作者在這段時間大概學習了一下分布式、高並發和 MySQL 這三塊的知識點。后面有時間也想將這些分享給大家。

  最后,歡迎大佬拍磚。


ps:本系列的源碼地址


![](https://img2018.cnblogs.com/blog/1183871/201909/1183871-20190904111525706-1403904811.png)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM