feat: acs开关

This commit is contained in:
2026-07-28 08:44:20 +08:00
parent 220bdec122
commit 50e47de5bd

View File

@@ -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.ServerException;
import cn.code.nl.framework.common.exception.ServiceException; 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.json.JsonUtils;
import cn.code.nl.framework.common.util.spring.SpringUtils;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import java.lang.reflect.InvocationTargetException;
import java.net.ConnectException; import java.net.ConnectException;
import java.net.NoRouteToHostException; import java.net.NoRouteToHostException;
import java.net.SocketException; import java.net.SocketException;
@@ -18,6 +22,16 @@ import java.util.Map;
*/ */
public class AcsUtil { public class AcsUtil {
/**
* ACS 开关配置
*/
private static final String ACS_SWITCH_CONFIG_KEY = "acs_switch_config";
/**
* ACS 不启用
*/
private static final String ACS_DISABLED = "0";
/** /**
* 发送 POST 请求并解析响应 * 发送 POST 请求并解析响应
* *
@@ -28,6 +42,9 @@ public class AcsUtil {
* @return 响应对象 * @return 响应对象
*/ */
public static <T> T post(String serverAddress, String api, Object request, Class<T> responseType) { 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 url = buildUrl(serverAddress, api);
String response; String response;
try { try {
@@ -41,6 +58,54 @@ public class AcsUtil {
return JsonUtils.parseObject(response, responseType); 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");
return JsonUtils.convertObject(response, responseType);
}
/** /**
* 拼接服务地址和 API * 拼接服务地址和 API
*/ */