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;
|
package org.springframework.cloud.alibaba.dubbo.openfeign;
|
||||||
|
|
||||||
import org.apache.dubbo.rpc.service.GenericService;
|
import org.apache.dubbo.rpc.service.GenericService;
|
||||||
|
|
||||||
import org.springframework.cloud.alibaba.dubbo.metadata.RestMethodMetadata;
|
import org.springframework.cloud.alibaba.dubbo.metadata.RestMethodMetadata;
|
||||||
import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContext;
|
import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContext;
|
||||||
import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContextFactory;
|
import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContextFactory;
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationHandler;
|
import java.lang.reflect.InvocationHandler;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.apache.dubbo.common.utils.PojoUtils.realize;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dubbo {@link GenericService} for {@link InvocationHandler}
|
* Dubbo {@link GenericService} for {@link InvocationHandler}
|
||||||
*
|
*
|
||||||
@ -38,11 +42,15 @@ public class DubboInvocationHandler implements InvocationHandler {
|
|||||||
|
|
||||||
private final DubboGenericServiceExecutionContextFactory contextFactory;
|
private final DubboGenericServiceExecutionContextFactory contextFactory;
|
||||||
|
|
||||||
|
private final ClassLoader classLoader;
|
||||||
|
|
||||||
public DubboInvocationHandler(Map<Method, FeignMethodMetadata> feignMethodMetadataMap,
|
public DubboInvocationHandler(Map<Method, FeignMethodMetadata> feignMethodMetadataMap,
|
||||||
InvocationHandler defaultInvocationHandler,
|
InvocationHandler defaultInvocationHandler,
|
||||||
|
ClassLoader classLoader,
|
||||||
DubboGenericServiceExecutionContextFactory contextFactory) {
|
DubboGenericServiceExecutionContextFactory contextFactory) {
|
||||||
this.feignMethodMetadataMap = feignMethodMetadataMap;
|
this.feignMethodMetadataMap = feignMethodMetadataMap;
|
||||||
this.defaultInvocationHandler = defaultInvocationHandler;
|
this.defaultInvocationHandler = defaultInvocationHandler;
|
||||||
|
this.classLoader = classLoader;
|
||||||
this.contextFactory = contextFactory;
|
this.contextFactory = contextFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +73,15 @@ public class DubboInvocationHandler implements InvocationHandler {
|
|||||||
String[] parameterTypes = context.getParameterTypes();
|
String[] parameterTypes = context.getParameterTypes();
|
||||||
Object[] parameters = context.getParameters();
|
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);
|
Class<?> targetClass = resolveClassName(TARGETER_CLASS_NAME, classLoader);
|
||||||
if (targetClass.isAssignableFrom(beanClass)) {
|
if (targetClass.isAssignableFrom(beanClass)) {
|
||||||
return newProxyInstance(classLoader, new Class[]{targetClass},
|
return newProxyInstance(classLoader, new Class[]{targetClass},
|
||||||
new TargeterInvocationHandler(bean, environment, dubboServiceMetadataRepository,
|
new TargeterInvocationHandler(bean, environment, classLoader, dubboServiceMetadataRepository,
|
||||||
dubboGenericServiceFactory, contextFactory));
|
dubboGenericServiceFactory, contextFactory));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,17 +57,22 @@ class TargeterInvocationHandler implements InvocationHandler {
|
|||||||
|
|
||||||
private final Environment environment;
|
private final Environment environment;
|
||||||
|
|
||||||
|
private final ClassLoader classLoader;
|
||||||
|
|
||||||
private final DubboServiceMetadataRepository repository;
|
private final DubboServiceMetadataRepository repository;
|
||||||
|
|
||||||
private final DubboGenericServiceFactory dubboGenericServiceFactory;
|
private final DubboGenericServiceFactory dubboGenericServiceFactory;
|
||||||
|
|
||||||
private final DubboGenericServiceExecutionContextFactory contextFactory;
|
private final DubboGenericServiceExecutionContextFactory contextFactory;
|
||||||
|
|
||||||
TargeterInvocationHandler(Object bean, Environment environment, DubboServiceMetadataRepository repository,
|
TargeterInvocationHandler(Object bean, Environment environment,
|
||||||
|
ClassLoader classLoader,
|
||||||
|
DubboServiceMetadataRepository repository,
|
||||||
DubboGenericServiceFactory dubboGenericServiceFactory,
|
DubboGenericServiceFactory dubboGenericServiceFactory,
|
||||||
DubboGenericServiceExecutionContextFactory contextFactory) {
|
DubboGenericServiceExecutionContextFactory contextFactory) {
|
||||||
this.bean = bean;
|
this.bean = bean;
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
|
this.classLoader = classLoader;
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.dubboGenericServiceFactory = dubboGenericServiceFactory;
|
this.dubboGenericServiceFactory = dubboGenericServiceFactory;
|
||||||
this.contextFactory = contextFactory;
|
this.contextFactory = contextFactory;
|
||||||
@ -134,7 +139,7 @@ class TargeterInvocationHandler implements InvocationHandler {
|
|||||||
InvocationHandler defaultFeignClientInvocationHandler = Proxy.getInvocationHandler(defaultFeignClientProxy);
|
InvocationHandler defaultFeignClientInvocationHandler = Proxy.getInvocationHandler(defaultFeignClientProxy);
|
||||||
|
|
||||||
DubboInvocationHandler dubboInvocationHandler = new DubboInvocationHandler(feignMethodMetadataMap,
|
DubboInvocationHandler dubboInvocationHandler = new DubboInvocationHandler(feignMethodMetadataMap,
|
||||||
defaultFeignClientInvocationHandler, contextFactory);
|
defaultFeignClientInvocationHandler, classLoader, contextFactory);
|
||||||
|
|
||||||
return dubboInvocationHandler;
|
return dubboInvocationHandler;
|
||||||
}
|
}
|
||||||
|
@ -192,7 +192,7 @@ public class DubboSpringCloudConsumerBootstrap {
|
|||||||
// Dubbo Service call
|
// Dubbo Service call
|
||||||
System.out.println(restService.requestBodyMap(data, "Hello,World"));
|
System.out.println(restService.requestBodyMap(data, "Hello,World"));
|
||||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
// 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
|
// Spring Cloud Open Feign REST Call
|
||||||
System.out.println(feignRestService.requestBody("Hello,World", data));
|
System.out.println(feignRestService.requestBody("Hello,World", data));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user