使用Spring Boot Admin监控服务

使用Actuator监控Spring Boot应用一节中我们介绍了使用Actuator来监控Spring Boot应用,其提供了许多REST接口来查看应用的信息。但其返回的是大量的JSON格式数据,信息看上去不直观也不易于理解。而Spring Boot Admin(SBA)是一款基于Actuator开发的开源软件:https://github.com/codecentric/spring-boot-admin,以图形化界面的方式展示Spring Boot应用的配置信息、Beans信息、环境属性、线程信息、JVM状况等。本文使用的Spring Boot Admin版本为1.5.7,有能力的朋友可以直接阅读官方文档:http://codecentric.github.io/spring-boot-admin/1.5.7

搭建SBA服务端

搭建一个SBA服务端(Server),其他被监控的Spring Boot应用作为客户端(Client),客户端通过HTTP的方式将自己注册到服务端,以供服务端进行监控服务。大致关系如下图所示:

QQ截图20180514100246.png

引入SBA Server依赖

使用http://start.spring.io/开启一个简单的Spring Boot项目,然后引入spring-boot-admin-server相关依赖:

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.7</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.7</version>·
</dependency>

开启Admin监控

在Spring Boot入口类中加入@EnableAutoConfiguration@EnableAdminServer注解开启监控功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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中对项目进行简单的配置:

1
2
3
server:
port: 8080
context-path: /admin-server

启动项目,访问http://localhost:8080/admin-server

QQ截图20180514102405.png

因为还没添加客户端,所以监控列表里是空的,接下来创建一个Spring Boot应用作为客户端。

SBA客户端

引入SBA Client依赖

同样的,使用http://start.spring.io/搭建一个简单的Spring Boot项目作为SBA客户端,然后引入Client依赖:

1
2
3
4
5
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.7</version>
</dependency>

yml配置:

1
2
3
4
5
6
7
8
9
10
11
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地址:

QQ截图20180514104330.png

可看到SBA客户端已经被注册到SBA服务端了,接下来便可以在SBA服务端页面上查看各种SBA客户端信息:

2018-05-14_105123.png

客户端额外信息

默认客户端在服务端监控列表呈现的信息并不多:

QQ截图20180514105704.png

我们可以在SBA客户端yml里配置更为详细的信息:

1
2
3
4
5
6
info: 
app:
name: "@project.name@"
description: "@project.description@"
version: "@project.version@"
spring-boot-version: "@project.parent.version@"

然后便可以在SBA服务端看到较为详细的客户端信息: QQ截图20180514111433.png

添加邮件预警

SBA服务端也可以配置邮件预警服务,默认情况下对于被检测的应用启动或者停止的时候会触发预警。

首先添加邮件依赖:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

然后在SBA服务端的yml中配置邮件预警:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

当被检测的应用关停的时候,收到如下邮件:

QQ截图20180514190424.png

附录

SBA Server配置说明

Property nameDescriptionDefault value中文说明
spring.boot.admin.context-pathThe 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.periodTime interval in ms to update the status of applications with expired status-informations.10.000更新应用信息的频率,单位毫秒
spring.boot.admin.monitor.status-lifetimeLifetime of application statuses in ms. The applications /health-endpoint will not be queried until the lifetime has expired.10.000被监控的应用信息的过期时间,单位毫秒

SBA Client配置说明

Property nameDescriptionDefault value中文说明
spring.boot.admin.client.enabledEnables the Spring Boot Admin Client.true默认开启
spring.boot.admin.urlList of URLs of the Spring Boot Admin server to register at. This triggers the AutoConfiguration. Mandatory.admin server 的地址列表,此设置会触发自动配置,必须
spring.boot.admin.api-pathHttp-path of registration endpoint at your admin server.“api/applications”注册到 admin server 端点的 Http-path
spring.boot.admin.username spring.boot.admin.passwordUsername and password for http-basic authentication. If set the registration uses http-basic-authentication when registering at the admin server.注册到 admin server 的账号密码
spring.boot.admin.periodInterval for repeating the registration (in ms).10.000重试注册的间隔时间
spring.boot.admin.auto-registrationIf set to true the periodic task to register the application is automatically scheduled after the application is ready.true应用启动后自动执行周期性的注册任务
spring.boot.admin.auto-deregistrationSwitch to enable auto-deregistration at Spring Boot Admin server when context is closed.false当应用关闭时,自动取消注册
spring.boot.admin.client.health-urlClient-health-url to register with. Can be overridden in case the reachable URL is different (e.g. Docker). Must be unique in registry.Guessed based on management-url and endpoints.health.id.
spring.boot.admin.client.management-urlClient-management-url to register with. Can be overridden in case the reachable url is different (e.g. Docker).Guessed based on service-url, server.servlet-path, management.port and management.context-path.
spring.boot.admin.client.service-urlClient-service-url to register with. Can be overridden in case the reachable url is different (e.g. Docker).Guessed based on hostname, server.port and server.context-path.
spring.boot.admin.client.nameName to register with.${spring.application.name} if set, “spring-boot-application” otherwise.注册时的名字
spring.boot.admin.client.prefer-ipUse the ip-address rather then the hostname in the guessed urls. If server.address / management.address is set, it get used. Otherwise the IP address returned from InetAddress.getLocalHost() gets used.false

邮件配置选项

Property nameDescriptionDefault value中文说明
spring.boot.admin.notify.mail.enabledEnable mail notificationstrue默认启用
spring.boot.admin.notify.mail.ignore-changesComma-delimited list of status changes to be ignored. Format: “:”. Wildcards allowed.“UNKNOWN:UP”需要忽略的状态改变通知,逗号分隔
spring.boot.admin.notify.mail.toComma-delimited list of mail recipients“root@localhost”接收通知的邮箱地址,逗号分隔
spring.boot.admin.notify.mail.ccComma-delimited list of carbon-copy recipients抄送
spring.boot.admin.notify.mail.fromMail sender发送人
spring.boot.admin.notify.mail.subjectMail subject. SpEL-expressions are supported“#{application.name} (#{application.id}) is #{to.status}”主题
spring.boot.admin.notify.mail.textMail body. SpEL-expressions are supported“#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}”内容

参考博文:https://blog.csdn.net/kinginblue/article/details/52132113#t26

源码链接:https://github.com/wuyouzhuguli/Spring-Boot-Demos/tree/master/23.Spring-Boot-Admin

请作者喝瓶肥宅水🥤

0