【Java Spring Cloud 實戰之路】新增一個SpringBootAdmin監控
阿新 • • 發佈:2020-06-11
# 0. 前言
在之前的幾章中,我們先搭建了一個專案骨架,又搭建了一個使用nacos的gateway閘道器專案,閘道器專案中並沒有配置太多的東西。現在我們就接著搭建在Spring Cloud 微服務中另一個重要的專案 - Spring boot admin.
# 1. Spring Boot Admin 介紹
![](https://img2020.cnblogs.com/other/1266612/202006/1266612-20200611090224215-926276068.png)
Spring Boot Admin 用來監控基於Spring Boot的應用,在Spring Boot Actuator的基礎上提供了簡潔的視覺化Web UI。Spring Boot Admin 提供了以下功能:
- 顯示應用的健康狀態
- 顯示應用的細節內容: JVM和記憶體資訊,micrometer資訊, 資料來源資訊,快取資訊等
- 顯示 編譯版本
- 檢視和下載日誌
- 檢視jvm引數和環境變數值
- 檢視Spring Boot專案配置
- 顯示 thread dump
- 顯示 http-traces
……
等一系列內容。
# 2. 建立一個 Spring Boot Admin專案
那麼,我們就來建立一個Spring Boot Admin 專案吧。
## 2.1 建立 Spring Boot Admin 服務端
在manager 目錄下,建立一個 monitor目錄,並在monitor目錄下建立一個pom.xml 檔案,新增以下內容:
```xml
```
在 manager/pom.xml 註冊我們新建的專案模組:
```xml
```
在 monitor 建立如下目錄:
```
.
├── pom.xml
└── src
└── main
├── java
└── resources
```
在根目錄的pom.xml 新增 Spring Boot Admin 依賴:
先新增spring-boot-admin版本號變數:
```xml
```
並在dependencyManagement > dependencies 下新增:
```xml
```
在monitor/pom.xml檔案中新增:
```xml
```
執行
> mvn clean install
檢查並刷mvn引用快取。
建立MonitorApplication類:
```java
package club.attachie.nature.monitor;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAdminServer
public class MonitorApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorApplication.class, args);
}
}
```
啟動後能看到如下介面:
![](https://img2020.cnblogs.com/other/1266612/202006/1266612-20200611090224412-2019924155.png)
# 3 與閘道器服務進行互通
在上一篇中,我們添加了Spring Cloud Gateway專案,到目前為止兩個專案之間完全割裂沒有關聯。在這一節,我們在兩者之間建立關聯。也就是說,將gateway 專案引入Spring Admin Boot監聽。
在 manager/gateway 的pom.xml 檔案中加入如下引用:
```xml
```
然後修改 gateway專案的啟動埠,在resources/bootstrap.yml 新增:
```yml
server:
port: 8070
```
在 monitor中加入nacos引用:
```xml
```
修改MonitorApplication 為:
```java
package club.attachie.nature.monitor;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
@SpringBootApplication
@EnableAdminServer
@RefreshScope
public class MonitorApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorApplication.class, args);
}
}
```
建立monitor專案的bootsrap.yml:
```yaml
spring:
application:
name: monitor
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
```
關於這裡的配置 在上一篇 中有個錯誤,應該是 discovery > server-addr,不是 config > server-addr。兩者有區別,discovery表示設定nacos為服務發現中心,config表示nacos為配置中心。
啟動 gateway 專案和 monitor專案檢視效果, 訪問 8080埠:
![圖片](https://img2020.cnblogs.com/other/1266612/202006/1266612-20200611090224688-188611953.png)
可以看到兩個應用可以被發現,如果沒有設定monitor專案把nacos當做服務發現中心,將無法獲取到具體線上的應用。點選 gateway 進去後可以檢視到:
![](https://img2020.cnblogs.com/other/1266612/202006/1266612-20200611090225002-835923372.png)
# 4. 總結
我們搭建了一個Spring Boot Admin 專案作為一個監控系統,後續會在這裡新增更多的內容。
> 更多內容煩請關注[我的部落格《高先生小屋》](https://www.attachie.club)
![file](https://img2020.cnblogs.com/other/1266612/202006/1266612-20200611090225412-335087