From 2ea53b534c23b3ee245cb521cf37f4f7edd6f72c Mon Sep 17 00:00:00 2001 From: KFCFans Date: Sat, 14 Nov 2020 11:29:44 +0800 Subject: [PATCH] feat: webhook alarm service use post to deliver alarm info #79 --- .../kfcfans/powerjob/client/OhMyClient.java | 3 ++- .../kfcfans/powerjob/common/OmsConstant.java | 2 ++ .../powerjob/common/utils/HttpUtils.java | 26 +++++++++---------- .../alarm/impl/WebHookAlarmService.java | 10 ++++++- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/powerjob-client/src/main/java/com/github/kfcfans/powerjob/client/OhMyClient.java b/powerjob-client/src/main/java/com/github/kfcfans/powerjob/client/OhMyClient.java index 04efad72..29609430 100644 --- a/powerjob-client/src/main/java/com/github/kfcfans/powerjob/client/OhMyClient.java +++ b/powerjob-client/src/main/java/com/github/kfcfans/powerjob/client/OhMyClient.java @@ -2,6 +2,7 @@ package com.github.kfcfans.powerjob.client; import com.alibaba.fastjson.JSONObject; 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.PowerJobException; import com.github.kfcfans.powerjob.common.request.http.SaveJobInfoRequest; @@ -283,7 +284,7 @@ public class OhMyClient { */ public ResultDTO saveWorkflow(SaveWorkflowRequest request) throws PowerJobException { 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 String json = JsonUtils.toJSONStringUnsafe(request); String post = postHA(OpenAPIConstant.SAVE_WORKFLOW, RequestBody.create(jsonType, json)); diff --git a/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/OmsConstant.java b/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/OmsConstant.java index ed1fd621..3cb99e5e 100644 --- a/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/OmsConstant.java +++ b/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/OmsConstant.java @@ -15,4 +15,6 @@ public class OmsConstant { public static final String COMMA = ","; public static final String LINE_SEPARATOR = "\r\n"; + + public static final String JSON_MEDIA_TYPE = "application/json; charset=utf-8"; } diff --git a/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/utils/HttpUtils.java b/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/utils/HttpUtils.java index 64f499f7..df39522a 100644 --- a/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/utils/HttpUtils.java +++ b/powerjob-common/src/main/java/com/github/kfcfans/powerjob/common/utils/HttpUtils.java @@ -3,7 +3,6 @@ package com.github.kfcfans.powerjob.common.utils; import okhttp3.*; import java.io.IOException; -import java.util.Objects; import java.util.concurrent.TimeUnit; /** @@ -29,6 +28,18 @@ public class HttpUtils { .get() .url(url) .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()) { if (response.code() == HTTP_SUCCESS_CODE) { ResponseBody body = response.body(); @@ -42,17 +53,4 @@ public class HttpUtils { 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; - } - } diff --git a/powerjob-server/src/main/java/com/github/kfcfans/powerjob/server/service/alarm/impl/WebHookAlarmService.java b/powerjob-server/src/main/java/com/github/kfcfans/powerjob/server/service/alarm/impl/WebHookAlarmService.java index e0c32cec..daa0561c 100644 --- a/powerjob-server/src/main/java/com/github/kfcfans/powerjob/server/service/alarm/impl/WebHookAlarmService.java +++ b/powerjob-server/src/main/java/com/github/kfcfans/powerjob/server/service/alarm/impl/WebHookAlarmService.java @@ -1,10 +1,14 @@ 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.server.persistence.core.model.UserInfoDO; import com.github.kfcfans.powerjob.server.service.alarm.Alarm; import com.github.kfcfans.powerjob.server.service.alarm.Alarmable; import lombok.extern.slf4j.Slf4j; +import okhttp3.MediaType; +import okhttp3.RequestBody; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; @@ -31,8 +35,12 @@ public class WebHookAlarmService implements Alarmable { if (StringUtils.isEmpty(webHook)) { return; } + + MediaType jsonType = MediaType.parse(OmsConstant.JSON_MEDIA_TYPE); + RequestBody requestBody = RequestBody.create(jsonType, JSONObject.toJSONString(alarm)); + try { - String response = HttpUtils.get(webHook); + String response = HttpUtils.post(webHook, requestBody); log.info("[WebHookAlarmService] invoke webhook[url={}] successfully, response is {}", webHook, response); }catch (Exception e) { log.warn("[WebHookAlarmService] invoke webhook[url={}] failed!", webHook, e);