Merge branch 'feature/REQ-1609' into dev

This commit is contained in:
TanJ 2023-11-17 16:57:13 +08:00
commit e066dca533

View File

@ -2,13 +2,20 @@ package cn.axzo.pokonyan.util;
import cn.axzo.framework.domain.ServiceException;
import cn.axzo.framework.domain.data.AssertUtil;
import cn.axzo.framework.domain.web.result.ApiResult;
import cn.azxo.framework.common.model.CommonResponse;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Supplier;
@ -21,17 +28,26 @@ import java.util.function.Supplier;
@Slf4j
public class RpcUtil {
/**
* 常用的RPC请求返回值解析如果 被请求方 返回非200会抛出异常
*
*/
public static <R,T> T rpcProcessor(Supplier<R> supplier, String operationType, Object... param) {
public static <T> T rpcCommonProcessor(Supplier<CommonResponse<T>> supplier, String operationType, Object... param) {
return rpcProcessorMayThrow(supplier, operationType, commonResponse -> {
throw new ServiceException(commonResponse.getMsg());
}, param);
}
public static <T> T rpcApiResultProcessor(Supplier<ApiResult<T>> supplier, String operationType, Object... param) {
log.info(operationType + "-Param: " + JSONUtil.toJsonStr(param));
ApiResult<T> result = printLatency(supplier,operationType);
log.info(operationType + "-Result: " + JSONUtil.toJsonStr(result));
Assert.notNull(result, "服务调用异常");
Assert.isTrue(result.getCode() == 200, "服务调用异常:" + result.getMsg());
return result.getData();
}
/**
* 第三方发生异常时自定义处理逻辑
@ -42,29 +58,27 @@ public class RpcUtil {
* @param param
* @return
*/
public static <R,P> R rpcProcessorMayThrow(Supplier<P> supplier, String operationType, Consumer<Response<R>> throwConsumer, Object... param) {
Assert.notNull(throwConsumer,"自定义的异常处理不可为空");
public static <T> T rpcProcessorMayThrow(Supplier<CommonResponse<T>> supplier, String operationType, Consumer<CommonResponse<T>> throwConsumer, Object... param) {
Assert.notNull(throwConsumer, "自定义的异常处理不可为空");
log.info(operationType + "-Param: " + JSONUtil.toJsonStr(param));
Response<R> response = printLatency(supplier, operationType);
log.info(operationType + "-Result: " + JSONUtil.toJsonStr(response));
Assert.notNull(response, "服务调用异常");
CommonResponse<T> result = printLatency(supplier, operationType);
log.info(operationType + "-Result: " + JSONUtil.toJsonStr(result));
Assert.notNull(result, "服务调用异常");
// 200自定义处理
if (HttpStatus.HTTP_OK != response.getCode()) {
throwConsumer.accept(response);
if (HttpStatus.HTTP_OK != result.getCode()) {
throwConsumer.accept(result);
}
return response.getData();
return result.getData();
}
public static <P,R> Response<R> printLatency(Supplier<P> function, String optType) {
public static <R> R printLatency(Supplier< R> function,String optType) {
StopWatch stopWatch = new StopWatch(optType);
stopWatch.start(optType);
Response<R> response = Response.create(JSONUtil.toJsonStr(function.get()));
R r = function.get();
stopWatch.stop();
log.info(stopWatch.shortSummary(TimeUnit.MILLISECONDS));
return response;
return r;
}
@Data
public static class Response<E> {
private Integer code;
@ -76,4 +90,7 @@ public class RpcUtil {
}
}
}