Commit 01443501 authored by zhouzihao's avatar zhouzihao

dev-添加邮件处理器(需要被测试)

parent b4386d8d
......@@ -63,6 +63,11 @@
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- 邮件相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
......
......@@ -2,7 +2,9 @@ package com.vandyo.sentry.core;
import cn.hutool.json.JSONUtil;
import com.vandyo.sentry.core.cases.*;
import com.vandyo.sentry.core.collectionMachine.CollectionDealer;
import com.vandyo.sentry.core.collectionMachine.CollectionMachine;
import com.vandyo.sentry.core.collectionMachine.EmailDealer;
import com.vandyo.sentry.core.config.MapConfig;
import com.vandyo.sentry.core.dto.ErrStatus;
import org.slf4j.Logger;
......@@ -12,6 +14,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
......@@ -30,9 +34,10 @@ public class Tmp {
@Autowired
private MapConfig mapConfig;
@Autowired
private EmailDealer emailDealer;
@Scheduled(fixedRate = 1000)
@Scheduled(fixedRate = 5000)
public void test() {
log.info("we run it {} times", ++times);
Case case1 = null;
......@@ -49,13 +54,16 @@ public class Tmp {
log.info("over!");
}
@Scheduled(fixedRate = 5000)
@Scheduled(fixedRate = 1000*60)
public void check() {
Map<String, Map<ErrStatus, Long>> data = CollectionMachine.checkRule(5);
String info = JSONUtil.parseFromMap(data).toStringPretty();
// log.warn(info);
// 查看是否 符合规则 Using timeMap
Map<String,Integer> timeMap = mapConfig.getTimeMap();
log.info("start to check rule!");
List<CollectionDealer> list = new ArrayList<>(1);
list.add(emailDealer);
// FIXME: 添加其他处理
CollectionMachine.checkTimes(timeMap,data,list);
}
}
......@@ -26,6 +26,7 @@ public class EmptyCase extends Case {
res.setSuccess(true);
// 这里的请求结果进行跳过
res.setIgnore(true);
res.setKey("test");
CollectionMachine.post(res);
return res;
}
......
package com.vandyo.sentry.core.collectionMachine;
import com.vandyo.sentry.core.dto.ErrStatus;
import java.util.Map;
/**
* 收集数据处理器
*/
public interface CollectionDealer {
/**
* 错误面板处理函数
* @param data 错误面板
*/
void deal(Map<String, Map<ErrStatus, Long>> data);
/**
* 错误面板格式化
* @param data
* @return
*/
String formatData(Map<String, Map<ErrStatus, Long>> data);
}
......@@ -20,7 +20,7 @@ public class CollectionMachine {
public static void post(Res<Map<String, String>> log) {
// 应该对于不同的请求统一管理 还是 每个单独管理呢?
logger.info("temp long is {}",temp.size());
logger.info("temp long is {}", temp.size());
if (log.getIgnore() || log.getSuccess()) {
//pass
} else {
......@@ -83,7 +83,7 @@ public class CollectionMachine {
while (iterator.hasNext()) {
Long key = iterator.next();
if (key < System.currentTimeMillis() - 1000 * 60 * min) {
logger.error("remove key {}",key);
logger.error("remove key {}", key);
iterator.remove();
temp.remove(key);
} else {
......@@ -91,4 +91,33 @@ public class CollectionMachine {
}
}
}
/**
* 检查处理
*
* @param timeMap 时间映射
* @param data 一分钟内错误数据
* @param dealers 处理器
*/
public static void checkTimes(Map<String, Integer> timeMap, Map<String, Map<ErrStatus, Long>> data, List<CollectionDealer> dealers) {
for(Map.Entry<String,Map<ErrStatus, Long>> entry:data.entrySet()){
String key = entry.getKey();
if (!timeMap.containsKey(key)){
continue;
}
Map<ErrStatus, Long> value = entry.getValue();
int sum = 0;
for(Map.Entry<ErrStatus,Long> i:value.entrySet()){
sum += i.getValue().intValue();
}
if (sum >= timeMap.get(key)){
//打印 阈值 进行处理
for (CollectionDealer dealer:dealers){
dealer.deal(data);
}
break;
}
}
}
}
package com.vandyo.sentry.core.collectionMachine;
import com.vandyo.sentry.core.dto.ErrStatus;
import java.util.Map;
public abstract class DataClearDealer implements CollectionDealer{
@Override
public String formatData(Map<String, Map<ErrStatus, Long>> data) {
StringBuilder builder = new StringBuilder();
builder.append("接口报错:\n");
for(Map.Entry<String,Map<ErrStatus, Long>> entry:data.entrySet()){
builder.append(entry.getKey()+" :\n");
Map<ErrStatus, Long> value = entry.getValue();
for(Map.Entry<ErrStatus,Long> i:value.entrySet()){
builder.append("\t"+i.getKey().getDes()+":"+i.getValue()+"次。\n");
}
}
return builder.toString();
}
}
package com.vandyo.sentry.core.collectionMachine;
import com.vandyo.sentry.core.dto.ErrStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class EmailDealer extends DataClearDealer {
@Autowired
private JavaMailSender javaMailSender;
@Override
public void deal(Map<String, Map<ErrStatus, Long>> data) {
String content = formatData(data);
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("zhouzihao@vandyo.com");
message.setTo("1042181618@qq.com");
message.setSubject("服务报错");
message.setText(content);
javaMailSender.send(message);
}
}
......@@ -4,21 +4,31 @@ public enum ErrStatus {
/**
* 500错误
*/
Err5xx,
Err5xx("5xx 错误"),
/**
* 400错误
*/
Err4xx,
Err4xx("4xx 错误"),
/**
* 超时错误 408
*/
ErrTimeout,
ErrTimeout("请求超时"),
/**
* 没有预期的返回值
*/
ErrUnexpected,
ErrUnexpected("没有预期返回值"),
/**
* 未知错误
*/
ErrUnknown;
ErrUnknown("未知异常");
private String des;
ErrStatus(String des) {
this.des = des;
}
public String getDes() {
return des;
}
}
......@@ -7,5 +7,20 @@ vandyo:
checkmeta:
desMap:
"[/car/my_car/info]": 车辆信息
test: 测试接口
timeMap:
"[/car/my_car/info]": 6
test: 2
spring:
mail:
host: imap.exmail.qq.com
default-encoding: UTF-8
username: zhouzihao
password: zzhcool123
port: 993
properties:
mail:
smtp:
ssl:
enable: true
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment