From 1e0a43dde1a7e9362f4ee0143b8e28a8c9ab5e20 Mon Sep 17 00:00:00 2001 From: TanJ Date: Fri, 17 Nov 2023 16:56:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(1609):=20=E7=BB=9F=E4=B8=80response?= =?UTF-8?q?=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/axzo/pokonyan/util/RpcUtil.java | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/main/java/cn/axzo/pokonyan/util/RpcUtil.java b/src/main/java/cn/axzo/pokonyan/util/RpcUtil.java index d6fa9a0..ce8ce75 100644 --- a/src/main/java/cn/axzo/pokonyan/util/RpcUtil.java +++ b/src/main/java/cn/axzo/pokonyan/util/RpcUtil.java @@ -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 T rpcProcessor(Supplier supplier, String operationType, Object... param) { + public static T rpcCommonProcessor(Supplier> supplier, String operationType, Object... param) { return rpcProcessorMayThrow(supplier, operationType, commonResponse -> { throw new ServiceException(commonResponse.getMsg()); }, param); } + public static T rpcApiResultProcessor(Supplier> supplier, String operationType, Object... param) { + log.info(operationType + "-Param: " + JSONUtil.toJsonStr(param)); + ApiResult 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 rpcProcessorMayThrow(Supplier

supplier, String operationType, Consumer> throwConsumer, Object... param) { - Assert.notNull(throwConsumer,"自定义的异常处理不可为空"); + public static T rpcProcessorMayThrow(Supplier> supplier, String operationType, Consumer> throwConsumer, Object... param) { + Assert.notNull(throwConsumer, "自定义的异常处理不可为空"); log.info(operationType + "-Param: " + JSONUtil.toJsonStr(param)); - Response response = printLatency(supplier, operationType); - log.info(operationType + "-Result: " + JSONUtil.toJsonStr(response)); - - Assert.notNull(response, "服务调用异常"); + CommonResponse 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 Response printLatency(Supplier

function, String optType) { + public static R printLatency(Supplier< R> function,String optType) { StopWatch stopWatch = new StopWatch(optType); stopWatch.start(optType); - Response 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 { private Integer code; @@ -76,4 +90,7 @@ public class RpcUtil { } } + + + }