一、簡介
Spring Boot Admin是一個完整的應用程序,用於監控和管理Spring Boot應用。原理是通過調用Spring Boot Actuator提供的http接口來實現的監控和管理,所以要想能夠被Spring Boot Admin 的Server端管理,則Client端必須集成了Spring Boot Actuator。
-
可監控內容:
狀態;基本信息;JVM和堆棧實時數據;應用配置信息;緩存指標;線程數據;接口調用數據 -
可管理內容:
按照包名調整日志級別;管理、查看和下載系統日志;報警(服務異常下線發送郵件)
AdminServer的web界面:
二、Spring-Boot集成
Spring Boot Admin是C/S結構,server端負責收集和處理client端的數據,並且可以通過向client端發送命令來管理client。
項目工程目錄結構:
三、Server端
- 引入所需的pom依賴:spring-boot-starter-web;spring-boot-admin-starter-server
- application.yml配置文件中配置服務的端口:server.port=9000
- 啟動類上面添加EnableAdminServer的注解
完整的pom文件:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.naylor</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.3.0</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
啟動AdminServer后訪問 http://127.0.0.1:9000/applications 可以看到“暫無應用注冊”的提示
四、Client端
- 引入所需的pom依賴:spring-boot-starter-web;spring-boot-admin-starter-client;spring-boot-actuator
- application.propertis配置文件中增加相關配置:服務端口;服務名稱;開啟actuator所有端點都可以被訪問;AdminServer服務端的地址
完整的pom文件:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.naylor</groupId>
<artifactId>admin-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.3.0</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-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
完整的配置文件:
spring.boot.admin.client.url=http://localhost:9000
server.port=8003
spring.application.name=admin-client
management.endpoints.web.exposure.include=*
啟動AdminClient后,訪問 http://127.0.0.1:8003/actuator , 先看看actuator是否正常
同時啟動AdminServer和AdminClient:
訪問 http://127.0.0.1:9000/wallboard 可以看到一個client端已經注冊到了AdminServer之中
五、集成到Eureka
集成到Eureka之后,就不必在每一個Admin-Client的配置文件中都配置一遍Admin-Server的地址,讓Admin-Server主動從Eureka中拉取Admin-Client,並獲取Actuator地址,進而進行監控和管理。
1、Admin-Server的配置文件中加入Eureka-Server地址:
eureka:
instance:
hostname: localhost
#eureka-client 向 eureka-server端發送心跳包的頻率
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
#是否把自己向eureka-server注冊
registerWithEureka: false
fetch-registry: true
#從eureka-server拉取已注冊服務的列表的頻率
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: http://${eurekaclient.username}:${eurekaclient.password}@localhost:7001/eureka/
配置了serviceUrl之后,Admin-Server就可以從Eureka中獲取已經注冊的微服務信息,並進行監控和管理。
至於registerWithEureka是否配置為true,取決於自己的喜歡。配置為false,Admin-Server不會注冊到Eureka-Server
;配置為true,Admin-Server就會注冊到Eureka-Server中,那么,意味着Admin-Server從注冊中心拉取微服務的時候會把自己(即Admin-Server)也拉取下來進行監控和管理。
2、Admin-Client的配置文件中去除已經配置的Admin-Server地址:
# 注釋掉配置的Admin-Server地址
# spring.boot.admin.client.url=http://localhost:9000
注釋掉配置的Admin-Server地址的目的是避免重復,因為當前的Admin-Client作為一個微服務已經注冊到了Eureka-Server,那么Admin-Server就已經可以從Eureka-Server中拉取到此服務的Actuator信息。
若不注釋,那么在Admin-Server的WallBoard中將看到兩個重復的Admin-Client。
引用:
https://www.cnblogs.com/huanchupkblog/p/10563629.html
https://www.jianshu.com/p/1749f04105fb