R 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, "服务调用异常");
+ // 200自定义处理
+ if (HttpStatus.HTTP_OK != response.getCode()) {
+ throwConsumer.accept(response);
+ }
+ return response.getData();
+ }
+ public static Response printLatency(Supplier function, String optType) {
+ StopWatch stopWatch = new StopWatch(optType);
+ stopWatch.start(optType);
+ Response response = Response.create(JSONUtil.toJsonStr(function.get()));
+ stopWatch.stop();
+ log.info(stopWatch.shortSummary(TimeUnit.MILLISECONDS));
+ return response;
+ }
+
+
+ @Data
+ public static class Response {
+ private Integer code;
+ private String msg;
+ private E data;
+
+ public static Response create(String data) {
+ return JSONUtil.toBean(data, Response.class);
+ }
+ }
+
+}