目錄
2.1、基於SpringCloud的 Eureka方式實現對註冊微服務進行監控
3、開源項目spring-boot-plus單微服務監控實踐
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