介绍
模拟服务器故障,然后实现邮件告警
一、首先配置邮件的maven依赖
代码如下:
<!--邮件告警--><!-- Spring Boot的邮件发送依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><dependency><groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId><version>1.6.2</version></dependency>
二、其次在ideal中创建相关类文件
2.1、创建一个服务类MonitoringService
使用Spring的JavaMailSender来发送邮件。
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;/*** Created by 姜水桦 on 2024/3/21 16:58* 功能描述:* 创建一个服务类,使用Spring的JavaMailSender来发送邮件。*/
@Service
public class MonitoringService {private final JavaMailSender mailSender;public MonitoringService(JavaMailSender mailSender) {this.mailSender = mailSender;}public void sendAlertEmail(String from,String to, String subject, String text) {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(from);message.setTo(to);message.setSubject(subject);message.setText(text);mailSender.send(message);}
}
2.2、 定义一个监控类
在你的监控组件中,当检测到异常时,调用EmailService的sendAlertEmail方法来发送邮件。
package com.example.service;import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;/*** Created by 姜水桦 on 2024/3/21 16:58* 功能描述:* 创建一个服务类,使用Spring的JavaMailSender来发送邮件。*/
@Service
public class MonitoringService {private final JavaMailSender mailSender;public MonitoringService(JavaMailSender mailSender) {this.mailSender = mailSender;}public void sendAlertEmail(String from,String to, String subject, String text) {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(from);message.setTo(to);message.setSubject(subject);message.setText(text);mailSender.send(message);}
}
2.3、再创建一个控制器类(逻辑层)
EmailController
package com.example.controller;import com.example.common.AuthAccess;
import com.example.utils.ServiceMonitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** Created by 姜水桦 on 2024/3/21 19:53* 功能描述:*/
@RestController
public class EmailController {@Autowiredprivate ServiceMonitor serviceMonitor;// 每隔30秒执行一次checkServiceStatus方法
// @Scheduled(fixedRate = 30000)
// @Scheduled(fixedRate = 1000)@GetMapping("/mail")@AuthAccesspublic void monitorService() {serviceMonitor.checkServiceStatus();}
}
2.4、 配置application.yml
spring:#邮箱配置mail:host: smtp.qq.comport: 465# 使用自己的发送方用户名 + 授权码填充username: jiangshuihua1018@qq.compassword: vyqsovsepomlecdddefault-encoding: UTF-8properties:mail:smtp:ssl:enable: falserequired: falsesocketFactory:class: javax.net.ssl.SSLSocketFactoryallow8bitmime: truedebug: true
注意:这里的密码指的是你QQ账号里面的授权码
关于如何获取授权码链接地址如下:
如何开启QQ邮箱的SMTP服务和设置授权码 - 知乎 (zhihu.com)
2.5、最后启动springboot服务器即可
三、在postman中测试用例
服务器启动后,在postman中测试接口用例 ,因为上面随机数生成在0~1之间,所以if()中的值始终为true,会继续往下执行,实现邮件告警通知。
四、结果展示
看到如下界面即表示邮件告警成功
查看发送到网易的邮箱内容