feat: webhook alarm service use post to deliver alarm info #79

This commit is contained in:
KFCFans 2020-11-14 11:29:44 +08:00
parent 528920379d
commit 2ea53b534c
4 changed files with 25 additions and 16 deletions

View File

@ -2,6 +2,7 @@ package com.github.kfcfans.powerjob.client;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.github.kfcfans.powerjob.common.InstanceStatus; import com.github.kfcfans.powerjob.common.InstanceStatus;
import com.github.kfcfans.powerjob.common.OmsConstant;
import com.github.kfcfans.powerjob.common.OpenAPIConstant; import com.github.kfcfans.powerjob.common.OpenAPIConstant;
import com.github.kfcfans.powerjob.common.PowerJobException; import com.github.kfcfans.powerjob.common.PowerJobException;
import com.github.kfcfans.powerjob.common.request.http.SaveJobInfoRequest; import com.github.kfcfans.powerjob.common.request.http.SaveJobInfoRequest;
@ -283,7 +284,7 @@ public class OhMyClient {
*/ */
public ResultDTO<Long> saveWorkflow(SaveWorkflowRequest request) throws PowerJobException { public ResultDTO<Long> saveWorkflow(SaveWorkflowRequest request) throws PowerJobException {
request.setAppId(appId); request.setAppId(appId);
MediaType jsonType = MediaType.parse("application/json; charset=utf-8"); MediaType jsonType = MediaType.parse(OmsConstant.JSON_MEDIA_TYPE);
// 中坑记录 FastJSON 序列化会导致 Server 接收时 pEWorkflowDAG null无语.jpg // 中坑记录 FastJSON 序列化会导致 Server 接收时 pEWorkflowDAG null无语.jpg
String json = JsonUtils.toJSONStringUnsafe(request); String json = JsonUtils.toJSONStringUnsafe(request);
String post = postHA(OpenAPIConstant.SAVE_WORKFLOW, RequestBody.create(jsonType, json)); String post = postHA(OpenAPIConstant.SAVE_WORKFLOW, RequestBody.create(jsonType, json));

View File

@ -15,4 +15,6 @@ public class OmsConstant {
public static final String COMMA = ","; public static final String COMMA = ",";
public static final String LINE_SEPARATOR = "\r\n"; public static final String LINE_SEPARATOR = "\r\n";
public static final String JSON_MEDIA_TYPE = "application/json; charset=utf-8";
} }

View File

@ -3,7 +3,6 @@ package com.github.kfcfans.powerjob.common.utils;
import okhttp3.*; import okhttp3.*;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -29,6 +28,18 @@ public class HttpUtils {
.get() .get()
.url(url) .url(url)
.build(); .build();
return execute(request);
}
public static String post(String url, RequestBody requestBody) throws IOException {
Request request = new Request.Builder()
.post(requestBody)
.url(url)
.build();
return execute(request);
}
private static String execute(Request request) throws IOException {
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
if (response.code() == HTTP_SUCCESS_CODE) { if (response.code() == HTTP_SUCCESS_CODE) {
ResponseBody body = response.body(); ResponseBody body = response.body();
@ -42,17 +53,4 @@ public class HttpUtils {
return null; return null;
} }
public static String post(String url, RequestBody requestBody) throws IOException {
Request request = new Request.Builder()
.post(requestBody)
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
if (response.code() == HTTP_SUCCESS_CODE) {
return Objects.requireNonNull(response.body()).string();
}
}
return null;
}
} }

View File

@ -1,10 +1,14 @@
package com.github.kfcfans.powerjob.server.service.alarm.impl; package com.github.kfcfans.powerjob.server.service.alarm.impl;
import com.alibaba.fastjson.JSONObject;
import com.github.kfcfans.powerjob.common.OmsConstant;
import com.github.kfcfans.powerjob.common.utils.HttpUtils; import com.github.kfcfans.powerjob.common.utils.HttpUtils;
import com.github.kfcfans.powerjob.server.persistence.core.model.UserInfoDO; import com.github.kfcfans.powerjob.server.persistence.core.model.UserInfoDO;
import com.github.kfcfans.powerjob.server.service.alarm.Alarm; import com.github.kfcfans.powerjob.server.service.alarm.Alarm;
import com.github.kfcfans.powerjob.server.service.alarm.Alarmable; import com.github.kfcfans.powerjob.server.service.alarm.Alarmable;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -31,8 +35,12 @@ public class WebHookAlarmService implements Alarmable {
if (StringUtils.isEmpty(webHook)) { if (StringUtils.isEmpty(webHook)) {
return; return;
} }
MediaType jsonType = MediaType.parse(OmsConstant.JSON_MEDIA_TYPE);
RequestBody requestBody = RequestBody.create(jsonType, JSONObject.toJSONString(alarm));
try { try {
String response = HttpUtils.get(webHook); String response = HttpUtils.post(webHook, requestBody);
log.info("[WebHookAlarmService] invoke webhook[url={}] successfully, response is {}", webHook, response); log.info("[WebHookAlarmService] invoke webhook[url={}] successfully, response is {}", webHook, response);
}catch (Exception e) { }catch (Exception e) {
log.warn("[WebHookAlarmService] invoke webhook[url={}] failed!", webHook, e); log.warn("[WebHookAlarmService] invoke webhook[url={}] failed!", webHook, e);