fix: 是否启用acs连接
This commit is contained in:
@@ -18,6 +18,12 @@
|
||||
<artifactId>nl-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.nl.cloud</groupId>
|
||||
<artifactId>nl-module-infra-api</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring 核心 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.code.nl.framework.execute.config;
|
||||
|
||||
import cn.code.nl.framework.execute.biz.api.lms.LmsTaskCommonApi;
|
||||
import cn.code.nl.framework.execute.biz.api.wms.WmsTaskCommonApi;
|
||||
import cn.code.nl.module.infra.api.config.ConfigApi;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@@ -13,6 +14,6 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
* @Date: 2026/7/15
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@EnableFeignClients(clients = {LmsTaskCommonApi.class, WmsTaskCommonApi.class})
|
||||
@EnableFeignClients(clients = {LmsTaskCommonApi.class, WmsTaskCommonApi.class, ConfigApi.class})
|
||||
public class NlExecuteRpcAutoConfiguration {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
package cn.code.nl.framework.execute.util.http;
|
||||
|
||||
import cn.code.nl.framework.common.exception.ServiceException;
|
||||
import cn.code.nl.framework.common.pojo.AcsBaseRespDTO;
|
||||
import cn.code.nl.framework.common.util.http.HttpUtils;
|
||||
import cn.code.nl.framework.common.util.json.JsonUtils;
|
||||
import cn.code.nl.framework.common.util.spring.SpringUtils;
|
||||
import cn.code.nl.module.infra.api.config.ConfigApi;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ACS 调用工具
|
||||
*/
|
||||
@Slf4j
|
||||
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() {
|
||||
try {
|
||||
ConfigApi configApi = SpringUtils.getBean("configApi");
|
||||
String checkedData = configApi.getConfigValueByKey(ACS_SWITCH_CONFIG_KEY).getCheckedData();
|
||||
return ObjectUtil.isEmpty(checkedData) ? ACS_DISABLED : checkedData;
|
||||
} catch (Exception ex) {
|
||||
log.error("获取 ACS 开关配置失败", ex);
|
||||
return ACS_DISABLED;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建默认成功响应
|
||||
*/
|
||||
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