feat(sj_1.0.0):
1、客户端网络异常告警提交
This commit is contained in:
parent
e4886b170d
commit
44da029125
@ -0,0 +1,38 @@
|
||||
package com.aizuda.snailjob.client.common.exception;
|
||||
|
||||
import com.aizuda.snailjob.common.core.exception.BaseSnailJobException;
|
||||
|
||||
/**
|
||||
* @author: opensnail
|
||||
* @date : 2022-03-03 14:49
|
||||
*/
|
||||
public class SnailJobRemoteException extends BaseSnailJobException {
|
||||
|
||||
public SnailJobRemoteException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public SnailJobRemoteException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public SnailJobRemoteException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public SnailJobRemoteException(String message, Object... arguments) {
|
||||
super(message, arguments);
|
||||
}
|
||||
|
||||
public SnailJobRemoteException(String message, Object[] arguments, Throwable cause) {
|
||||
super(message, arguments, cause);
|
||||
}
|
||||
|
||||
public SnailJobRemoteException(String message, Object argument, Throwable cause) {
|
||||
super(message, argument, cause);
|
||||
}
|
||||
|
||||
public SnailJobRemoteException(String message, Object argument) {
|
||||
super(message, argument);
|
||||
}
|
||||
}
|
@ -4,19 +4,15 @@ import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.aizuda.snailjob.client.common.cache.GroupVersionCache;
|
||||
import com.aizuda.snailjob.client.common.config.SnailJobProperties;
|
||||
import com.aizuda.snailjob.client.common.exception.SnailJobRemoteException;
|
||||
import com.aizuda.snailjob.common.core.constant.SystemConstants;
|
||||
import com.aizuda.snailjob.common.core.context.SpringContext;
|
||||
import com.aizuda.snailjob.common.core.enums.HeadersEnum;
|
||||
import com.aizuda.snailjob.common.log.SnailJobLog;
|
||||
import com.aizuda.snailjob.common.core.util.NetUtil;
|
||||
import com.aizuda.snailjob.common.log.SnailJobLog;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.handler.codec.http.DefaultFullHttpRequest;
|
||||
import io.netty.handler.codec.http.FullHttpRequest;
|
||||
import io.netty.handler.codec.http.HttpHeaderNames;
|
||||
import io.netty.handler.codec.http.HttpHeaderValues;
|
||||
import io.netty.handler.codec.http.HttpMethod;
|
||||
import io.netty.handler.codec.http.HttpVersion;
|
||||
import io.netty.handler.codec.http.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
|
||||
@ -179,12 +175,16 @@ public class NettyChannel {
|
||||
.set(HeadersEnum.VERSION.getKey(), GroupVersionCache.getVersion())
|
||||
.set(HeadersEnum.HOST.getKey(), serverConfig.getHost())
|
||||
.set(HeadersEnum.NAMESPACE.getKey(), Optional.ofNullable(snailJobProperties.getNamespace()).orElse(
|
||||
SystemConstants.DEFAULT_NAMESPACE))
|
||||
SystemConstants.DEFAULT_NAMESPACE))
|
||||
.set(HeadersEnum.TOKEN.getKey(), Optional.ofNullable(snailJobProperties.getToken()).orElse(
|
||||
SystemConstants.DEFAULT_TOKEN))
|
||||
;
|
||||
|
||||
//发送数据
|
||||
CHANNEL.writeAndFlush(request).sync();
|
||||
try {
|
||||
CHANNEL.writeAndFlush(request).sync();
|
||||
} catch (Exception exception) {
|
||||
throw new SnailJobRemoteException("网络异常");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -34,24 +34,21 @@ public class WebhookAlarm extends AbstractAlarm<AlarmContext> {
|
||||
public boolean syncSendMessage(AlarmContext alarmContext) {
|
||||
|
||||
WebhookAttribute webhookAttribute = JsonUtil.parseObject(alarmContext.getNotifyAttribute(), WebhookAttribute.class);
|
||||
for (int i = 0; i < 2; i++) {
|
||||
try {
|
||||
WebhookMessage webhookMessage = WebhookMessage.builder().text(alarmContext.getTitle()).build();
|
||||
try {
|
||||
WebhookMessage webhookMessage = WebhookMessage.builder().text(alarmContext.getTitle()).build();
|
||||
|
||||
HttpRequest post = HttpUtil.createPost(webhookAttribute.getWebhookUrl());
|
||||
HttpRequest request = post.body(JsonUtil.toJsonString(webhookMessage), ContentTypeEnum.valueOf(webhookAttribute.getContentType()).getMediaType().toString())
|
||||
.header(SystemConstants.SECRET, webhookAttribute.getSecret());
|
||||
HttpResponse execute = request.execute();
|
||||
if (execute.isOk()) {
|
||||
return true;
|
||||
}
|
||||
SnailJobLog.LOCAL.info("发送Webhook告警结果. webHook:[{}],结果: [{}]", webhookAttribute.getWebhookUrl(), execute.body());
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
SnailJobLog.LOCAL.error("发送Webhook告警异常. webHook:[{}]", webhookAttribute, e);
|
||||
HttpRequest post = HttpUtil.createPost(webhookAttribute.getWebhookUrl());
|
||||
HttpRequest request = post.body(JsonUtil.toJsonString(webhookMessage), ContentTypeEnum.valueOf(webhookAttribute.getContentType()).getMediaType().toString())
|
||||
.header(SystemConstants.SECRET, webhookAttribute.getSecret());
|
||||
HttpResponse execute = request.execute();
|
||||
SnailJobLog.LOCAL.info("发送Webhook告警结果. webHook:[{}],结果: [{}]", webhookAttribute.getWebhookUrl(), execute.body());
|
||||
if (execute.isOk()) {
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
SnailJobLog.LOCAL.error("发送Webhook告警异常. webHook:[{}]", webhookAttribute, e);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -71,4 +68,3 @@ public class WebhookAlarm extends AbstractAlarm<AlarmContext> {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user