package com.example.principle.ocp;
public class ApiInfo {
private String api;
private long requestCount;
private long errorCount;
private long durationOfSeconds;
// private long timeoutCount;
public String getApi() {
return api;
}
public void setApi(String api) {
this.api = api;
}
public long getRequestCount() {
return requestCount;
}
public void setRequestCount(long requestCount) {
this.requestCount = requestCount;
}
public long getErrorCount() {
return errorCount;
}
public void setErrorCount(long errorCount) {
this.errorCount = errorCount;
}
public long getDurationOfSeconds() {
return durationOfSeconds;
}
public void setDurationOfSeconds(long durationOfSeconds) {
this.durationOfSeconds = durationOfSeconds;
}
}
package com.example.principle.ocp;
// 存储告警规则
public class AlertRule {
public MatchRule getMatchedRule(String api) {
return null;
}
class MatchRule {
long maxTps;
long maxErrorCount;
//...... Maxtimeout
public long getMaxTps() {
return maxTps;
}
public void setMaxTps(long maxTps) {
this.maxTps = maxTps;
}
public long getMaxErrorCount() {
return maxErrorCount;
}
public void setMaxErrorCount(long maxErrorCount) {
this.maxErrorCount = maxErrorCount;
}
}
}
package com.example.principle.ocp;
// 通知类 支持邮件、短信、微信、手机等多种通知渠道
public class Notification {
// 通知
void notify(NotificationEmergencyLevel level,String content) {
// 略
}
// 表示通知的紧急程度,
//包括 SEVERE(严重)、URGENCY(紧急)、NORMAL(普通)、TRIVIAL(无关紧要),不同的紧急程度对应不同的发送渠道
enum NotificationEmergencyLevel {
SEVERE(0,0),URGENCY(1,1),NORMAL(2,2),TRIVIAL(3,3);
int channel ; //渠道
int level; // 等级
NotificationEmergencyLevel(int channel, int level) {
this.channel = channel;
this.level = level;
}
public int getChannel() {
return channel;
}
public void setChannel(int channel) {
this.channel = channel;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
}
package com.example.principle.ocp;
public abstract class AlertHandler {
protected AlertRule rule;
protected Notification notification;
public AlertHandler(AlertRule rule, Notification notification) {
this.rule = rule;
this.notification = notification;
}
public abstract void adaptiveAlert(ApiInfo apiInfo);
}
class TpsAlertHandler extends AlertHandler {
public TpsAlertHandler(AlertRule rule, Notification notification) {
super(rule, notification);
}
@Override
public void adaptiveAlert(ApiInfo apiInfo) {
long tps = apiInfo.getRequestCount() / apiInfo.getDurationOfSeconds();
if (tps > rule.getMatchedRule(apiInfo.getApi()).getMaxTps()) {
notification.notify(Notification.NotificationEmergencyLevel.URGENCY,".........");
}
}
}
class ErrorCountAlertHandler extends AlertHandler{
public ErrorCountAlertHandler(AlertRule rule, Notification notification) {
super(rule, notification);
}
@Override
public void adaptiveAlert(ApiInfo apiInfo) {
if (apiInfo.getErrorCount() > rule.getMatchedRule(apiInfo.getApi()).getMaxErrorCount()) {
notification.notify(Notification.NotificationEmergencyLevel.URGENCY,"........");
}
}
}
class TimeoutAlertHandler extends AlertHandler {
public TimeoutAlertHandler(AlertRule rule, Notification notification) {
super(rule, notification);
}
@Override
public void adaptiveAlert(ApiInfo apiInfo) {
}
}