23.使用Spring Boot Admin监控服务
使用Spring Boot Admin监控服务在使用Actuator监控Spring Boot应用一节中我们介绍了使用Actuator来监控Spring Boot应用,其提供了许多REST接口来查看应用的信息。但其返回的是大量的JSON格式数据,信息看上去不直观也不易于理解。而Spring Boot Admin(SBA)是一款基于Actuator开发的开源软件,以图形化界面的方式展示Spring.
使用Spring Boot Admin监控服务
在使用Actuator监控Spring Boot应用一节中我们介绍了使用Actuator来监控Spring Boot应用,其提供了许多REST接口来查看应用的信息。但其返回的是大量的JSON格式数据,信息看上去不直观也不易于理解。而Spring Boot Admin(SBA)是一款基于Actuator开发的开源软件,以图形化界面的方式展示Spring Boot应用的配置信息、Beans信息、环境属性、线程信息、JVM状况等。本文使用的Spring Boot Admin版本为2.1.7,有能力的朋友可以直接阅读官方文档 。【声明一下,截至2019-09-05这个时间点github 上最新版本参考文档版本为2.1.6】
构建环境
Requirements:
- Node.js v8.x (LTS)
- JDK >= 1.8
1. 搭建SBA服务端
搭建一个SBA服务端(Server),其他被监控的Spring Boot应用作为客户端(Client),客户端通过HTTP的方式将自己注册到服务端,以供服务端进行监控服务。大致关系如下图所示:
1.1 引入SBA Server依赖
使用http://start.spring.io/开启一个简单的Spring Boot项目,然后引入spring-boot-admin-server相关依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.1.6</version>·
</dependency>
1.2 开启Admin监控
在Spring Boot入口类中加入@EnableAutoConfiguration,@EnableAdminServer注解开启监控功能:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import de.codecentric.boot.admin.config.EnableAdminServer;
@SpringBootApplication
@EnableAutoConfiguration
@EnableAdminServer
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
然后在yml中对项目进行简单的配置:
server:
port: 8080
context-path: /admin-server
启动项目,访问http://localhost:8080/admin-server:
因为还没添加客户端,所以监控列表里是空的,接下来创建一个Spring Boot应用作为客户端。
2. SBA客户端
2.1 引入SBA Client依赖
同样的,使用http://start.spring.io/搭建一个简单的Spring Boot项目作为SBA客户端,然后引入Client依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.6</version>
</dependency>
yml配置:
management:
security:
enabled: false
server:
port: 8081
spring:
boot:
admin:
url: http://localhost:8080/admin-server
spring.boot.admin.url配置的是SBA服务端的地址,management.security.enabled: false这里先关闭安全验证。启动客户端后查看http://localhost:8080/admin-server地址:
可看到SBA客户端已经被注册到SBA服务端了,接下来便可以在SBA服务端页面上查看各种SBA客户端信息:
2.2 客户端额外信息
默认客户端在服务端监控列表呈现的信息并不多:
我们可以在SBA客户端yml里配置更为详细的信息:
info:
app:
name: "@project.name@"
description: "@project.description@"
version: "@project.version@"
spring-boot-version: "@project.parent.version@"
然后便可以在SBA服务端看到较为详细的客户端信息:
3. 添加邮件预警
SBA服务端也可以配置邮件预警服务,默认情况下对于被检测的应用启动或者停止的时候会触发预警。
首先添加邮件依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
然后在SBA服务端的yml中配置邮件预警:
spring:
mail:
host: smtp.163.com
username: xxx@163.com
password: xxx
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
boot:
admin:
notify:
mail:
from: xxx@163.com
to: xxx@qq.com
邮件的具体使用方法可参考https://mrbird.cc/Spring-Boot-Email.html#。
当被检测的应用关停的时候,收到如下邮件:
4. 附录
4.1 SBA Server配置说明
Property name | Description | Default value | 中文说明 |
---|---|---|---|
spring.boot.admin.context-path | The context-path prefixes the path where the Admin Server’s statics assets and API should be served. Relative to the Dispatcher-Servlet. | Admin Server 保留的静态访问和API的前缀(当你在业务应用中使用而不是单独使用时就很有必要了) | |
spring.boot.admin.monitor.period | Time interval in ms to update the status of applications with expired status-informations. | 10.000 | 更新应用信息的频率,单位毫秒 |
spring.boot.admin.monitor.status-lifetime | Lifetime of application statuses in ms. The applications /health-endpoint will not be queried until the lifetime has expired. | 10.000 | 被监控的应用信息的过期时间,单位毫秒 |
4.2 SBA Client配置说明
Property name | Description | Default value | 中文说明 |
---|---|---|---|
spring.boot.admin.client.enabled | Enables the Spring Boot Admin Client. | true | 默认开启 |
spring.boot.admin.url | List of URLs of the Spring Boot Admin server to register at. This triggers the AutoConfiguration. Mandatory. | admin server 的地址列表,此设置会触发自动配置,必须 | |
spring.boot.admin.api-path | Http-path of registration endpoint at your admin server. | “api/applications” | 注册到 admin server 端点的 Http-path |
4.3 邮件配置选项
下边属性需要自己填写。
Property name | Description | Default value | 中文说明 |
---|---|---|---|
spring.boot.admin.client.enabled | Enables the Spring Boot Admin Client. | true | 默认开启 |
spring.boot.admin.url | List of URLs of the Spring Boot Admin server to register at. This triggers the AutoConfiguration. Mandatory. | admin server 的地址列表,此设置会触发自动配置,必须 | |
spring.boot.admin.api-path | Http-path of registration endpoint at your admin server. | “api/applications” | 注册到 admin server 端点的 Http-path |
更多推荐
所有评论(0)