瀏覽代碼

ias

master
chenchaod 3 月之前
父節點
當前提交
846fd8f766

+ 29
- 184
zhywpt-service-ias/src/main/java/cn/com/taiji/ias/manager/AbstractCommManager.java 查看文件

@@ -30,6 +30,7 @@ public abstract class AbstractCommManager extends AbstractManager {
@Autowired
protected FeignClientManager feignClientManager;

//=================================================心中台=================================================
public <RS extends AbstractSignTypeResponse> RS jsonPostRepeat(AbstractSignTypeRequest<RS> req)
throws ServiceHandleException {
return jsonPostRepeat(req, 3);
@@ -48,45 +49,6 @@ public abstract class AbstractCommManager extends AbstractManager {
return jsonPostRepeat(req, repeat, null, null);
}

@Deprecated
public <RS> RS jsonPostRepeat(AbstractReqIdTypeRequest req, Class<RS> clazz, String ifCode, String accessToken)
throws ManagerException {
return convertException(req, ifCode, r -> jsonPostReqIdRepeat(r, clazz, 3, ifCode, accessToken));
}

/**
* <pre>
* 重复3次提交请求,会将调接口错误的响应转换成前端的响应
* </pre>
*/
public <RS> RS jsonPostRepeat(AbstractOldSystemRequest<RS> req, String accessToken) throws ManagerException {
return convertException(req, req.getIfCode(), r -> jsonPostOldSystemRepeat(r, 3, accessToken));
}

public <RS> RS jsonPostRepeat(AbstractOldSystemRequest<RS> req, int repeat, String accessToken)
throws ServiceHandleException {
for (int i = 0; i < repeat; i++) {
try {
return jsonPost(req, accessToken);
} catch (IOException e) {
logger.error("第 {} 次执行Exception:{}", i + 1, e.getMessage());
// if (logManager != null) {
// logManager.addConsoleLog(FeeBusinessType.EXCEPTION, req.getFilename(), "error", e.getMessage());
// }
} catch (ApiRequestException e) {
logger.error("接口调用异常", e);
SignJsonResponse jsonRes = toJsonResponse(e);
assert jsonRes != null;
Integer statusCode = jsonRes.getStatusCode();
if (statusCode.equals(801)) {
statusCode = 704;
}
throw new ServiceHandleException(jsonRes.getErrorMsg(), statusCode);
}
}
return null;
}

public <RS extends AbstractSignTypeResponse> RS jsonPostRepeat(AbstractSignTypeRequest<RS> req, int repeat,
String ifCode, String accessToken) throws ServiceHandleException {
for (int i = 0; i < repeat; i++) {
@@ -128,174 +90,57 @@ public abstract class AbstractCommManager extends AbstractManager {
return feignClientManager.jsonPost(req, clazz, ifCode, accessToken);
}

/**
* 未做异常处理的接口调用
*/
protected <RS> RS jsonPost(AbstractOldSystemRequest<RS> req, String accessToken) throws IOException {
return feignClientManager.jsonPost(req, accessToken);
}

private SignJsonResponse toJsonResponse(ApiRequestException e) {
String responseJson = e.getMessage().substring(0, e.getMessage().length() - 4);
try {
return JsonTools.json2Object(responseJson, SignJsonResponse.class);
} catch (IOException e1) {
logger.error("转换json出错 json:\n{} \n exception:{}", responseJson, e1.getMessage());
}
return null;
//===========================================================老中台==============================================

public <RS> RS jsonPostRepeat(AbstractOldSystemRequest<RS> req)
throws ServiceHandleException {
return jsonPostRepeat(req, null);
}

/**
* 会将调接口错误的响应转换成前端的响应
* <pre>
* 重复3次提交请求,会将调接口错误的响应转换成前端的响应
* </pre>
*/
private <RE extends BaseModel, RS> RS convertException(RE req, String ifCode, IOFunction<RE, RS> f)
throws ManagerException {
try {
return f.apply(req);
} catch (ApiRequestException e) {
convertApiRequestException(e, ifCode);
return null;// 走不到这儿
} catch (NoHttpResponseException e) {
logger.error("NoHttpResponseException:ifCode:{}", ifCode, e);
throw new ManagerException("网络中断异常!");
} catch (IOException e) {
logger.error("IOException(网络异常):ifCode:{}", ifCode, e);
throw new ManagerException("网络异常!");
} catch (RetryableException e) {
logger.error("RetryableException(重试异常):ifCode:{}", ifCode, e);
throw new ManagerException("服务不可用,请重试!");
} catch (RuntimeException e) {
if (e.getCause() != null && e.getCause() instanceof ClientException) {
logger.error("ClientException:ifCode:{}", ifCode, e);
throw new ManagerException("服务不可用,请重试!");
}
throw e;
}
public <RS> RS jsonPostRepeat(AbstractOldSystemRequest<RS> req, String accessToken) throws ServiceHandleException {
return jsonPostOldSystemRepeat(req, 3, accessToken);
}

private <RE extends AbstractOldSystemRequest<RS>, RS> RS jsonPostOldSystemRepeat(RE req, int repeat,
String accessToken) throws IOException {
String accessToken) throws ServiceHandleException {
for (int i = 0; i < repeat; i++) {
try {
return feignClientManager.jsonPost(req, accessToken);
} catch (NoHttpResponseException e) {
logger.error("第 {} NoHttpResponseException:{}", i + 1, e.getMessage());
if (i >= repeat - 1) {
throw e;
}
} catch (IOException e) {
logger.error("第 {} 次执行Exception:{}", i + 1, e.getMessage());
if (i >= repeat - 1) {
throw e;
}
// if (i >= repeat - 1) {
// throw e;
// }
} catch (ApiRequestException e) {
logger.error("第 {} 次执行ApiRequestException:{}", i + 1, e.getMessage());
if (i >= repeat - 1) {
throw e;
logger.error("接口调用异常", e);
SignJsonResponse jsonRes = toJsonResponse(e);
assert jsonRes != null;
Integer statusCode = jsonRes.getStatusCode();
if (statusCode.equals(801)) {
statusCode = 704;
}
throw new ServiceHandleException(jsonRes.getErrorMsg(), statusCode);
}
}
return null;
}
private int location = -1;
private void convertApiRequestException(ApiRequestException e, String ifCode) throws ManagerException {
String uri = getRequestUri();
logger.info("调用接口Api业务失败,Controller的url:{}", uri);
StackTraceElement[] stackTrace = e.getStackTrace();
if (stackTrace != null) {
if (location == -1) {
location = getMethodLocation(stackTrace);
}
System.out.println(toLogString("调接口位置:{}", stackTrace[location]));
} else {
logger.error("", e);
}
if (e.getErrCode() == FileProtocolSystemError.SIGN_FAILED.getCode()) {
throw new ManagerException(e.getMessage());
}
SignJsonResponse jsonRes = toJsonResponse(e);
assert jsonRes != null;
if (e.getErrCode() == GlyServiceError.FORMAT_VALIDATE_ERR.getCode()) {
System.out.println(toLogString("错误类别:FORMAT_ERROR(703),ifCode:{},\n响应内容:{}", ifCode, jsonRes));
throw new FormatException(jsonRes.getErrorMsg());
}
if (e.getErrCode() == GlyServiceError.BUSINESS_VALIDATE_ERR.getCode() || e.getErrCode() == 801) {
System.out.println(toLogString("错误类别:BUSINESS_ERROR(704),ifCode:{}\n响应内容:{}", ifCode, jsonRes));
throw new ManagerException(jsonRes.getErrorMsg());// 业务校验出错
}
if (e.getErrCode() == GlyServiceError.PERSIST_ERR.getCode() || e.getErrCode() == 801) {
System.out.println(toLogString("错误类别:PERSIST_ERR(705),ifCode:{}\n响应内容:{}", ifCode, jsonRes));
throw new ManagerException(jsonRes.getErrorMsg());// 持久化出错
}
// if (e.getErrCode() == GlyServiceError.UNKNOWN_ERROR.getCode()) {
// System.out.println(toLogString("错误类别:UNKNOWN_ERROR(999:实际是服务端报错),ifCode:{}\n响应内容:{}", ifCode, jsonRes));
// throw new ManagerException("网络错误!");// 实际是服务端报错
// }
// if (e.getErrCode() == GlyServiceError.IO_ERROR.getCode()) {
// System.out.println(toLogString("错误类别:IO_ERROR(998:实际是服务端报错),ifCode:{}\n响应内容:{}", ifCode, jsonRes));
// throw new ManagerException("网络错误...");// 实际是服务端报错
// }
if (e.getErrCode() == 808) {
System.out.println(toLogString("错误类别:TOKEN_INVALID(808),ifCode:{}\n响应内容:{}", ifCode, jsonRes));
throw new TokenInvalidException(e.getMessage());// 实际是服务端报错
}
if (e.getErrCode() == 996) {// 中台接口失败
System.out.println(toLogString("错误类别:中台内部失败(996),ifCode:{}\n响应内容:{}", ifCode, jsonRes));
throw new ManagerException("网络错误。");// 实际是服务端报错
}
System.out.println(toLogString("错误类别:未知错误,errorCode:{},ifCode:{}\n响应内容:{}", e.getErrCode(), ifCode, jsonRes));
throw new ManagerException("未知错误:" + jsonRes.getErrorMsg());
}

private String getRequestUri() {
try {
ServletRequestAttributes requestAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert requestAttributes != null;
HttpServletRequest request = requestAttributes.getRequest();
return WebTools.getUri(request);
} catch (Exception e) {
return "";
}
}
private int getMethodLocation(StackTraceElement[] stackTrace) {
for (int i = 0; i < stackTrace.length; i++) {
if (!stackTrace[i].getClassName().contains("AbstractCommManager")
&& !stackTrace[i].getClassName().contains("FeignClientManager")) {
return i;
}
}
return -1;
}

private <RE extends AbstractReqIdTypeRequest,RS> RS jsonPostReqIdRepeat(RE req,Class<RS> clazz, int repeat, String ifCode, String accessToken)
throws IOException {
for (int i = 0; i < repeat; i++) {
try {
return feignClientManager.jsonPost(req, clazz, ifCode, accessToken);
} catch (NoHttpResponseException e) {
logger.error("第 {} NoHttpResponseException:{}", i + 1, e.getMessage());
if (i >= repeat - 1) {
throw e;
}
} catch (IOException e) {
logger.error("第 {} 次执行Exception:{}", i + 1, e.getMessage());
if (i >= repeat - 1) {
throw e;
}
} catch (ApiRequestException e) {
if (e.getErrCode() == 707) {
// 707压住异常
try {
return clazz.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
throw e;
}
private SignJsonResponse toJsonResponse(ApiRequestException e) {
String responseJson = e.getMessage().substring(0, e.getMessage().length() - 4);
try {
return JsonTools.json2Object(responseJson, SignJsonResponse.class);
} catch (IOException e1) {
logger.error("转换json出错 json:\n{} \n exception:{}", responseJson, e1.getMessage());
}
return null;
}


}

+ 31
- 0
zhywpt-service-ias/src/test/java/cn/com/taiji/oqs/client/demo/OneTest.java 查看文件

@@ -0,0 +1,31 @@
package cn.com.taiji.oqs.client.demo;

import cn.com.taiji.common.manager.ManagerException;
import cn.com.taiji.core.model.comm.protocol.ias.order.GetCustomerIdRequest;
import cn.com.taiji.core.model.comm.protocol.ias.order.GetCustomerIdResponse;
import cn.com.taiji.ias.Application;
import cn.com.taiji.ias.manager.AbstractCommManager;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
* @Author:ChenChao
* @Date:2025/6/11 16:22
* @Filename:OneTest
* @description:
*/
@SpringBootTest(classes = Application.class)
public class OneTest extends AbstractCommManager {

@Test
public void test() {

GetCustomerIdRequest getCustomerIdRequest = new GetCustomerIdRequest();
try {
GetCustomerIdResponse response = jsonPostRepeat(getCustomerIdRequest);
logger.info("response:{}", response);
} catch (ManagerException e) {
throw new RuntimeException(e);
}
}
}

Loading…
取消
儲存