mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish spring-cloud-incubator/spring-cloud-alibaba#500 : dubbo failed to transfer object using Fegin mode
This commit is contained in:
parent
e32b020198
commit
026046fb15
@ -17,14 +17,18 @@
|
||||
package org.springframework.cloud.alibaba.dubbo.openfeign;
|
||||
|
||||
import org.apache.dubbo.rpc.service.GenericService;
|
||||
|
||||
import org.springframework.cloud.alibaba.dubbo.metadata.RestMethodMetadata;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContext;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContextFactory;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.apache.dubbo.common.utils.PojoUtils.realize;
|
||||
|
||||
/**
|
||||
* Dubbo {@link GenericService} for {@link InvocationHandler}
|
||||
*
|
||||
@ -38,11 +42,15 @@ public class DubboInvocationHandler implements InvocationHandler {
|
||||
|
||||
private final DubboGenericServiceExecutionContextFactory contextFactory;
|
||||
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
public DubboInvocationHandler(Map<Method, FeignMethodMetadata> feignMethodMetadataMap,
|
||||
InvocationHandler defaultInvocationHandler,
|
||||
ClassLoader classLoader,
|
||||
DubboGenericServiceExecutionContextFactory contextFactory) {
|
||||
this.feignMethodMetadataMap = feignMethodMetadataMap;
|
||||
this.defaultInvocationHandler = defaultInvocationHandler;
|
||||
this.classLoader = classLoader;
|
||||
this.contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
@ -65,6 +73,15 @@ public class DubboInvocationHandler implements InvocationHandler {
|
||||
String[] parameterTypes = context.getParameterTypes();
|
||||
Object[] parameters = context.getParameters();
|
||||
|
||||
return dubboGenericService.$invoke(methodName, parameterTypes, parameters);
|
||||
Object result = dubboGenericService.$invoke(methodName, parameterTypes, parameters);
|
||||
|
||||
Class<?> returnType = getReturnType(dubboRestMethodMetadata);
|
||||
|
||||
return realize(result, returnType);
|
||||
}
|
||||
|
||||
private Class<?> getReturnType(RestMethodMetadata dubboRestMethodMetadata) {
|
||||
String returnType = dubboRestMethodMetadata.getReturnType();
|
||||
return ClassUtils.resolveClassName(returnType, classLoader);
|
||||
}
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ public class TargeterBeanPostProcessor implements BeanPostProcessor, BeanClassLo
|
||||
Class<?> targetClass = resolveClassName(TARGETER_CLASS_NAME, classLoader);
|
||||
if (targetClass.isAssignableFrom(beanClass)) {
|
||||
return newProxyInstance(classLoader, new Class[]{targetClass},
|
||||
new TargeterInvocationHandler(bean, environment, dubboServiceMetadataRepository,
|
||||
new TargeterInvocationHandler(bean, environment, classLoader, dubboServiceMetadataRepository,
|
||||
dubboGenericServiceFactory, contextFactory));
|
||||
}
|
||||
}
|
||||
|
@ -57,17 +57,22 @@ class TargeterInvocationHandler implements InvocationHandler {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
private final DubboServiceMetadataRepository repository;
|
||||
|
||||
private final DubboGenericServiceFactory dubboGenericServiceFactory;
|
||||
|
||||
private final DubboGenericServiceExecutionContextFactory contextFactory;
|
||||
|
||||
TargeterInvocationHandler(Object bean, Environment environment, DubboServiceMetadataRepository repository,
|
||||
TargeterInvocationHandler(Object bean, Environment environment,
|
||||
ClassLoader classLoader,
|
||||
DubboServiceMetadataRepository repository,
|
||||
DubboGenericServiceFactory dubboGenericServiceFactory,
|
||||
DubboGenericServiceExecutionContextFactory contextFactory) {
|
||||
this.bean = bean;
|
||||
this.environment = environment;
|
||||
this.classLoader = classLoader;
|
||||
this.repository = repository;
|
||||
this.dubboGenericServiceFactory = dubboGenericServiceFactory;
|
||||
this.contextFactory = contextFactory;
|
||||
@ -134,7 +139,7 @@ class TargeterInvocationHandler implements InvocationHandler {
|
||||
InvocationHandler defaultFeignClientInvocationHandler = Proxy.getInvocationHandler(defaultFeignClientProxy);
|
||||
|
||||
DubboInvocationHandler dubboInvocationHandler = new DubboInvocationHandler(feignMethodMetadataMap,
|
||||
defaultFeignClientInvocationHandler, contextFactory);
|
||||
defaultFeignClientInvocationHandler, classLoader, contextFactory);
|
||||
|
||||
return dubboInvocationHandler;
|
||||
}
|
||||
|
@ -192,7 +192,7 @@ public class DubboSpringCloudConsumerBootstrap {
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.requestBodyMap(data, "Hello,World"));
|
||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||
// System.out.println(dubboFeignRestService.requestBody("Hello,World", data));
|
||||
System.out.println(dubboFeignRestService.requestBody("Hello,World", data));
|
||||
// Spring Cloud Open Feign REST Call
|
||||
System.out.println(feignRestService.requestBody("Hello,World", data));
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user