本人在作接口自動化時候,由於服務器不穩定形成可能的用例失敗,但這個失敗表象只是在獲取響應實體的json對象時爲空,在後期排查問題時可能形成困擾,因此特地加了一個獲取響應失敗的通知,目的就是即便了解到服務器異常。暫時用的是免費的alertover,用了好久,簡單可靠是它的優勢,後續會加入微信提醒。分享代碼,供你們參考。java
下面是獲取響應實體的json對象的方法(可忽略某一些封裝方法):mysql
/** * 獲取響應實體 * <p>會自動設置cookie,可是須要各個項目再自行實現cookie管理</p> * <p>該方法只會處理文本信息,對於文件處理能夠調用兩個過時的方法解決</p> * * @param request 請求對象 * @return 返回json類型的對象 */ public static JSONObject getHttpResponse(HttpRequestBase request) { if (!isRightRequest(request)) return new JSONObject(); beforeRequest(request); JSONObject res = new JSONObject(); RequestInfo requestInfo = new RequestInfo(request); if (HEADER_KEY) output("===========request header===========", Arrays.asList(request.getAllHeaders())); long start = Time.getTimeStamp(); try (CloseableHttpResponse response = ClientManage.httpsClient.execute(request)) { long end = Time.getTimeStamp(); long elapsed_time = end - start; if (HEADER_KEY) output("===========response header===========", Arrays.asList(response.getAllHeaders())); int status = getStatus(response, res); JSONObject setCookies = afterResponse(response); String content = getContent(response); int data_size = content.length(); res.putAll(getJsonResponse(content, setCookies)); int code = iBase == null ? -2 : iBase.checkCode(res, requestInfo); if (!iBase.isRight(res)) new AlertOver("響應狀態碼錯誤:" + status, "狀態碼錯誤:" + status, requestInfo.getUrl(), requestInfo).sendSystemMessage(); MySqlTest.saveApiTestDate(requestInfo, data_size, elapsed_time, status, getMark(), code, LOCAL_IP, COMPUTER_USER_NAME); } catch (Exception e) { logger.warn("獲取請求相應失敗!", e); if (!SysInit.isBlack(requestInfo.getHost())) new AlertOver("接口請求失敗", requestInfo.toString(), requestInfo.getUrl(), requestInfo).sendSystemMessage(); } finally { HEADER_KEY = false; if (!SysInit.isBlack(requestInfo.getHost())) { if (requests.size() > 9) requests.removeFirst(); boolean add = requests.add(request); } } return res; }
下面是alertover類的代碼,比較簡單:sql
package com.fun.utils.message; import com.fun.frame.httpclient.FanLibrary; import com.fun.base.bean.RequestInfo; import com.fun.base.interfaces.IMessage; import com.fun.db.mysql.MySqlTest; import com.fun.config.SysInit; import net.sf.json.JSONObject; import org.apache.http.client.methods.HttpPost; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class AlertOver extends FanLibrary implements IMessage { private static Logger logger = LoggerFactory.getLogger(AlertOver.class); String title; String content; String murl; RequestInfo requestInfo; private static String system = "s-7e93ec02-1308-480c-bc11-a7260c14";//系統異常 private static String function = "s-7e3b7ea5-b4b0-4479-a0e3-bce6c830";//功能異常 private static String business = "s-466a191a-cbb8-4164-b8be-9779bb88";//業務異常 private static String remind = "s-f49ac5bc-008b-4b11-890e-6715ef89";//提醒推送 private static String code = "s-490d0fc6-35cc-4430-9f87-09cdeb05";//程序異常 private static final String testGroup = "g-4eefc0ad-19af-4b1c-9d0b-ef87be15"; public AlertOver() { this("test title", "test content!"); } public AlertOver(String title, String content) { this.title = title; this.content = content + LINE + "發送源:" + COMPUTER_USER_NAME; } public AlertOver(String title, String content, String url) { this(title, content); this.murl = url; } public AlertOver(String title, String content, String url, RequestInfo requestInfo) { this(title, content); this.murl = url; this.requestInfo = requestInfo; } /** * 發送系統異常 */ public void sendSystemMessage() { if (SysInit.isBlack(murl)) return; sendMessage(system); MySqlTest.saveAlertOverMessage(requestInfo, "system", title, LOCAL_IP, COMPUTER_USER_NAME); logger.info("發送系統錯誤提醒,title:{},ip:{},computer:{}", title, LOCAL_IP, COMPUTER_USER_NAME); } /** * 發送功能異常 */ public void sendFunctionMessage() { sendMessage(function); } /** * 發送業務異常 */ public void sendBusinessMessage() { sendMessage(business); } /** * 發送程序異常 */ public void sendCodeMessage() { sendMessage(code); } /** * 提醒推送 */ public void sendRemindMessage() { sendMessage(remind); } /** * 發送消息 * * @return */ public void sendMessage(String source) { if (SysInit.isBlack(murl)) return; String url = "https://api.alertover.com/v1/alert"; String receiver = testGroup;//測試組ID JSONObject jsonObject = new JSONObject();// 新建json數組 jsonObject.put("frame", source);// 添加發送源id jsonObject.put("receiver", receiver);// 添加接收組id jsonObject.put("content", content);// 發送內容 jsonObject.put("title", title);// 發送標題 jsonObject.put("url", murl);// 發送標題 jsonObject.put("sound", "pianobar");// 發送聲音 logger.debug("消息詳情:{}", jsonObject.toString()); HttpPost httpPost = getHttpPost(url, jsonObject); 取消發送 getHttpResponse(httpPost); } }