diff --git a/nl-framework/nl-common/src/main/java/cn/code/nl/framework/common/util/http/AcsUtil.java b/nl-framework/nl-common/src/main/java/cn/code/nl/framework/common/util/http/AcsUtil.java index 0ada5947..21e2a13f 100644 --- a/nl-framework/nl-common/src/main/java/cn/code/nl/framework/common/util/http/AcsUtil.java +++ b/nl-framework/nl-common/src/main/java/cn/code/nl/framework/common/util/http/AcsUtil.java @@ -2,9 +2,13 @@ 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.StrUtil; +import java.lang.reflect.InvocationTargetException; import java.net.ConnectException; import java.net.NoRouteToHostException; import java.net.SocketException; @@ -18,6 +22,16 @@ import java.util.Map; */ public class AcsUtil { + /** + * ACS 开关配置 + */ + private static final String ACS_SWITCH_CONFIG_KEY = "acs_switch_config"; + + /** + * ACS 不启用 + */ + private static final String ACS_DISABLED = "0"; + /** * 发送 POST 请求并解析响应 * @@ -28,6 +42,9 @@ public class AcsUtil { * @return 响应对象 */ public static T post(String serverAddress, String api, Object request, Class responseType) { + if (isAcsDisabled()) { + return buildDefaultSuccessResponse(responseType); + } String url = buildUrl(serverAddress, api); String response; try { @@ -41,6 +58,54 @@ public class AcsUtil { 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 buildDefaultSuccessResponse(Class responseType) { + AcsBaseRespDTO response = new AcsBaseRespDTO(); + response.setSuccess(true); + response.setCode("200"); + return JsonUtils.convertObject(response, responseType); + } + /** * 拼接服务地址和 API */