利用 Spring Boot Admin 进行项目监控管理
一、Spring Boot Admin 是什么
Spring Boot Admin (SBA) 是一个社区开源项目,用于管理和监视 Spring Boot 应用程序。应用程序通过 http
的方式注册到 Spring Boot 管理客户端,或者通过 Spring Cloud 的服务发现机制,然后针对 actuator
接口将数据通过 Vue.js
进行可视化管理。
对于我们来说,我们可以通过 Spring Boot Admin 浏览所有被监控的 Spring Boot 项目,详细的 Health 信息、内存信息、JVM 系统和环境属性、垃圾回收信息等。
二、Spring Boot Admin 起步
Spring Boot Admin 由两种角色组成:一种是 Server 端;一种是 Client 端,即要被监控的应用。下面分别进行两种角色的配置:
Spring Boot Admin Server
引入依赖 [pom.xml]
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.0</version>
</dependency>
@EnableAdminServer 注解
@SpringBootApplication
@EnableAdminServer
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
application.yml 配置
spring:
application:
name: spring-boot-admin-server
server:
port: 8080
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
启动后访问 http://localhost:8080
就可以看见一个 UI 界面
此时应用和实例都是,是因为我们没有注册客户端,接下来我们实现来客户端。
Spring Boot Admin Client
引入客户端依赖 [pom.xml]
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.0</version>
</dependency>
程序入口没有特别需要修改的地方
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
application.yml 配置
spring:
application:
name: spring-boot-admin-client
boot:
admin:
client:
url: http://localhost:8080
server:
port: 8081
此时再访问 localhost:8080
就可以发现新增了一个应用实例,如下图所示:
那么到这里我们一个基本的 Spring Boot Admin 应用就配置成功了。
点击菜单栏中的 wallboard
,再点击要查看的应用程序,就可以访问应用的信息,如内存状态信息等:
三、SBA 结合 Spring Cloud 注册中心
除了上面案例中,直接在客户端中配置相应的 SBA 配置外,还可以配合 Spring Cloud 的服务注册与发现应用(例如:Eureka, Consul) ,接下来演示如何配置:
新建统一的依赖管理
<?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>
<groupId>com.jojo</groupId>
<artifactId>admin-demo-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>admin-demo-dependencies</name>
<description>Demo project for Spring Boot Admin</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<!-- Environment Settings -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- Spring Settings -->
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.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>
搭载服务注册中心
引入依赖
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>
应用程序入口
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
application.yml
spring:
application:
name: admin-demo-eureka
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
service-url:
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka
register-with-eureka: false
fetch-registry: false
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
配置成功后启动应用程序,访问 localhost:8761
,可以看到类似下图的 UI 界面:
创建 SBA 服务端
引入依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.0</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>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
应用程序入口
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
application.yml 配置
spring:
application:
name: admin-demo-admin-server
server:
port: 8769
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
创建 SBA 客户端
引入依赖
<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>
applicaion.yml 配置文件
spring:
application:
name: admin-demo-admin-client
server:
port: 8762
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
应用程序入口
@SpringBootApplication
@EnableDiscoveryClient
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
将三个应用启动后,访问 localhost:8769
就可以监控各个应用的运行情况了:
四、集成邮件通知
SBA 中也可以集成邮件通知,当注册的服务下线、宕机时,向指定的邮箱发送信息邮件。其配置也十分容易,只需要在 yaml
配置文件中配置邮箱的发送方和邮件的收件方,以及在 pom
文件中引入 `` 的依赖即可。下面演示这段配置:
pom.xml配置:加入 mail 的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.yml:在配置文件中配置收件人以及发件方信息,并且开启邮箱提醒
spring:
boot:
admin:
notify:
mail:
enabled: true
from: [email protected]
...
spring.mail.host: smtp.example.com
spring.mail.username: [email protected]
spring.mail.password: # 授权码
spring.boot.admin.notify.mail.to: [email protected]
配置并启动完成后,可以尝试将 Client 项目中止:
然后打开收件方的邮箱,就可以看见 SBA 发送的邮件了
五、参考
SBA 官方文档:https://codecentric.github.io/spring-boot-admin/current/