This commit is contained in:
六如
2024-10-27 21:08:00 +08:00
parent 13b06f0592
commit 40835d2f69
57 changed files with 991 additions and 536 deletions

View File

@@ -72,12 +72,12 @@ public class ApiResponse {
/**
* 业务异常码
*/
private String sub_code = "";
private String subCode = "";
/**
* 业务异常信息
*/
private String sub_msg = "";
private String subMsg = "";
/**
* 返回对象
@@ -98,8 +98,8 @@ public class ApiResponse {
IError error = e.getError();
apiResponse.setCode(error.getCode());
apiResponse.setMsg(error.getMsg());
apiResponse.setSub_code(error.getSubCode());
apiResponse.setSub_msg(error.getSubMsg());
apiResponse.setSubCode(error.getSubCode());
apiResponse.setSubMsg(error.getSubMsg());
return apiResponse;
}
@@ -109,8 +109,8 @@ public class ApiResponse {
IError error = errorMeta.getError(locale);
apiResponse.setCode(error.getCode());
apiResponse.setMsg(error.getMsg());
apiResponse.setSub_code(error.getSubCode());
apiResponse.setSub_msg(subMsg);
apiResponse.setSubCode(error.getSubCode());
apiResponse.setSubMsg(subMsg);
return apiResponse;
}
@@ -120,8 +120,8 @@ public class ApiResponse {
IError error = errorMeta.getError(locale);
apiResponse.setCode(error.getCode());
apiResponse.setMsg(error.getMsg());
apiResponse.setSub_code(error.getSubCode());
apiResponse.setSub_msg(error.getSubMsg());
apiResponse.setSubCode(error.getSubCode());
apiResponse.setSubMsg(error.getSubMsg());
return apiResponse;
}

View File

@@ -8,11 +8,13 @@ import com.gitee.sop.gateway.service.manager.ApiManager;
import com.gitee.sop.gateway.util.CopyUtil;
import com.gitee.sop.support.service.ApiRegisterService;
import com.gitee.sop.support.service.dto.RegisterDTO;
import com.gitee.sop.support.service.dto.RegisterResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Objects;
/**
* @author 六如
*/
@@ -23,26 +25,44 @@ public class ApiRegisterServiceImpl implements ApiRegisterService {
private static final int REG_SOURCE_SYS = 1;
@Autowired
private ApiManager apiCacheManager;
private ApiManager apiManager;
@Autowired
private ApiInfoMapper apiInfoMapper;
@Override
public void register(RegisterDTO registerDTO) {
public RegisterResult register(RegisterDTO registerDTO) {
log.info("收到接口注册, registerDTO={}", registerDTO);
ApiInfoDTO apiInfoDTO = CopyUtil.copyBean(registerDTO, ApiInfoDTO::new);
apiInfoDTO.setStatus(StatusEnum.ENABLE.getValue());
ApiInfo apiInfo = apiInfoMapper.getByNameVersion(apiInfoDTO.getApiName(), apiInfoDTO.getApiVersion());
if (apiInfo == null) {
apiInfo = new ApiInfo();
try {
ApiInfoDTO apiInfoDTO = CopyUtil.copyBean(registerDTO, ApiInfoDTO::new);
apiInfoDTO.setStatus(StatusEnum.ENABLE.getValue());
ApiInfo apiInfo = apiInfoMapper.getByNameVersion(apiInfoDTO.getApiName(), apiInfoDTO.getApiVersion());
if (apiInfo == null) {
apiInfo = new ApiInfo();
} else {
check(apiInfo, registerDTO);
}
CopyUtil.copyPropertiesIgnoreNull(apiInfoDTO, apiInfo);
apiInfo.setRegSource(REG_SOURCE_SYS);
// 保存到数据库
apiInfoMapper.saveOrUpdate(apiInfo);
apiInfoDTO.setId(apiInfo.getId());
// 保存到缓存
apiManager.save(apiInfoDTO);
return RegisterResult.success();
} catch (Exception e) {
log.error("接口注册失败, registerDTO={}", registerDTO, e);
return RegisterResult.error(e.getMessage());
}
CopyUtil.copyPropertiesIgnoreNull(apiInfoDTO, apiInfo);
apiInfo.setRegSource(REG_SOURCE_SYS);
// 保存到数据库
apiInfoMapper.saveOrUpdate(apiInfo);
// 保存到缓存
apiCacheManager.save(apiInfoDTO);
}
private void check(ApiInfo apiInfo, RegisterDTO registerDTO) {
if (!Objects.equals(apiInfo.getApplication(), registerDTO.getApplication())) {
throw new RuntimeException("接口[" + registerDTO + "]已存在于[" + apiInfo.getApplication() + "]应用中.必须保证接口全局唯一");
}
}
}

View File

@@ -32,7 +32,7 @@ public class ResultRouteInterceptor implements RouteInterceptor {
}
if (result instanceof Map) {
Map<String, Object> map = (Map<String, Object>) result;
Map<?, ?> map = (Map<?, ?>) result;
Object className = map.get(CLASS);
// 处理文件下载
if (Objects.equals(className, FileData.class.getName()) || Objects.equals(className, CommonFileData.class.getName())) {

View File

@@ -62,7 +62,7 @@ public class ApiValidator implements Validator {
private ApiConfig apiConfig;
@Autowired
private ApiManager apiCacheManager;
private ApiManager apiManager;
@Autowired
private IpBlacklistManager ipBlacklistManager;
@@ -94,7 +94,7 @@ public class ApiValidator implements Validator {
checkIP(apiRequestContext);
ApiRequest apiRequest = apiRequestContext.getApiRequest();
ApiInfoDTO apiInfo = apiCacheManager.get(apiRequest.getMethod(), apiRequest.getVersion());
ApiInfoDTO apiInfo = apiManager.get(apiRequest.getMethod(), apiRequest.getVersion());
// 检查接口信息
checkApiInfo(apiRequestContext, apiInfo, isvDTO);