【Spring Boot】利用 Spring Boot Admin 進行專案監控管理
利用 Spring Boot Admin 進行專案監控管理
一、Spring Boot Admin 是什麼
Spring Boot Admin (SBA) 是一個社群開源專案,用於管理和監視 Spring Boot 應用程式。應用程式通過 http
的方式註冊到 Spring Boot 管理客戶端,或者通過 Spring Cloud 的服務發現機制,然後針對 actuator
介面將資料通過 Vue.js
進行視覺化管理。
對於我們來說,我們可以通過 Spring Boot Admin 瀏覽所有被監控的 Spring Boot 專案,詳細的 Health 資訊、記憶體資訊、JVM 系統和環境屬性、垃圾回收資訊等。
二、Spring Boot Admin 起步
Spring Boot Admin 由兩種角色組成:一種是 Server 端;一種是 Client 端,即要被監控的應用。下面分別進行兩種角色的配置:
Spring Boot Admin Server
引入依賴 [pom.xml]
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.0.0</version> </dependency>
@EnableAdminServer 註解
@SpringBootApplication
@EnableAdminServer
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
application.yml 配置
spring: application: name: spring-boot-admin-server server: port: 8080 management: endpoint: health: show-details: always endpoints: web: exposure: include: '*'
啟動後訪問 http://localhost:8080
就可以看見一個 UI 介面
此時應用和例項都是,是因為我們沒有註冊客戶端,接下來我們實現來客戶端。
Spring Boot Admin Client
引入客戶端依賴 [pom.xml]
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.0</version>
</dependency>
程式入口沒有特別需要修改的地方
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
application.yml 配置
spring:
application:
name: spring-boot-admin-client
boot:
admin:
client:
url: http://localhost:8080
server:
port: 8081
此時再訪問 localhost:8080
就可以發現新增了一個應用例項,如下圖所示:
那麼到這裡我們一個基本的 Spring Boot Admin 應用就配置成功了。
點選選單欄中的 wallboard
,再點選要檢視的應用程式,就可以訪問應用的資訊,如記憶體狀態資訊等:
三、SBA 結合 Spring Cloud 註冊中心
除了上面案例中,直接在客戶端中配置相應的 SBA 配置外,還可以配合 Spring Cloud 的服務註冊與發現應用(例如:Eureka, Consul) ,接下來演示如何配置:
新建統一的依賴管理
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jojo</groupId>
<artifactId>admin-demo-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>admin-demo-dependencies</name>
<description>Demo project for Spring Boot Admin</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<!-- Environment Settings -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- Spring Settings -->
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
搭載服務註冊中心
引入依賴
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>
應用程式入口
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
application.yml
spring:
application:
name: admin-demo-eureka
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
service-url:
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka
register-with-eureka: false
fetch-registry: false
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
配置成功後啟動應用程式,訪問 localhost:8761
,可以看到類似下圖的 UI 介面:
建立 SBA 服務端
引入依賴
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
應用程式入口
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
application.yml 配置
spring:
application:
name: admin-demo-admin-server
server:
port: 8769
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
建立 SBA 客戶端
引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
applicaion.yml 配置檔案
spring:
application:
name: admin-demo-admin-client
server:
port: 8762
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
應用程式入口
@SpringBootApplication
@EnableDiscoveryClient
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
將三個應用啟動後,訪問 localhost:8769
就可以監控各個應用的執行情況了:
四、整合郵件通知
SBA 中也可以整合郵件通知,當註冊的服務下線、宕機時,向指定的郵箱傳送資訊郵件。其配置也十分容易,只需要在 yaml
配置檔案中配置郵箱的傳送方和郵件的收件方,以及在 pom
檔案中引入 `` 的依賴即可。下面演示這段配置:
pom.xml配置:加入 mail 的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.yml:在配置檔案中配置收件人以及發件方資訊,並且開啟郵箱提醒
spring:
boot:
admin:
notify:
mail:
enabled: true
from: [email protected]
...
spring.mail.host: smtp.example.com
spring.mail.username: [email protected]
spring.mail.password: # 授權碼
spring.boot.admin.notify.mail.to: [email protected]
配置並啟動完成後,可以嘗試將 Client 專案中止:
然後開啟收件方的郵箱,就可以看見 SBA 傳送的郵件了
五、參考
SBA 官方文件:https://codecentric.github.io/spring-boot-admin/curre