SpringBoot可视化监控
可以直接利用 Spring Boot Admin 实现可视化监控,此时至少需要两个项目实例,一个是监控的管理端,一个是被监控的客户端。
1 构建监控管理端项目
2 引入管理端项目依赖
监控管理端需要使用网页展示监控信息,所以引入 Web 依赖,另外添加 Spring Boot Admin 管理端依赖项。
实例:
<!-- Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Admin 管理端依赖项 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.0</version>
</dependency>
3 开启监控管理端
在启动类上添加 @EnableAdminServer 注解开启 Spring Boot Admin 监控管理功能,代码如下:
实例:
@SpringBootApplication
@EnableAdminServer // 开启监控管理
public class SpringBootMonitorManagerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMonitorManagerApplication.class, args);
}
}
然后运行启动类,访问 http://127.0.0.1:8080
会发现界面上已经显示监控信息了。

如果运行报错,就吧工程改为2.2.4版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Admin Client注册
引入client依赖包
Spring Boot Admin是使用actuator实现的服务监控,所以在client应用中需要引入actuator的包,并开放相关的接口,否则监控的信息不完整。
pom.xml文件添加如下依赖
<!-- actuator-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
修改配置文件
spring:
application:
name: filemanager
profiles:
active: dev
boot:
admin:
client:
url: http://127.0.0.1:1234 # 指定admin-server注册地址,和Eureka更像了
instance:
name: 文件服务
server:
port: 8881
tomcat:
uri-encoding: UTF-8
max-threads: 1000
max-connections: 20000
management:
endpoints:
web:
exposure:
include: "*" #暴露actuator所有接口
至此就完成了Admin Client端的配置,可以启动服务了,查看admin页面


集成邮件通知 Spring boot admin监控邮件发送
pom添加邮件依赖
<!-- 邮件依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
yml配置文件增加如下:
server:
port: 1234
spring:
application:
name: service-admin
mail:
host: smtp.163.com
username: xxx@163.com
password: xxxxx
properties:
mail.debug: false
mail.smtp.auth: true
boot:
admin:
notify:
mail:
to: xxxx@qq.com
from: xxxx@163.com
# ignore-changes: UNKNOWN:UP
routes:
endpoints: "/*"
网友评论