fix: 是否启用acs连接
This commit is contained in:
@@ -1,146 +0,0 @@
|
||||
package cn.code.nl.framework.common.util.http;
|
||||
|
||||
import cn.code.nl.framework.common.exception.ServerException;
|
||||
import cn.code.nl.framework.common.exception.ServiceException;
|
||||
import cn.code.nl.framework.common.pojo.AcsBaseRespDTO;
|
||||
import cn.code.nl.framework.common.pojo.CommonResult;
|
||||
import cn.code.nl.framework.common.util.json.JsonUtils;
|
||||
import cn.code.nl.framework.common.util.spring.SpringUtils;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.ConnectException;
|
||||
import java.net.NoRouteToHostException;
|
||||
import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ACS 调用工具
|
||||
*/
|
||||
public class AcsUtil {
|
||||
|
||||
/**
|
||||
* ACS 开关配置
|
||||
*/
|
||||
private static final String ACS_SWITCH_CONFIG_KEY = "acs_switch_config";
|
||||
|
||||
/**
|
||||
* ACS 不启用
|
||||
*/
|
||||
private static final String ACS_DISABLED = "0";
|
||||
|
||||
/**
|
||||
* 发送 POST 请求并解析响应
|
||||
*
|
||||
* @param serverAddress ACS 服务地址
|
||||
* @param api API 路径
|
||||
* @param request 请求参数
|
||||
* @param responseType 响应类型
|
||||
* @return 响应对象
|
||||
*/
|
||||
public static <T> T post(String serverAddress, String api, Object request, Class<T> responseType) {
|
||||
if (isAcsDisabled()) {
|
||||
return buildDefaultSuccessResponse(responseType);
|
||||
}
|
||||
String url = buildUrl(serverAddress, api);
|
||||
String response;
|
||||
try {
|
||||
response = HttpUtils.post(url, headers(), JsonUtils.toJsonString(request));
|
||||
} catch (RuntimeException ex) {
|
||||
if (isNetworkException(ex)) {
|
||||
throw new ServiceException(500, "ACS服务网络不通");
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
return JsonUtils.parseObject(response, responseType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否关闭 ACS
|
||||
*/
|
||||
private static boolean isAcsDisabled() {
|
||||
String configValue = getAcsSwitchConfigValue();
|
||||
return StrUtil.equals(ACS_DISABLED, configValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 ACS 开关配置
|
||||
*/
|
||||
private static String getAcsSwitchConfigValue() {
|
||||
Object configApi;
|
||||
try {
|
||||
configApi = SpringUtils.getBean("configApi");
|
||||
} catch (RuntimeException ex) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Object result = configApi.getClass()
|
||||
.getMethod("getConfigValueByKey", String.class)
|
||||
.invoke(configApi, ACS_SWITCH_CONFIG_KEY);
|
||||
if (result instanceof CommonResult<?> commonResult) {
|
||||
Object data = commonResult.getCheckedData();
|
||||
return data instanceof String value ? value : null;
|
||||
}
|
||||
return null;
|
||||
} catch (InvocationTargetException ex) {
|
||||
Throwable targetException = ex.getTargetException();
|
||||
if (targetException instanceof RuntimeException runtimeException) {
|
||||
throw runtimeException;
|
||||
}
|
||||
throw new ServerException(500, "获取 ACS 开关配置失败");
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
throw new ServerException(500, "获取 ACS 开关配置失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建默认成功响应
|
||||
*/
|
||||
private static <T> T buildDefaultSuccessResponse(Class<T> responseType) {
|
||||
AcsBaseRespDTO response = new AcsBaseRespDTO();
|
||||
response.setSuccess(true);
|
||||
response.setCode("200");
|
||||
response.setTraceId(IdUtil.simpleUUID());
|
||||
response.setTimestamp(System.currentTimeMillis());
|
||||
return JsonUtils.convertObject(response, responseType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接服务地址和 API
|
||||
*/
|
||||
private static String buildUrl(String serverAddress, String api) {
|
||||
String baseUrl = StrUtil.removeSuffix(serverAddress, StrUtil.SLASH);
|
||||
String apiPath = StrUtil.addPrefixIfNot(api, StrUtil.SLASH);
|
||||
return baseUrl + apiPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 JSON 请求头
|
||||
*/
|
||||
private static Map<String, String> headers() {
|
||||
return Collections.singletonMap("Content-Type", "application/json;charset=UTF-8");
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为 ACS 网络连接异常
|
||||
*/
|
||||
private static boolean isNetworkException(Throwable ex) {
|
||||
Throwable cause = ex;
|
||||
while (cause != null) {
|
||||
if (cause instanceof ConnectException
|
||||
|| cause instanceof SocketTimeoutException
|
||||
|| cause instanceof UnknownHostException
|
||||
|| cause instanceof NoRouteToHostException
|
||||
|| cause instanceof SocketException) {
|
||||
return true;
|
||||
}
|
||||
cause = cause.getCause();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user