最近做了一个监测k8s服务pod水平伸缩发送邮件的功能(当pod的cpu/内存达到指定阈值后会水平扩展出多个pod、或者指定时间内pod数应扩展到指定数量),一开始写了个格式很low的邮件,像下面这样:
主流程打通,算个v1版本吧,程序员是个追求完美的人,再说这么低逼格的邮件,给客户看,客户也会不满意。那怎么提高邮件的逼格呢?下面写了个简单的demo,v2版本如下:
感兴趣的小伙伴可以参考,模板可以找你公司前端和美工小姐姐设计。
因为监测k8s服务pod水平伸缩是用go开发的,发送通知邮件提供了个接口,用springboot写的,以下也用springboot做demo
Springboot的pom.xml文件:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.2.RELEASE
com.example
email-demo
0.0.1-SNAPSHOT
email-demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.apache.commons
commons-lang3
3.8.1
com.alibaba
fastjson
1.2.47
org.springframework.boot
spring-boot-starter-mail
org.springframework.boot
spring-boot-maven-plugin
pod-scale-alarm.html模板文件:
模板中的{0}、{1}这样的占位符后面java代码会替换掉
Confidential - Scale Alarm Use only
success-alarm.png图标:
java代码如下,简单的demo,优化可以自己在项目中去做。
package com.example.emaildemo;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Properties;
public class SendEmailUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(SendEmailUtil.class);
public static void main(String[] args) throws MessagingException, IOException {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setUsername("你的邮箱地址");
javaMailSender.setPassword("你的邮箱AUTH密码,不是登陆密码哦,在邮箱的设置里单独开启和设置");
javaMailSender.setHost("smtp.exmail.qq.com");
javaMailSender.setPort(587);
javaMailSender.setDefaultEncoding("UTF-8");
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.connectiontimeout", "20000");
props.setProperty("mail.smtp.timeout", "20000");
javaMailSender.setJavaMailProperties(props);
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
helper.setTo(new String[]{"收件人邮箱"});
helper.setCc("抄送人邮箱");
helper.setFrom("你的邮箱地址");
helper.setSubject("liang subject");
helper.setText(buildContent(), true);
String alarmIconName = "success-alarm.png";
ClassPathResource img = new ClassPathResource(alarmIconName);
if (Objects.nonNull(img)) {
helper.addInline("icon-alarm", img);
}
javaMailSender.send(message);
}
private static String buildContent() throws IOException {
//加载邮件html模板
String fileName = "pod-scale-alarm.html";
InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName);
BufferedReader fileReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer buffer = new StringBuffer();
String line = "";
try {
while ((line = fileReader.readLine()) != null) {
buffer.append(line);
}
} catch (Exception e) {
LOGGER.error("读取文件失败,fileName:{}", fileName, e);
} finally {
inputStream.close();
fileReader.close();
}
String contentText = "以下是服务实例伸缩信息, 敬请查看.
below is the information of service instance scale, please check. ";
//邮件表格header
String header = "分区(Namespace) 服务(Service) 伸缩结果(Scale Result) 伸缩原因(Scale Reason) 当前实例数(Pod instance number) ";
StringBuilder linesBuffer = new StringBuilder();
linesBuffer.append("" + "myNamespace" + " " + "myServiceName" + " " + "myscaleResult" + " " +
"" + "mReason" + " " + "my4" + " ");
//绿色
String emailHeadColor = "#10fa81";
String date = DateFormatUtils.format(new Date(), "yyyy/MM/dd HH:mm:ss");
//填充html模板中的五个参数
String htmlText = MessageFormat.format(buffer.toString(), emailHeadColor, contentText, date, header, linesBuffer.toString());
//改变表格样式
htmlText = htmlText.replaceAll("", " ");
htmlText = htmlText.replaceAll(" ", " ");
return htmlText;
}
}
Java相关栏目本月热门文章
- 1【Linux驱动开发】设备树详解(二)设备树语法详解
- 2别跟客户扯细节
- 3Springboot+RabbitMQ+ACK机制(生产方确认(全局、局部)、消费方确认)、知识盲区
- 4【Java】对象处理流(ObjectOutputStream和ObjectInputStream)
- 5【分页】常见两种SpringBoot项目中分页技巧
- 6一文带你搞懂OAuth2.0
- 7我要写整个中文互联网界最牛逼的JVM系列教程 | 「JVM与Java体系架构」章节:虚拟机与Java虚拟机介绍
- 8【Spring Cloud】新闻头条微服务项目:FreeMarker模板引擎实现文章静态页面生成
- 9JavaSE - 封装、static成员和内部类
- 10树莓派mjpg-streamer实现监控及拍照功能调试
- 11用c++写一个蓝屏代码
- 12从JDK8源码中看ArrayList和LinkedList的区别
- 13idea 1、报错java: 找不到符号 符号: 变量 log 2、转换成Maven项目
- 14在openwrt使用C语言增加ubus接口(包含C uci操作)
- 15Spring 解决循环依赖
- 16SpringMVC——基于MVC架构的Spring框架
- 17Andy‘s First Dictionary C++ STL set应用
- 18动态内存管理
- 19我的创作纪念日
- 20Docker自定义镜像-Dockerfile
热门相关搜索
路由器设置
木托盘
宝塔面板
儿童python教程
心情低落
朋友圈
vim
双一流学科
专升本
我的学校
日记学校
西点培训学校
汽修学校
情书
化妆学校
塔沟武校
异形模板
西南大学排名
最精辟人生短句
6步教你追回被骗的钱
南昌大学排名
清朝十二帝
北京印刷学院排名
北方工业大学排名
北京航空航天大学排名
首都经济贸易大学排名
中国传媒大学排名
首都师范大学排名
中国地质大学(北京)排名
北京信息科技大学排名
中央民族大学排名
北京舞蹈学院排名
北京电影学院排名
中国戏曲学院排名
河北政法职业学院排名
河北经贸大学排名
天津中德应用技术大学排名
天津医学高等专科学校排名
天津美术学院排名
天津音乐学院排名
天津工业大学排名
北京工业大学耿丹学院排名
北京警察学院排名
天津科技大学排名
北京邮电大学(宏福校区)排名
北京网络职业学院排名
北京大学医学部排名
河北科技大学排名
河北地质大学排名
河北体育学院排名



