|
@@ -0,0 +1,98 @@
|
|
|
+package com.ywt.biz.common.config.dubbo;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.dubbo.rpc.RpcException;
|
|
|
+import org.apache.dubbo.rpc.service.GenericException;
|
|
|
+import org.apache.dubbo.rpc.service.GenericService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 调用处理
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+@ConditionalOnProperty(name = "switch.dubbo.generic", havingValue = "true")
|
|
|
+public class DubboGenericService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GenericServiceFactory genericServiceFactory;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 实体对象调用
|
|
|
+ * @param interfaceName 接口名称 全限定名
|
|
|
+ * @param methodName 方法名称
|
|
|
+ * @param parameterType 参数名称-请求实体全限定名
|
|
|
+ * @param parameter 请求参数 用map进行包装
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public GenericResponse<Object> genericCall(String interfaceName, String methodName, String parameterType, Map<String, Object> parameter) {
|
|
|
+ try {
|
|
|
+ // 从工厂中获取对应接口的泛化服务
|
|
|
+ GenericService genericService = genericServiceFactory.getGenericService(interfaceName);
|
|
|
+ // 通过invoke进行泛化调用
|
|
|
+ Object object = genericService.$invoke(methodName, new String[]{parameterType}, new Object[]{parameter});
|
|
|
+ // 判断返回类型对象返回一般也是map 暂时不考虑返回
|
|
|
+ /*if (result instanceof Map) {
|
|
|
+ Object responseObj = JacksonUtil.convertMapToObject((Map<String, Object>) result, MyResponse.class);
|
|
|
+ return new GenericResponse<>(true, "Success", responseObj);
|
|
|
+ }*/
|
|
|
+ GenericResponse<Object> response = new GenericResponse<>();
|
|
|
+ response.setSuccess(true);
|
|
|
+ response.setMessage("Success");
|
|
|
+ response.setData(object);
|
|
|
+ return response ;
|
|
|
+ }catch (GenericException | RpcException e){
|
|
|
+ log.error("调用rpc异常,接口名称:{},方法名称:{}",interfaceName,methodName,e);
|
|
|
+ return handleRpcException(e);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("调用系统异常,接口名称:{},方法名称:{}",interfaceName,methodName,e);
|
|
|
+ return handleGenericException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通用调用
|
|
|
+ * @param interfaceName 接口名称 全限定名
|
|
|
+ * @param methodName 方法名称
|
|
|
+ * @param parameterTypes 请求参数类型- 根据传入的参数个数按序进行填充
|
|
|
+ * @param parameters 请求参数- 根据传入的参数个数按序进行填充
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public GenericResponse<Object> genericCall(String interfaceName, String methodName, String[] parameterTypes,Object[] parameters) {
|
|
|
+ try {
|
|
|
+ GenericService genericService = genericServiceFactory.getGenericService(interfaceName);
|
|
|
+ Object object = genericService.$invoke(methodName, parameterTypes, parameters);
|
|
|
+ GenericResponse<Object> response = new GenericResponse<>();
|
|
|
+ response.setSuccess(true);
|
|
|
+ response.setMessage("Success");
|
|
|
+ response.setData(object);
|
|
|
+ return response ;
|
|
|
+ }catch (GenericException | RpcException e){
|
|
|
+ log.error("调用rpc异常,接口名称:{},方法名称:{}",interfaceName,methodName,e);
|
|
|
+ return handleRpcException(e);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("调用系统异常,接口名称:{},方法名称:{}",interfaceName,methodName,e);
|
|
|
+ return handleGenericException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private GenericResponse<Object> handleRpcException(RuntimeException e) {
|
|
|
+ // 此处添加针对RpcException的处理逻辑
|
|
|
+ GenericResponse<Object> response = new GenericResponse<>();
|
|
|
+ response.setSuccess(false);
|
|
|
+ response.setMessage("RPC Exception: " + e.getMessage());
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ private GenericResponse<Object> handleGenericException(Exception e) {
|
|
|
+ // 此处添加针对其他类型异常的处理逻辑
|
|
|
+ GenericResponse<Object> response = new GenericResponse<>();
|
|
|
+ response.setSuccess(false);
|
|
|
+ response.setMessage("Exception: " + e.getMessage());
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+}
|