一、配置过程
- 登录自己的cat服务端 默认登录用户名和密码都是 catadmin添加一个监控规则:
如下面的监控规则就是说,如果在一分钟以内访问mycat这个项目的任意链接超过20次就会向指定告警人发送邮件:
2. 邮件发送的controller写法如下:
package com.fu.cat.demo.controller;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;
@RestController
@RequestMapping("mycat")
public class emailController {
@PostMapping(value = "/sendEmail")
public void sendEmail(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, String[]> parameterMap = request.getParameterMap();
for (Object o:parameterMap.keySet() ) {
System.out.println(o+":" +parameterMap.get(o)[0].toString());
}
try {
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.qq.com");
email.setCharset("utf-8");
email.addTo(parameterMap.get("to")[0]);
email.setFrom("换成自己的邮箱");
email.setAuthentication("换成自己的邮箱","换成自己的邮箱的授权码");
email.setSubject(parameterMap.get("value")[0].substring(0,16));
email.setMsg(parameterMap.get("value")[0]);
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
System.out.println("哈哈哈哈一次告警结束啦");
System.out.println("=====================================================================");
response.setStatus(200);
PrintWriter pw = response.getWriter();
pw.write("200");
pw.flush();
pw.close();
}
}
需要导入的邮件发送maven依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5</version>
</dependency>
- 多次访问mycat这个项目具体是否发送邮件由你制定的告警策略决定:如果触发规则则指定的告警人会收到如下告警信息:
到此一个项目就集成完毕!
二、相关的参考文档
- CAT项目的开源地址: https://github.com/dianping/cat
- CAT官方站点:http://unidal.org/cat/r
- 微盟的CAT接入文档:http://tx.cat.weimob.com/cat/doc.html
- 参考文档:http://fanlychie.github.io/post/cat-setup.html