【Java Spring Cloud 實戰之路】添加一個SpringBootAdmin監控

0. 前言

在以前的幾章中,咱們先搭建了一個項目骨架,又搭建了一個使用nacos的gateway網關項目,網關項目中並無配置太多的東西。如今咱們就接着搭建在Spring Cloud 微服務中另外一個重要的項目 - Spring boot admin.java

1. Spring Boot Admin 介紹

Spring Boot Admin 用來監控基於Spring Boot的應用,在Spring Boot Actuator的基礎上提供了簡潔的可視化Web UI。Spring Boot Admin 提供瞭如下功能:web

  • 顯示應用的健康狀態
  • 顯示應用的細節內容: JVM和內存信息,micrometer信息, 數據源信息,緩存信息等
  • 顯示 編譯版本
  • 查看和下載日誌
  • 查看jvm參數和環境變量值
  • 查看Spring Boot項目配置
  • 顯示 thread dump
  • 顯示 http-traces

……spring

等一系列內容。apache

2. 建立一個 Spring Boot Admin項目

那麼,咱們就來建立一個Spring Boot Admin 項目吧。bootstrap

2.1 建立 Spring Boot Admin 服務端

在manager 目錄下,建立一個 monitor目錄,並在monitor目錄下建立一個pom.xml 文件,添加如下內容:緩存

<?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>

    <artifactId>monitor</artifactId>
    <version>${revision}</version>
    <packaging>jar</packaging>
    <parent>
        <artifactId>manager</artifactId>
        <groupId>club.attachie</groupId>
        <version>${revision}</version>
    </parent>

</project>

在 manager/pom.xml 註冊咱們新建的項目模塊:app

<modules>
    <module>gateway</module>
    <module>monitor</module>
</modules>

在 monitor 建立以下目錄:jvm

.
├── pom.xml
└── src
    └── main
        ├── java
        └── resources

在根目錄的pom.xml 添加 Spring Boot Admin 依賴:maven

先添加spring-boot-admin版本號變量:spring-boot

<spring-boot-admin.version>2.2.3</spring-boot-admin.version>

並在dependencyManagement > dependencies 下添加:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>${spring-boot-admin.version}</version>
</dependency>

在monitor/pom.xml文件中添加:

<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>
</dependencies>

運行

mvn clean install

檢查並刷mvn引用緩存。

建立MonitorApplication類:

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);
    }
}

啓動後能看到以下界面:

3 與網關服務進行互通

在上一篇中,咱們添加了Spring Cloud Gateway項目,到目前爲止兩個項目之間徹底割裂沒有關聯。在這一節,咱們在二者之間創建關聯。也就是說,將gateway 項目引入Spring Admin Boot監聽。

在 manager/gateway 的pom.xml 文件中加入以下引用:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

而後修改 gateway項目的啓動端口,在resources/bootstrap.yml 添加:

server:
  port: 8070

在 monitor中加入nacos引用:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>      
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

修改MonitorApplication 爲:

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:

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端口:

圖片

能夠看到兩個應用能夠被發現,若是沒有設置monitor項目把nacos當作服務發現中心,將沒法獲取到具體在線的應用。點擊 gateway 進去後能夠查看到:

4. 總結

咱們搭建了一個Spring Boot Admin 項目做爲一個監控系統,後續會在這裏添加更多的內容。

更多內容煩請關注個人博客《高先生小屋》

file

相關文章
相關標籤/搜索