目錄
1、什麼是Spring Boot Admin
2、實踐方案
2.1、基于SpringCloud的 Eureka方式實作對注冊微服務進行監控
搭建注冊中心
搭建admin-server
搭建admin-client
2.2、單體SpringBoot直接監控方式
建立Spring Boot Admin Server
建立Spring Boot Admin Client
3、開源項目spring-boot-plus單微服務監控實踐
4、總結
5、參考文章
1、什麼是Spring Boot Admin
Spring Boot Admin是一個開源社群項目,用于管理和監控SpringBoot應用程式。 應用程式作為Spring Boot Admin Client向為Spring Boot Admin Server注冊(通過HTTP)或使用SpringCloud注冊中心(例如Eureka,Consul)發現。 UI是的AngularJs應用程式,展示Spring Boot Admin Client的Actuator端點上的一些監控。常見的功能或者監控如下:
- 顯示健康狀況
- 顯示詳細資訊,例如
- JVM和記憶體名額
- micrometer.io名額
- 資料源名額
- 緩存名額
- 顯示建構資訊編号
- 關注并下載下傳日志檔案
- 檢視jvm系統和環境屬性
- 檢視Spring Boot配置屬性
- 支援Spring Cloud的postable / env-和/ refresh-endpoint
- 輕松的日志級管理
- 與JMX-beans互動
- 檢視線程轉儲
- 檢視http跟蹤
- 檢視auditevents
- 檢視http-endpoints
- 檢視計劃任務
- 檢視和删除活動會話(使用spring-session)
- 檢視Flyway / Liquibase資料庫遷移
- 下載下傳heapdump
- 狀态變更通知(通過電子郵件,Slack,Hipchat,......)
- 狀态更改的事件日志(非持久性)
Spring Boot Admin(下文簡稱SBA)是一個社群開源項目,用于管理和監控你的Spring Boot應用。 應用通過SBA Client注冊到SBA Server中,可通過HTTP請求或者Spring Cloud發現(例如Eureka、Consul),UI展示通過Vue在Spring Boot Actuator端點上擷取應用監控資料進行管理。使用SpringBoot Admin進行監控,個人目前感覺最為實用的功能是可以線上檢視日志功能。
2、實踐方案
2.1、基于SpringCloud的 Eureka方式實作對注冊微服務進行監控
由于目前中小企業大部分使用的是SpringCloud方式進行相關業務應用進行微服務拆分。本人也查閱了網上的相關資料,以及github上共享的此方案的實作方式。下面将針對此種方式進行說明。
本案例也是使用的是Spring Boot版本為2.1.6 、Spring Cloud版本為Finchley.SR2。案例采用Maven多module形式,父pom檔案引入以下的依賴(完整的依賴見源碼),此處省略。
搭建注冊中心
注冊中心使用Eureka、使用Consul也是可以的,在eureka-server工程中的pom檔案中引入:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
配置eureka-server的端口資訊,以及defaultZone和防止自注冊。最後系統暴露eureka-server的actuator的所有端口。
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
register-with-eureka: false
fetch-registry: false
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
在工程的啟動檔案EurekaServerApplication加上@EnableEurekaServer注解開啟Eureka Server.
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run( EurekaServerApplication.class, args );
}
}
eureka-server搭建完畢。
搭建admin-server
在admin-server工程的pom檔案引入admin-server的起步依賴、web的起步依賴、eureka-client的起步依賴,如下:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.6</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>
然後配置admin-server,應用名、端口資訊。并向注冊中心注冊,注冊位址為http://localhost:8761,最後将actuator的所有端口暴露出來,配置如下
server:
port: 8769
spring:
application:
name: sc-admin-server
security:
user:
name: "admin"
password: "admin"
mail:
host: smtp.163.com
username: [email protected]
password: xxxx
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
boot:
admin:
notify:
mail:
from: [email protected]
to: [email protected]
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
logging:
file: ${spring.application.name}/logs/sc-admin-server.log
file.level.root: INFO
file.max-history: 15
file.max-size: 10MB
file.pattern:
console: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
health:
db:
enabled: false
mail:
enabled: false
redis:
enabled: false
mongo:
enabled: false
在工程的啟動類AdminServerApplication加上@EnableAdminServer注解,開啟admin server的功能,加上@EnableDiscoveryClient注解開啟eurke client的功能。
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run( AdminServerApplication.class, args );
}
}
搭建admin-client
在admin-client的pom檔案引入以下的依賴,由于2.1.7采用webflux,引入webflux的起步依賴,引入eureka-client的起步依賴,并引用actuator的起步依賴如下:
<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>
在工程的配置檔案配置應用名、端口、向注冊中心注冊的位址,以及暴露actuator的所有端口。
spring:
application:
name: sc-admin-client
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
logging:
file: ${spring.application.name}/logs/sc-admin-client.log
file.level.root: INFO
file.max-history: 15
file.max-size: 10MB
file.pattern:
console: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
server:
port: 8762
在啟動類加上@EnableDiscoveryCliet注解,開啟DiscoveryClient的功能。
@SpringBootApplication
@EnableDiscoveryClient
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run( AdminClientApplication.class, args );
}
}
一次啟動三個工程,在浏覽器上通路localhost:8761,localhost:8769,localhost::8762顯示如下圖所示
localhost:8769 輸入使用者名/密碼 admin/admin
檢視sc-admin-server的實時日志檔案如下所示
2.2、單體SpringBoot直接監控方式
建立Spring Boot Admin Server
在工程admin-server引入admin-server的起來依賴和web的起步依賴,代碼如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gf</groupId>
<artifactId>admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.6</spring-boot-admin.version>
</properties>
<dependencies>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
然後在工程的啟動類AdminServerApplication加上@EnableAdminServer注解,開啟AdminServer的功能,代碼如下:
@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run( AdminServerApplication.class, args );
}
}
在工程的配置檔案application.yml中配置程式名和程式的端口,代碼如下:
spring:
application:
name: admin-server
server:
port: 8769
這樣Admin Server就建立好了。
建立Spring Boot Admin Client
在admin-client工程的pom檔案引入admin-client的起步依賴和web的起步依賴,代碼如下:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在工程的配置檔案application.yml中配置應用名和端口資訊,以及向admin-server注冊的位址為http://localhost:8769,最後暴露自己的actuator的所有端口資訊,具體配置如下:
spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:8769
server:
port: 8768
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
在工程的啟動檔案如下:
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run( AdminClientApplication.class, args );
}
}
一次啟動兩個工程,在浏覽器上輸入localhost:8769 ,浏覽器顯示的界面如下
示例檔案源碼所在位置:https://github.com/jianxia612/StudySampleJava/tree/master/springboot-admin
3、開源項目spring-boot-plus單微服務監控實踐
本文僅僅隻針對關于SpringBoot Admin的相關(pom.xml)配置和說明如下所示:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project-parent.version>${parent.version}</project-parent.version>
<java.version>1.8</java.version>
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
</properties>
<dependencies>
<!-- spring boot admin start -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<!-- spring boot admin end -->
</dependencies>
其中yaml檔案配置如下:
# 友善Spring Boot Admin頁面上實時檢視日志
# logback.xml中有詳細的日志配置
logging:
file: logs/spring-boot-plus-log.log
最後運作後監控截圖如下所示:
spring-boot-plus 所在開源位址:https://gitee.com/geekidea/spring-boot-plus.git
4、總結
本文主要的目的是針對如何使用SpringBoot Admin進行SpringBoot微服務後端程式的時候監控。并且通過使用SpringCloud和基本SpringBoot進行監控基本實作。同時本人也參考了其他人不少文章。同時也是作為本人一個學習終結,可以針對以後搭建監控應用作為備注。如果能夠幫助學習此部分相關知識的人也是一種榮幸。
5、參考文章
Spring Boot Admin 2.1.0 全攻略 SpringBoot2.x搭建SpringBootAdmin2.x
Spring Boot Admin 監控 Spring Boot Admin-應用健康監控背景管理
SpringBoot admin 2.0 詳解(特别詳細)