当前位置: 首页 > news >正文

36. Spring Boot 2.1.3.RELEASE 中实现监控信息可视化并添加邮件报警功能

1. 创建 Spring Boot Admin Server 项目

1.1 添加依赖

pom.xml 中添加 Spring Boot Admin Server 和邮件相关依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Spring Boot Admin Server -->
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-server</artifactId>
        <version>2.1.6</version>
    </dependency>
    <!-- Spring Boot Starter Mail -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
</dependencies>

这里选用 Spring Boot Admin 2.1.6 版本,与 Spring Boot 2.1.3.RELEASE 相对兼容,同时引入邮件启动器用于邮件报警。

1.2 启用 Admin Server

在主应用类添加 @EnableAdminServer 注解:

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAdminServer
public class AdminServerEmailApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminServerEmailApplication.class, args);
    }
}

2. 配置邮件报警

2.1 配置邮件服务

application.properties 中配置邮件服务器信息:

server.port=8080

# QQ 邮箱 SMTP 服务器地址
spring.mail.host=smtp.qq.com
# QQ 邮箱 SMTP 服务器端口,开启 SSL 时使用 465 或 587
spring.mail.port=587
# 发件人 QQ 邮箱地址
spring.mail.username=your_qq_email@qq.com
# 发件人 QQ 邮箱授权码
spring.mail.password=your_qq_email_authorization_code
# 邮件协议
spring.mail.protocol=smtp
# 开启 STARTTLS 支持
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# 开启 SMTP 认证
spring.mail.properties.mail.smtp.auth=true

若使用其他邮件服务提供商,需相应修改 hostport 等信息。

2.2 配置 Spring Boot Admin 邮件报警

继续在 application.properties 中添加 Spring Boot Admin 邮件报警配置:

# 启用邮件通知
spring.boot.admin.notify.mail.enabled=true
# 收件人邮箱地址,可以是多个,用逗号分隔
spring.boot.admin.notify.mail.to=recipient_email@example.com
# 发件人邮箱地址,与上面配置的 username 一致
spring.boot.admin.notify.mail.from=your_qq_email@qq.com

3. 将被监控的 Spring Boot 应用注册到 Admin Server

3.1 添加依赖

在被监控的 Spring Boot 应用的 pom.xml 中添加 Spring Boot Admin Client 依赖:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.1.6</version>
</dependency>
3.2 配置客户端

在被监控应用的 application.properties 中添加以下配置:

server.port=8081
# Admin Server 的地址
spring.boot.admin.client.url=http://localhost:8080
# 暴露所有 Actuator 端点
management.endpoints.web.exposure.include=*

4. 启动应用并验证

4.1 启动 Spring Boot Admin Server

运行 AdminServerEmailApplication 类启动 Spring Boot Admin Server。

4.2 启动被监控的 Spring Boot 应用

启动被监控的应用,它会自动注册到 Spring Boot Admin Server。

4.3 验证邮件报警

当被监控应用的状态发生变化(如上线、下线)时,Spring Boot Admin Server 会根据配置发送邮件到指定的收件人邮箱,你可以通过停止和启动被监控应用来验证邮件报警功能是否正常工作。
在这里插入图片描述

在这里插入图片描述

注意事项

  • 邮件服务安全:使用 Gmail 等邮箱服务时,可能需要开启“允许不太安全的应用访问”或使用应用专用密码,以确保能正常发送邮件。
  • 版本兼容性:确保 Spring Boot、Spring Boot Admin 及其他依赖库的版本相互兼容,避免出现版本冲突问题。

相关文章:

  • 信息系统的安全防护
  • 神经网络 - 激活函数(Sigmoid 型函数)
  • 剑指 Offer II 032. 有效的变位词
  • flask 是如何分发请求的?
  • 机试准备第三天
  • 关于CanvasRenderer.SyncTransform触发调用的机制
  • 04 路由表的IP分组传输过程
  • 【deepseek解决不了的问题】vue2响应式数据在视图改变后被无感置空
  • 【Python LeetCode】面试经典 150 题
  • (九)axios的使用
  • .net websocket 使用
  • 机器学习3-聚类
  • 前端多个项目放在一个域名下如何配置打包文件
  • 【软件安装】CentOS7系统安装Python3和opencv-python库
  • C++ Qt常见面试题(3):Qt内存管理机制
  • 【爬虫基础】第二部分 爬虫基础理论 P1/3
  • 【MySQL篇】数据库基础
  • 国自然面上项目|基于肺癌精准靶向治疗的基因影像组学研究|基金申请·25-02-26
  • mongodb【实用教程】
  • Windows docker下载minio出现“Using default tag: latestError response from daemon”
  • 秦洪看盘|浮现新预期,存量资金积极调仓
  • 跨海论汉|专访白馥兰:对中国农业史的兴趣,从翻译《齐民要术》开始
  • 新任海南琼海市委副书记陈明已主持市政府党组全面工作
  • 新任浙江省委常委、组织部长杨荫凯到嘉兴南湖瞻仰红船
  • 大漠孤烟为何“直”?物理学家在唐诗中读出“不一样的美”
  • VR数字沉浸体验又添新节目,泰坦尼克号驶进文旅元宇宙