feat: (feature/REQ-3167) 修改client
This commit is contained in:
parent
0c18ff1988
commit
501d69b649
@ -0,0 +1,46 @@
|
||||
package cn.axzo.tyr.client.common.util;
|
||||
|
||||
import cn.axzo.basics.common.util.AssertUtil;
|
||||
import cn.axzo.framework.domain.ServiceException;
|
||||
import cn.axzo.framework.domain.web.result.ApiResult;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Slf4j
|
||||
public class RpcUtil {
|
||||
/**
|
||||
* 常用的RPC请求返回值解析,如果 被请求方 返回非200会抛出异常
|
||||
*/
|
||||
public static <T> ApiResult<T> rpcProcessor(Supplier<ApiResult<T>> supplier, String operationType, Object... param) {
|
||||
|
||||
return rpcProcessorMayThrow(supplier, operationType, (commonResponse) -> {
|
||||
throw new ServiceException(commonResponse.getMsg());
|
||||
}, param);
|
||||
}
|
||||
|
||||
public static <T> ApiResult<T> rpcProcessorMayThrow(Supplier<ApiResult<T>> supplier, String operationType, Consumer<ApiResult<T>> throwConsumer, Object... param) {
|
||||
AssertUtil.notNull(throwConsumer, "自定义的异常处理不可为空");
|
||||
log.info(operationType + "-Param: " + JSONUtil.toJsonStr(param));
|
||||
ApiResult<T> result = null;
|
||||
|
||||
try {
|
||||
result = supplier.get();
|
||||
} catch (Throwable e) {
|
||||
throwConsumer.accept(ApiResult.err(e.getMessage()));
|
||||
}
|
||||
|
||||
log.info(operationType + "-Result: " + JSONUtil.toJsonStr(result));
|
||||
Assert.notNull(result, "服务调用异常");
|
||||
// 200自定义处理
|
||||
if (HttpStatus.HTTP_OK != result.getCode()) {
|
||||
throwConsumer.accept(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@ -2,6 +2,7 @@ package cn.axzo.tyr.client.impl;
|
||||
|
||||
import cn.axzo.framework.auth.domain.TerminalInfo;
|
||||
import cn.axzo.tyr.client.TerminalClient;
|
||||
import cn.axzo.tyr.client.common.util.RpcUtil;
|
||||
import cn.axzo.tyr.client.feign.TerminalApi;
|
||||
import cn.axzo.tyr.client.model.req.GetTerminalInfoReq;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
@ -31,7 +32,8 @@ public class TerminalClientImpl implements TerminalClient {
|
||||
GetTerminalInfoReq getTerminalInfoReq = GetTerminalInfoReq.builder()
|
||||
.terminalCode(terminalCode)
|
||||
.build();
|
||||
return Optional.ofNullable(terminalApi.getTerminalInfo(getTerminalInfoReq).getData());
|
||||
return Optional.ofNullable(RpcUtil.rpcProcessor(() -> terminalApi.getTerminalInfo(getTerminalInfoReq),
|
||||
"查询端的信息", getTerminalInfoReq).getData());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@ -20,7 +20,7 @@ import java.util.List;
|
||||
* @author: ZhanSiHu
|
||||
* @date: 2024/4/3 10:25
|
||||
*/
|
||||
@FeignClient(name = "tyr", url = "${axzo.service.base.tyr:https://pre-api.axzo.cn/tyr}")
|
||||
@FeignClient(name = "tyr", url = "${axzo.service.base.tyr:https://test-api.axzo.cn/tyr}")
|
||||
public interface BaseFeatureResourceApi {
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user