Merge branch 'feature/REQ-1609' into dev
This commit is contained in:
commit
e066dca533
@ -2,13 +2,20 @@ package cn.axzo.pokonyan.util;
|
|||||||
|
|
||||||
|
|
||||||
import cn.axzo.framework.domain.ServiceException;
|
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.date.StopWatch;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.hutool.core.lang.TypeReference;
|
||||||
import cn.hutool.http.HttpStatus;
|
import cn.hutool.http.HttpStatus;
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.concurrent.TimeUnit;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
@ -21,17 +28,26 @@ import java.util.function.Supplier;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class RpcUtil {
|
public class RpcUtil {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 常用的RPC请求返回值解析,如果 被请求方 返回非200会抛出异常
|
* 常用的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 -> {
|
return rpcProcessorMayThrow(supplier, operationType, commonResponse -> {
|
||||||
throw new ServiceException(commonResponse.getMsg());
|
throw new ServiceException(commonResponse.getMsg());
|
||||||
}, param);
|
}, 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
|
* @param param
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static <R,P> R rpcProcessorMayThrow(Supplier<P> supplier, String operationType, Consumer<Response<R>> throwConsumer, Object... param) {
|
public static <T> T rpcProcessorMayThrow(Supplier<CommonResponse<T>> supplier, String operationType, Consumer<CommonResponse<T>> throwConsumer, Object... param) {
|
||||||
Assert.notNull(throwConsumer,"自定义的异常处理不可为空");
|
Assert.notNull(throwConsumer, "自定义的异常处理不可为空");
|
||||||
log.info(operationType + "-Param: " + JSONUtil.toJsonStr(param));
|
log.info(operationType + "-Param: " + JSONUtil.toJsonStr(param));
|
||||||
Response<R> response = printLatency(supplier, operationType);
|
CommonResponse<T> result = printLatency(supplier, operationType);
|
||||||
log.info(operationType + "-Result: " + JSONUtil.toJsonStr(response));
|
log.info(operationType + "-Result: " + JSONUtil.toJsonStr(result));
|
||||||
|
Assert.notNull(result, "服务调用异常");
|
||||||
Assert.notNull(response, "服务调用异常");
|
|
||||||
// 200自定义处理
|
// 200自定义处理
|
||||||
if (HttpStatus.HTTP_OK != response.getCode()) {
|
if (HttpStatus.HTTP_OK != result.getCode()) {
|
||||||
throwConsumer.accept(response);
|
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 stopWatch = new StopWatch(optType);
|
||||||
stopWatch.start(optType);
|
stopWatch.start(optType);
|
||||||
Response<R> response = Response.create(JSONUtil.toJsonStr(function.get()));
|
R r = function.get();
|
||||||
stopWatch.stop();
|
stopWatch.stop();
|
||||||
log.info(stopWatch.shortSummary(TimeUnit.MILLISECONDS));
|
log.info(stopWatch.shortSummary(TimeUnit.MILLISECONDS));
|
||||||
return response;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public static class Response<E> {
|
public static class Response<E> {
|
||||||
private Integer code;
|
private Integer code;
|
||||||
@ -76,4 +90,7 @@ public class RpcUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user