1. 程式人生 > >SpringBoot學習筆記(14):使用SpringBootAdmin管理監控你的應用

SpringBoot學習筆記(14):使用SpringBootAdmin管理監控你的應用

SpringBoot學習筆記(14):使用SpringBootAdmin管理監控你的應用

Spring Boot Admin是一個管理和監控Spring Boot應用程式的應用程式。本文參考文件:

  • 官方文件:http://codecentric.github.io/spring-boot-admin/1.5.6
  • GITHUB倉庫:https://github.com/codecentric/spring-boot-admin
  • 純潔的微笑:http://www.ityouknow.com/springboot/2018/02/11/spring-boot-admin.html

SBA現在有三個版本,下面是是三個版本的參考文件,本文基於1.5.7

SpringBootAdmin是什麼

  Spring Boot Admin是一個管理和監控Spring Boot應用程式的應用程式。應用程式向我們的Spring Boot Admin Client註冊(通過HTTP)或使用Spring Cloud(例如Eureka)發現。它是基於AngularJs在前端顯示Spring Boot Actuator資料的應用。

  

快速開始

新建一個SpringBoot應用作為服務端

  首先,您需要設定您的伺服器。要做到這一點,只需設定一個簡單的啟動專案(例如使用start.spring.io)。

1.將Spring Boot Admin Server和UI新增到依賴項:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server</artifactId>
    <version>1.5.6</version>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui</artifactId>
    <version>1.5.6</version>
</dependency>

2.通過將@EnableAdminServer新增到您的配置來引入Spring Boot Admin Server配置:

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

 我在這裡將該應用的埠設定為8088,啟動應用後,可看到其效果:

  

在客戶端註冊服務

1.新增客戶端必要依賴

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>1.5.6</version>
</dependency>

2.通過配置Spring Boot Admin Server的URL來啟用SBA客戶端:

1.spring.boot.admin.url: http://localhost:8080  
2.management.security.enabled: false   

說明:
1.要註冊的Spring Boot Admin Server的URL。

2.從Spring Boot 1.5.x開始,預設情況下所有端點都是安全的。為簡潔起見,我們暫時禁用了安全性。檢視有關如何處理安全端點的安全性部分。 

配置完成之後,啟動Client端服務,再次訪問服務:http://localhost:8000可以看到客戶端的相關資訊。

  

郵件通知

使用 spring-boot-starter-mail 建立郵箱傳送器

pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

application.properties

spring.mail.host=smtp.example.com
[email protected]  

其他的郵箱通知註冊項

Property name Description Default value

spring.boot.admin.notify.mail.enabled

Enable mail notifications

true

spring.boot.admin.notify.mail.ignore-changes

Comma-delimited list of status changes to be ignored. Format: "<from-status>:<to-status>". Wildcards allowed.

"UNKNOWN:UP"

spring.boot.admin.notify.mail.to

Comma-delimited list of mail recipients

"[email protected]"

spring.boot.admin.notify.mail.cc

Comma-delimited list of carbon-copy recipients

 

spring.boot.admin.notify.mail.from

Mail sender

 

spring.boot.admin.notify.mail.subject

Mail subject. SpEL-expressions are supported

"#{application.name} (#{application.id}) is #{to.status}"

spring.boot.admin.notify.mail.text

Mail body. SpEL-expressions are supported

"#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}"

配置完成後,重新啟動專案spring-boot-admin-server,這樣Admin Server就具備了郵件告警的功能。