Commit 9ef9a02b by zinc

请求网关添加log

parent d81a4708
package com.palacesun.engine.common; package com.palacesun.engine.common;
import com.palacesun.engine.gateway.EncryptionGatewayManager;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.palacesun.engine.gateway.EncryptionGatewayManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
...@@ -10,126 +12,237 @@ import org.apache.http.entity.StringEntity; ...@@ -10,126 +12,237 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
public class ClientUtil { public class ClientUtil {
public static ExecutorService executorService = Executors.newFixedThreadPool(10); private static final Log log = LogFactory.getLog(ClientUtil.class);
// HTTP请求日志debug开关,true表示开启详细日志,false表示关闭
public static String doClient(String url, String json) { private static final boolean HTTP_LOG_DEBUG = true;
String result = "调用" + url + "接口结束!"; public static ExecutorService executorService = Executors.newFixedThreadPool(10);
InnerThread innerThread = new InnerThread();
innerThread.url = url; public static String doClient(String url, String json) {
innerThread.json = json; String result = "调用" + url + "接口结束!";
innerThread.start(); InnerThread innerThread = new InnerThread();
return result; innerThread.url = url;
} innerThread.json = json;
innerThread.start();
public static CloseableHttpClient buildHttpClient() { return result;
return EncryptionGatewayManager.getParameterValue("gatewayurl").contains("https") ? SkipHttpsUtil.wrapClient() : HttpClients.createDefault(); }
}
public static CloseableHttpClient buildHttpClient() {
public static String doPost(String url, String json) { return EncryptionGatewayManager.getParameterValue("gatewayurl").contains("https") ? SkipHttpsUtil.wrapClient() : HttpClients.createDefault();
CloseableHttpClient httpClient = buildHttpClient(); }
HttpPost httpPost = new HttpPost(url);
String result = null; public static String doPost(String url, String json) {
CloseableHttpClient httpClient = buildHttpClient();
try { HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("content-type", "application/json;charset=UTF-8"); String result = null;
StringEntity se = new StringEntity(json, "utf-8"); long startTime = System.currentTimeMillis();
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost); // 记录请求日志
HttpEntity entity = response.getEntity(); if (HTTP_LOG_DEBUG) {
if (entity != null) { log.info("==================== HTTP请求开始 ====================");
result = EntityUtils.toString(entity, "UTF-8"); log.info("请求URL: " + url);
} log.info("请求方法: POST");
} catch (ClientProtocolException var20) { log.info("请求头: content-type=application/json;charset=UTF-8");
} catch (UnsupportedEncodingException var21) { log.info("请求参数: " + (json != null ? json : "{}"));
} catch (Exception e) { log.info("请求时间: " + new java.util.Date());
e.printStackTrace(); }
} finally {
try { try {
httpClient.close(); httpPost.setHeader("content-type", "application/json;charset=UTF-8");
} catch (IOException e) { StringEntity se = new StringEntity(json, "utf-8");
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
long endTime = System.currentTimeMillis();
// 记录响应日志
if (HTTP_LOG_DEBUG) {
log.info("==================== HTTP响应结束 ====================");
log.info("响应状态码: " + response.getStatusLine().getStatusCode());
log.info("响应状态: " + response.getStatusLine().getReasonPhrase());
log.info("响应参数: " + (result != null ? result : "null"));
log.info("响应时间: " + new java.util.Date());
log.info("请求耗时: " + (endTime - startTime) + " ms");
log.info("========================================================");
}
} catch (ClientProtocolException var20) {
long endTime = System.currentTimeMillis();
if (HTTP_LOG_DEBUG) {
log.error("==================== HTTP请求异常 ====================");
log.error("异常类型: ClientProtocolException");
log.error("异常信息: " + var20.getMessage());
log.error("请求耗时: " + (endTime - startTime) + " ms");
log.error("========================================================");
}
} catch (UnsupportedEncodingException var21) {
long endTime = System.currentTimeMillis();
if (HTTP_LOG_DEBUG) {
log.error("==================== HTTP请求异常 ====================");
log.error("异常类型: UnsupportedEncodingException");
log.error("异常信息: " + var21.getMessage());
log.error("请求耗时: " + (endTime - startTime) + " ms");
log.error("========================================================");
}
} catch (Exception e) {
long endTime = System.currentTimeMillis();
if (HTTP_LOG_DEBUG) {
log.error("==================== HTTP请求异常 ====================");
log.error("异常类型: " + e.getClass().getSimpleName());
log.error("异常信息: " + e.getMessage());
log.error("请求耗时: " + (endTime - startTime) + " ms");
log.error("========================================================");
}
e.printStackTrace(); e.printStackTrace();
} } finally {
try {
} httpClient.close();
} catch (IOException e) {
return result; e.printStackTrace();
} }
public static String doPost(String url, String json, GatewaySocketCallback callback) { }
CloseableHttpClient httpClient = buildHttpClient();
HttpPost httpPost = new HttpPost(url); return result;
String result = null; }
String resultStatus = "";
public static String doPost(String url, String json, GatewaySocketCallback callback) {
try { CloseableHttpClient httpClient = buildHttpClient();
httpPost.setHeader("content-type", "application/json;charset=UTF-8"); HttpPost httpPost = new HttpPost(url);
StringEntity se = new StringEntity(json, "utf-8"); String result = null;
httpPost.setEntity(se); String resultStatus = "";
HttpResponse response = httpClient.execute(httpPost); long startTime = System.currentTimeMillis();
HttpEntity entity = response.getEntity();
if (entity != null) { // 记录请求日志
result = EntityUtils.toString(entity, "UTF-8"); if (HTTP_LOG_DEBUG) {
} log.info("==================== HTTP请求开始(回调版本) ====================");
log.info("请求URL: " + url);
JSONObject jsonObject = JSONObject.parseObject(result); log.info("请求方法: POST");
String data = (String)jsonObject.get("data"); log.info("请求头: content-type=application/json;charset=UTF-8");
if (!jsonObject.get("code").equals("OOW_01")) { log.info("请求参数: " + (json != null ? json : "{}"));
if ("refresh".equals(data)) { log.info("请求时间: " + new java.util.Date());
resultStatus = "refresh"; log.info("回调对象: " + (callback != null ? callback.getClass().getSimpleName() : "null"));
}
try {
httpPost.setHeader("content-type", "application/json;charset=UTF-8");
StringEntity se = new StringEntity(json, "utf-8");
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
long endTime = System.currentTimeMillis();
// 记录响应日志
if (HTTP_LOG_DEBUG) {
log.info("==================== HTTP响应结束(回调版本) ====================");
log.info("响应状态码: " + response.getStatusLine().getStatusCode());
log.info("响应状态: " + response.getStatusLine().getReasonPhrase());
log.info("响应参数: " + (result != null ? result : "null"));
log.info("响应时间: " + new java.util.Date());
log.info("请求耗时: " + (endTime - startTime) + " ms");
}
JSONObject jsonObject = JSONObject.parseObject(result);
String data = (String) jsonObject.get("data");
if (!jsonObject.get("code").equals("OOW_01")) {
if ("refresh".equals(data)) {
resultStatus = "refresh";
} else {
resultStatus = "norefresh";
}
callback.isSucc(data);
if (HTTP_LOG_DEBUG) {
log.info("回调处理: 成功处理,数据=" + data + ",结果状态=" + resultStatus);
}
} else { } else {
resultStatus = "norefresh"; callback.clean();
if (HTTP_LOG_DEBUG) {
log.info("回调处理: 执行清理操作");
}
}
if (HTTP_LOG_DEBUG) {
log.info("====================================================================");
} }
callback.isSucc(data); } catch (ClientProtocolException var24) {
} else { long endTime = System.currentTimeMillis();
callback.clean(); if (HTTP_LOG_DEBUG) {
} log.error("==================== HTTP请求异常(回调版本) ====================");
} catch (ClientProtocolException var24) { log.error("异常类型: ClientProtocolException");
callback.isFail(); log.error("异常信息: " + var24.getMessage());
result = "noRefresh"; log.error("请求耗时: " + (endTime - startTime) + " ms");
} catch (UnsupportedEncodingException var25) { log.error("回调处理: 执行失败回调");
callback.isFail(); log.error("====================================================================");
result = "noRefresh"; }
} catch (Exception e) { callback.isFail();
e.printStackTrace(); result = "noRefresh";
callback.isFail(); } catch (UnsupportedEncodingException var25) {
result = "noRefresh"; long endTime = System.currentTimeMillis();
} finally { if (HTTP_LOG_DEBUG) {
try { log.error("==================== HTTP请求异常(回调版本) ====================");
httpClient.close(); log.error("异常类型: UnsupportedEncodingException");
} catch (IOException e) { log.error("异常信息: " + var25.getMessage());
log.error("请求耗时: " + (endTime - startTime) + " ms");
log.error("回调处理: 执行失败回调");
log.error("====================================================================");
}
callback.isFail();
result = "noRefresh";
} catch (Exception e) {
long endTime = System.currentTimeMillis();
if (HTTP_LOG_DEBUG) {
log.error("==================== HTTP请求异常(回调版本) ====================");
log.error("异常类型: " + e.getClass().getSimpleName());
log.error("异常信息: " + e.getMessage());
log.error("请求耗时: " + (endTime - startTime) + " ms");
log.error("回调处理: 执行失败回调");
log.error("====================================================================");
}
e.printStackTrace(); e.printStackTrace();
} callback.isFail();
result = "noRefresh";
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
return resultStatus; return resultStatus;
} }
public static CloseableHttpClient getWrapClient() { public static CloseableHttpClient getWrapClient() {
return EncryptionGatewayManager.getParameterValue("gatewayurl").contains("https") ? SkipHttpsUtil.wrapClient() : HttpClients.createDefault(); return EncryptionGatewayManager.getParameterValue("gatewayurl").contains("https") ? SkipHttpsUtil.wrapClient() : HttpClients.createDefault();
} }
public static void main(String[] args) { public static void main(String[] args) {
String url = "http://192.168.2.88:8182/initconfigfile/loadconfig"; log.info("ClientUtil main method started");
String result = doClient(url, "{}"); String url = "http://172.16.100.87:9005/initconfigfile/loadconfig";
Map stringToMap = JSONObject.parseObject(result); String result = doClient(url, "{}");
System.out.println("result:" + result); System.out.println("result:" + result);
} }
public static class InnerThread extends Thread { public static class InnerThread extends Thread {
String url; String url;
String json; String json;
public void run() { public void run() {
ClientUtil.doPost(this.url, this.json); ClientUtil.doPost(this.url, this.json);
} }
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论