performance tuning: save rpcinvocation allocation (#4215)

This commit is contained in:
ken.lj 2019-06-03 13:03:29 +08:00 committed by GitHub
parent d767488800
commit b5a078f186
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
58 changed files with 698 additions and 586 deletions

View File

@ -38,6 +38,11 @@ public class MockDirInvocation implements Invocation {
return "echo";
}
@Override
public String getServiceName() {
return "DemoService";
}
public Class<?>[] getParameterTypes() {
return new Class[]{String.class};
}

View File

@ -89,7 +89,7 @@ public class ConditionRouterTest {
public void testRoute_matchFilter() {
List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf(
"dubbo://10.20.3.3:20880/com.foo.BarService?default.serialization=fastjson"));
"dubbo://10.10.3.3:20880/com.foo.BarService?default.serialization=fastjson"));
Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost()
+ ":20880/com.foo.BarService"));
Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost()
@ -102,19 +102,19 @@ public class ConditionRouterTest {
System.err.println(invoker3.getUrl().getAddress());
Router router1 = new ConditionRouterFactory().getRouter(getRouteUrl(
"host = " + NetUtils.getLocalHost() + " => " + " host = 10.20.3.3").addParameter(FORCE_KEY,
"host = " + NetUtils.getLocalHost() + " => " + " host = 10.10.3.3").addParameter(FORCE_KEY,
String.valueOf(true)));
Router router2 = new ConditionRouterFactory().getRouter(getRouteUrl(
"host = " + NetUtils.getLocalHost() + " => " + " host = 10.20.3.* & host != 10.20.3.3").addParameter(
"host = " + NetUtils.getLocalHost() + " => " + " host = 10.10.3.* & host != 10.10.3.3").addParameter(
FORCE_KEY, String.valueOf(true)));
Router router3 = new ConditionRouterFactory().getRouter(getRouteUrl(
"host = " + NetUtils.getLocalHost() + " => " + " host = 10.20.3.3 & host != 10.20.3.3").addParameter(
"host = " + NetUtils.getLocalHost() + " => " + " host = 10.10.3.3 & host != 10.10.3.3").addParameter(
FORCE_KEY, String.valueOf(true)));
Router router4 = new ConditionRouterFactory().getRouter(getRouteUrl(
"host = " + NetUtils.getLocalHost() + " => " + " host = 10.20.3.2,10.20.3.3,10.20.3.4").addParameter(
"host = " + NetUtils.getLocalHost() + " => " + " host = 10.10.3.2,10.10.3.3,10.10.3.4").addParameter(
FORCE_KEY, String.valueOf(true)));
Router router5 = new ConditionRouterFactory().getRouter(getRouteUrl(
"host = " + NetUtils.getLocalHost() + " => " + " host != 10.20.3.3").addParameter(FORCE_KEY,
"host = " + NetUtils.getLocalHost() + " => " + " host != 10.10.3.3").addParameter(FORCE_KEY,
String.valueOf(true)));
Router router6 = new ConditionRouterFactory().getRouter(getRouteUrl(
"host = " + NetUtils.getLocalHost() + " => " + " serialization = fastjson").addParameter(
@ -138,7 +138,7 @@ public class ConditionRouterTest {
@Test
public void testRoute_methodRoute() {
Invocation invocation = new RpcInvocation("getFoo", new Class<?>[0], new Object[0]);
Invocation invocation = new RpcInvocation("getFoo", "com.foo.BarService", new Class<?>[0], new Object[0]);
// More than one methods, mismatch
Router router = new ConditionRouterFactory().getRouter(getRouteUrl("methods=getFoo => host = 1.2.3.4"));
boolean matchWhen = ((ConditionRouter) router).matchWhen(
@ -162,7 +162,7 @@ public class ConditionRouterTest {
Assertions.assertTrue(matchWhen);
// Test filter condition
List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService"));
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService"));
Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost()
+ ":20880/com.foo.BarService"));
Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost()
@ -172,14 +172,14 @@ public class ConditionRouterTest {
invokers.add(invoker3);
Router router4 = new ConditionRouterFactory().getRouter(getRouteUrl(
"host = " + NetUtils.getLocalHost() + " & methods = getFoo => " + " host = 10.20.3.3").addParameter(
"host = " + NetUtils.getLocalHost() + " & methods = getFoo => " + " host = 10.10.3.3").addParameter(
FORCE_KEY, String.valueOf(true)));
List<Invoker<String>> filteredInvokers1 = router4.route(invokers,
URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), invocation);
Assertions.assertEquals(1, filteredInvokers1.size());
Router router5 = new ConditionRouterFactory().getRouter(getRouteUrl(
"host = " + NetUtils.getLocalHost() + " & methods = unvalidmethod => " + " host = 10.20.3.3")
"host = " + NetUtils.getLocalHost() + " & methods = unvalidmethod => " + " host = 10.10.3.3")
.addParameter(FORCE_KEY, String.valueOf(true)));
List<Invoker<String>> filteredInvokers2 = router5.route(invokers,
URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), invocation);
@ -224,7 +224,7 @@ public class ConditionRouterTest {
public void testRoute_HostFilter() {
Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = " + NetUtils.getLocalHost()));
List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService"));
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService"));
Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
invokers.add(invoker1);
@ -240,7 +240,7 @@ public class ConditionRouterTest {
public void testRoute_Empty_HostFilter() {
Router router = new ConditionRouterFactory().getRouter(getRouteUrl(" => " + " host = " + NetUtils.getLocalHost()));
List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService"));
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService"));
Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
invokers.add(invoker1);
@ -256,7 +256,7 @@ public class ConditionRouterTest {
public void testRoute_False_HostFilter() {
Router router = new ConditionRouterFactory().getRouter(getRouteUrl("true => " + " host = " + NetUtils.getLocalHost()));
List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService"));
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService"));
Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
invokers.add(invoker1);
@ -272,7 +272,7 @@ public class ConditionRouterTest {
public void testRoute_Placeholder() {
Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = $host"));
List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService"));
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService"));
Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
invokers.add(invoker1);
@ -288,7 +288,7 @@ public class ConditionRouterTest {
public void testRoute_NoForce() {
Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = 1.2.3.4"));
List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService"));
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService"));
Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
invokers.add(invoker1);
@ -302,7 +302,7 @@ public class ConditionRouterTest {
public void testRoute_Force() {
Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = 1.2.3.4").addParameter(FORCE_KEY, String.valueOf(true)));
List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService"));
Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.10.3.3:20880/com.foo.BarService"));
Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
invokers.add(invoker1);

View File

@ -47,10 +47,10 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.CLUSTER_AVAILABLE_CHECK_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.INVOCATION_NEED_MOCK;
import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@ -493,21 +493,21 @@ public class AbstractClusterInvokerTest {
Directory<DemoService> directory = new StaticDirectory<DemoService>(invokers);
FailoverClusterInvoker<DemoService> failoverClusterInvoker = new FailoverClusterInvoker<DemoService>(directory);
try {
failoverClusterInvoker.invoke(new RpcInvocation("sayHello", new Class<?>[0], new Object[0]));
failoverClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), new Class<?>[0], new Object[0]));
Assertions.fail();
} catch (RpcException e) {
Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
}
ForkingClusterInvoker<DemoService> forkingClusterInvoker = new ForkingClusterInvoker<DemoService>(directory);
try {
forkingClusterInvoker.invoke(new RpcInvocation("sayHello", new Class<?>[0], new Object[0]));
forkingClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), new Class<?>[0], new Object[0]));
Assertions.fail();
} catch (RpcException e) {
Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
}
FailfastClusterInvoker<DemoService> failfastClusterInvoker = new FailfastClusterInvoker<DemoService>(directory);
try {
failfastClusterInvoker.invoke(new RpcInvocation("sayHello", new Class<?>[0], new Object[0]));
failfastClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), new Class<?>[0], new Object[0]));
Assertions.fail();
} catch (RpcException e) {
Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());

View File

@ -38,6 +38,11 @@ public interface Invocation extends org.apache.dubbo.rpc.Invocation {
}
@Override
default String getServiceName() {
return null;
}
class CompatibleInvocation implements Invocation, org.apache.dubbo.rpc.Invocation {
private org.apache.dubbo.rpc.Invocation delegate;

View File

@ -21,7 +21,6 @@ import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.rpc.Invocation;
import java.lang.reflect.Type;
import java.util.Map;
/**
* 2019-04-18
@ -76,8 +75,4 @@ public class RpcUtils extends org.apache.dubbo.rpc.support.RpcUtils {
public static boolean isOneway(URL url, Invocation inv) {
return org.apache.dubbo.rpc.support.RpcUtils.isOneway(url.getOriginalURL(), inv);
}
public static Map<String, String> getNecessaryAttachments(Invocation inv) {
return org.apache.dubbo.rpc.support.RpcUtils.getNecessaryAttachments(inv);
}
}

View File

@ -17,7 +17,7 @@
package org.apache.dubbo.config;
import org.apache.dubbo.rpc.model.ConsumerMethodModel;
import org.apache.dubbo.rpc.model.ConsumerModel;
import org.apache.dubbo.service.Person;
import com.alibaba.dubbo.config.ArgumentConfig;
@ -113,7 +113,7 @@ public class MethodConfigTest {
methodConfig.setOninvokeMethod("setName");
methodConfig.setOninvoke(new Person());
ConsumerMethodModel.AsyncMethodInfo methodInfo = org.apache.dubbo.config.MethodConfig.convertMethodConfig2AyncInfo(methodConfig);
ConsumerModel.AsyncMethodInfo methodInfo = org.apache.dubbo.config.MethodConfig.convertMethodConfig2AyncInfo(methodConfig);
assertEquals(methodInfo.getOninvokeMethod(), Person.class.getMethod("setName", String.class));
}

View File

@ -44,6 +44,11 @@ public class MockInvocation implements Invocation {
return "echo";
}
@Override
public String getServiceName() {
return "DemoService";
}
public Class<?>[] getParameterTypes() {
return new Class[]{String.class};
}

View File

@ -30,7 +30,7 @@ import org.apache.dubbo.common.utils.MethodUtils;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.support.Parameter;
import org.apache.dubbo.rpc.model.ConsumerMethodModel;
import org.apache.dubbo.rpc.model.ConsumerModel;
import java.io.Serializable;
import java.lang.reflect.Method;
@ -245,7 +245,7 @@ public abstract class AbstractConfig implements Serializable {
}
}
protected static ConsumerMethodModel.AsyncMethodInfo convertMethodConfig2AyncInfo(MethodConfig methodConfig) {
protected static ConsumerModel.AsyncMethodInfo convertMethodConfig2AyncInfo(MethodConfig methodConfig) {
if (methodConfig == null || (methodConfig.getOninvoke() == null && methodConfig.getOnreturn() == null && methodConfig.getOnthrow() == null)) {
return null;
}
@ -255,7 +255,7 @@ public abstract class AbstractConfig implements Serializable {
throw new IllegalStateException("method config error : return attribute must be set true when onreturn or onthrow has been set.");
}
ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = new ConsumerMethodModel.AsyncMethodInfo();
ConsumerModel.AsyncMethodInfo asyncMethodInfo = new ConsumerModel.AsyncMethodInfo();
asyncMethodInfo.setOninvokeInstance(methodConfig.getOninvoke());
asyncMethodInfo.setOnreturnInstance(methodConfig.getOnreturn());

View File

@ -46,7 +46,6 @@ import org.apache.dubbo.rpc.support.ProtocolUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -56,24 +55,24 @@ import java.util.Map;
import java.util.Properties;
import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SEPARATOR;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO;
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SEMICOLON_SPLIT_PATTERN;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY;
import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY;
import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY;
import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY;
import static org.apache.dubbo.registry.Constants.CONSUMER_PROTOCOL;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_PROTOCOL;
import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL;
import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost;
import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY;
import static org.apache.dubbo.registry.Constants.CONSUMER_PROTOCOL;
import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY;
import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL;
import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY;
/**
* ReferenceConfig
@ -304,7 +303,7 @@ public class ReferenceConfig<T> extends AbstractReferenceConfig {
appendParameters(map, this);
Map<String, Object> attributes = null;
if (CollectionUtils.isNotEmpty(methods)) {
attributes = new HashMap<String, Object>();
attributes = new HashMap<>();
for (MethodConfig methodConfig : methods) {
appendParameters(map, methodConfig, methodConfig.getName());
String retryKey = methodConfig.getName() + ".retry";
@ -334,17 +333,7 @@ public class ReferenceConfig<T> extends AbstractReferenceConfig {
}
private ConsumerModel buildConsumerModel(String serviceKey, Map<String, Object> attributes) {
Method[] methods = interfaceClass.getMethods();
Class serviceInterface = interfaceClass;
if (interfaceClass == GenericService.class) {
try {
serviceInterface = Class.forName(interfaceName);
methods = serviceInterface.getMethods();
} catch (ClassNotFoundException e) {
methods = interfaceClass.getMethods();
}
}
return new ConsumerModel(serviceKey, serviceInterface, ref, methods, attributes);
return new ConsumerModel(serviceKey, interfaceClass, ref, ApplicationModel.registerServiceModel(interfaceClass), attributes);
}
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})

View File

@ -65,33 +65,33 @@ import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_IP_TO_BIND;
import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_IP_TO_BIND;
import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY;
import static org.apache.dubbo.config.Constants.DUBBO_PORT_TO_BIND;
import static org.apache.dubbo.config.Constants.DUBBO_PORT_TO_REGISTRY;
import static org.apache.dubbo.rpc.cluster.Constants.EXPORT_KEY;
import static org.apache.dubbo.config.Constants.MULTICAST;
import static org.apache.dubbo.config.Constants.PROTOCOLS_SUFFIX;
import static org.apache.dubbo.rpc.Constants.SCOPE_KEY;
import static org.apache.dubbo.rpc.Constants.SCOPE_LOCAL;
import static org.apache.dubbo.config.Constants.SCOPE_NONE;
import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE;
import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_KEY;
import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL;
import static org.apache.dubbo.rpc.Constants.PROXY_KEY;
import static org.apache.dubbo.rpc.Constants.TOKEN_KEY;
import static org.apache.dubbo.common.utils.NetUtils.getAvailablePort;
import static org.apache.dubbo.common.utils.NetUtils.getLocalHost;
import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost;
import static org.apache.dubbo.common.utils.NetUtils.isInvalidPort;
import static org.apache.dubbo.config.Constants.DUBBO_IP_TO_REGISTRY;
import static org.apache.dubbo.config.Constants.DUBBO_PORT_TO_BIND;
import static org.apache.dubbo.config.Constants.DUBBO_PORT_TO_REGISTRY;
import static org.apache.dubbo.config.Constants.MULTICAST;
import static org.apache.dubbo.config.Constants.PROTOCOLS_SUFFIX;
import static org.apache.dubbo.config.Constants.SCOPE_NONE;
import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL;
import static org.apache.dubbo.rpc.Constants.PROXY_KEY;
import static org.apache.dubbo.rpc.Constants.SCOPE_KEY;
import static org.apache.dubbo.rpc.Constants.SCOPE_LOCAL;
import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE;
import static org.apache.dubbo.rpc.Constants.TOKEN_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.EXPORT_KEY;
/**
* ServiceConfig
@ -452,7 +452,7 @@ public class ServiceConfig<T> extends AbstractServiceConfig {
List<URL> registryURLs = loadRegistries(true);
for (ProtocolConfig protocolConfig : protocols) {
String pathKey = URL.buildKey(getContextPath(protocolConfig).map(p -> p + "/" + path).orElse(path), group, version);
ProviderModel providerModel = new ProviderModel(pathKey, ref, interfaceClass);
ProviderModel providerModel = new ProviderModel(pathKey, ref, ApplicationModel.registerServiceModel(interfaceClass));
ApplicationModel.initProviderModel(pathKey, providerModel);
doExportUrlsFor1Protocol(protocolConfig, registryURLs);
}

View File

@ -132,7 +132,7 @@ public class CacheTest {
parameters.put("findCache.cache", "threadlocal");
URL url = new URL("dubbo", "127.0.0.1", 29582, "org.apache.dubbo.config.cache.CacheService", parameters);
Invocation invocation = new RpcInvocation("findCache", new Class[]{String.class}, new String[]{"0"}, null, null);
Invocation invocation = new RpcInvocation("findCache", "org.apache.dubbo.config.cache.CacheService", new Class[]{String.class}, new String[]{"0"}, null, null);
Cache cache = cacheFactory.getCache(url, invocation);
assertTrue(cache instanceof ThreadLocalCache);

View File

@ -16,8 +16,14 @@
*/
package org.apache.dubbo.demo;
import java.util.concurrent.CompletableFuture;
public interface DemoService {
String sayHello(String name);
default CompletableFuture<String> sayHelloAsync(String name) {
return CompletableFuture.completedFuture(sayHello(name));
}
}

View File

@ -20,16 +20,18 @@ import org.apache.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.concurrent.CompletableFuture;
public class Application {
/**
* In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before
* launch the application
*/
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
context.start();
DemoService demoService = context.getBean("demoService", DemoService.class);
String hello = demoService.sayHello("world");
System.out.println("result: " + hello);
CompletableFuture<String> hello = demoService.sayHelloAsync("world");
System.out.println("result: " + hello.get());
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.dubbo.demo.provider;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
@ -25,6 +27,7 @@ public class Application {
*/
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");
System.err.println(ReflectUtils.getDesc(new Class<?>[]{String.class, String[].class, Object[].class}));
context.start();
System.in.read();
}

View File

@ -22,6 +22,8 @@ import org.apache.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CompletableFuture;
public class DemoServiceImpl implements DemoService {
private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
@ -31,4 +33,16 @@ public class DemoServiceImpl implements DemoService {
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
@Override
public CompletableFuture<String> sayHelloAsync(String name) {
CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "async result";
});
return cf;
}
}

View File

@ -40,8 +40,8 @@ import java.util.List;
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@ -118,7 +118,7 @@ public class MonitorFilterTest {
public void testFilter() throws Exception {
MonitorFilter monitorFilter = new MonitorFilter();
monitorFilter.setMonitorFactory(monitorFactory);
Invocation invocation = new RpcInvocation("aaa", new Class<?>[0], new Object[0]);
Invocation invocation = new RpcInvocation("aaa", MonitorService.class.getName(), new Class<?>[0], new Object[0]);
RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
Result result = monitorFilter.invoke(serviceInvoker, invocation);
result.thenApplyWithContext((r) -> {
@ -145,7 +145,7 @@ public class MonitorFilterTest {
MonitorFilter monitorFilter = new MonitorFilter();
MonitorFactory mockMonitorFactory = mock(MonitorFactory.class);
monitorFilter.setMonitorFactory(mockMonitorFactory);
Invocation invocation = new RpcInvocation("aaa", new Class<?>[0], new Object[0]);
Invocation invocation = new RpcInvocation("aaa", MonitorService.class.getName(), new Class<?>[0], new Object[0]);
Invoker invoker = mock(Invoker.class);
given(invoker.getUrl()).willReturn(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880?" + APPLICATION_KEY + "=abc&" + SIDE_KEY + "=" + CONSUMER_SIDE));
@ -158,7 +158,7 @@ public class MonitorFilterTest {
public void testGenericFilter() throws Exception {
MonitorFilter monitorFilter = new MonitorFilter();
monitorFilter.setMonitorFactory(monitorFactory);
Invocation invocation = new RpcInvocation("$invoke", new Class<?>[]{String.class, String[].class, Object[].class}, new Object[]{"xxx", new String[]{}, new Object[]{}});
Invocation invocation = new RpcInvocation("$invoke", MonitorService.class.getName(), new Class<?>[]{String.class, String[].class, Object[].class}, new Object[]{"xxx", new String[]{}, new Object[]{}});
RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
Result result = monitorFilter.invoke(serviceInvoker, invocation);
result.thenApplyWithContext((r) -> {
@ -189,7 +189,7 @@ public class MonitorFilterTest {
monitorFilter.setMonitorFactory(mockMonitorFactory);
given(mockMonitorFactory.getMonitor(any(URL.class))).willReturn(mockMonitor);
Invocation invocation = new RpcInvocation("aaa", new Class<?>[0], new Object[0]);
Invocation invocation = new RpcInvocation("aaa", MonitorService.class.getName(), new Class<?>[0], new Object[0]);
monitorFilter.invoke(serviceInvoker, invocation);
}

View File

@ -25,9 +25,10 @@ import org.apache.dubbo.monitor.support.AbstractMonitorFactory;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.ProxyFactory;
import org.apache.dubbo.rpc.model.ApplicationModel;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.remoting.Constants.CHECK_KEY;
import static org.apache.dubbo.rpc.Constants.REFERENCE_FILTER_KEY;
@ -63,6 +64,7 @@ public class DubboMonitorFactory extends AbstractMonitorFactory {
}
urlBuilder.addParameters(CHECK_KEY, String.valueOf(false),
REFERENCE_FILTER_KEY, filter + "-monitor");
ApplicationModel.registerServiceModel(MonitorService.class);
Invoker<MonitorService> monitorInvoker = protocol.refer(MonitorService.class, urlBuilder.build());
MonitorService monitorService = proxyFactory.getProxy(monitorInvoker);
return new DubboMonitor(monitorInvoker, monitorService);

View File

@ -31,6 +31,7 @@ import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.support.RpcUtils;
import com.alibaba.fastjson.JSON;
@ -88,6 +89,7 @@ public class MetricsFilter implements Filter {
Invoker<MetricsService> metricsInvoker = initMetricsInvoker();
try {
ApplicationModel.registerServiceModel(MetricsService.class);
protocol.export(metricsInvoker);
} catch (RuntimeException e) {
logger.error("Metrics Service need to be configured" +

View File

@ -112,7 +112,7 @@ public class MetricsFilterTest {
IMetricManager metricManager = MetricManager.getIMetricManager();
metricManager.clear();
MetricsFilter metricsFilter = new MetricsFilter();
Invocation invocation = new RpcInvocation("sayName", new Class<?>[]{Integer.class}, new Object[0]);
Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), new Class<?>[]{Integer.class}, new Object[0]);
RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, CONSUMER_SIDE));
for (int i = 0; i < 100; i++) {
@ -137,7 +137,7 @@ public class MetricsFilterTest {
IMetricManager metricManager = MetricManager.getIMetricManager();
metricManager.clear();
MetricsFilter metricsFilter = new MetricsFilter();
Invocation invocation = new RpcInvocation("timeoutException", null, null);
Invocation invocation = new RpcInvocation("timeoutException", DemoService.class.getName(), null, null);
RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
RpcContext.getContext().setUrl(timeoutInvoker.getUrl().addParameter(SIDE_KEY, CONSUMER_SIDE)
.addParameter(TIMEOUT_KEY, 300));
@ -167,7 +167,7 @@ public class MetricsFilterTest {
IMetricManager metricManager = MetricManager.getIMetricManager();
metricManager.clear();
MetricsFilter metricsFilter = new MetricsFilter();
Invocation invocation = new RpcInvocation("sayName", new Class<?>[0], new Object[0]);
Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), new Class<?>[0], new Object[0]);
RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER));
for (int i = 0; i < 100; i++) {
@ -191,7 +191,7 @@ public class MetricsFilterTest {
IMetricManager metricManager = MetricManager.getIMetricManager();
metricManager.clear();
MetricsFilter metricsFilter = new MetricsFilter();
Invocation invocation = new RpcInvocation("sayName", new Class<?>[0], new Object[0]);
Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), new Class<?>[0], new Object[0]);
RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER_SIDE)
.addParameter(TIMEOUT_KEY, 300));
@ -206,7 +206,7 @@ public class MetricsFilterTest {
Protocol protocol = new DubboProtocol();
URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":20880/" + MetricsService.class.getName());
Invoker<MetricsService> invoker = protocol.refer(MetricsService.class, url);
invocation = new RpcInvocation("getMetricsByGroup", new Class<?>[]{String.class}, new Object[]{DUBBO_GROUP});
invocation = new RpcInvocation("getMetricsByGroup", DemoService.class.getName(), new Class<?>[]{String.class}, new Object[]{DUBBO_GROUP});
try {
Thread.sleep(5000);
} catch (Exception e) {
@ -235,8 +235,8 @@ public class MetricsFilterTest {
IMetricManager metricManager = MetricManager.getIMetricManager();
metricManager.clear();
MetricsFilter metricsFilter = new MetricsFilter();
Invocation sayNameInvocation = new RpcInvocation("sayName", new Class<?>[0], new Object[0]);
Invocation echoInvocation = new RpcInvocation("echo", new Class<?>[]{Integer.class}, new Integer[]{1});
Invocation sayNameInvocation = new RpcInvocation("sayName", DemoService.class.getName(), new Class<?>[0], new Object[0]);
Invocation echoInvocation = new RpcInvocation("echo", DemoService.class.getName(), new Class<?>[]{Integer.class}, new Integer[]{1});
RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER_SIDE)
.addParameter(TIMEOUT_KEY, 300));
@ -258,7 +258,7 @@ public class MetricsFilterTest {
Protocol protocol = new DubboProtocol();
URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":20880/" + MetricsService.class.getName());
Invoker<MetricsService> invoker = protocol.refer(MetricsService.class, url);
Invocation invocation = new RpcInvocation("getMetricsByGroup", new Class<?>[]{String.class}, new Object[]{DUBBO_GROUP});
Invocation invocation = new RpcInvocation("getMetricsByGroup", DemoService.class.getName(), new Class<?>[]{String.class}, new Object[]{DUBBO_GROUP});
try {
Thread.sleep(15000);
} catch (Exception e) {

View File

@ -57,7 +57,7 @@ public class Ls implements BaseCommand {
//Content
for (ProviderModel providerModel : providerModelList) {
tTable.addRow(providerModel.getServiceName(), isRegistered(providerModel.getServiceName()) ? "Y" : "N");
tTable.addRow(providerModel.getServiceKey(), isRegistered(providerModel.getServiceKey()) ? "Y" : "N");
}
stringBuilder.append(tTable.rendering());
@ -80,7 +80,7 @@ public class Ls implements BaseCommand {
//Content
//TODO to calculate consumerAddressNum
for (ConsumerModel consumerModel : consumerModelList) {
tTable.addRow(consumerModel.getServiceName(), getConsumerAddressNum(consumerModel.getServiceName()));
tTable.addRow(consumerModel.getServiceKey(), getConsumerAddressNum(consumerModel.getServiceKey()));
}
stringBuilder.append(tTable.rendering());

View File

@ -51,9 +51,9 @@ public class Offline implements BaseCommand {
Collection<ProviderModel> providerModelList = ApplicationModel.allProviderModels();
for (ProviderModel providerModel : providerModelList) {
if (providerModel.getServiceName().matches(servicePattern)) {
if (providerModel.getServiceKey().matches(servicePattern)) {
hasService = true;
Set<ProviderInvokerWrapper> providerInvokerWrapperSet = ProviderConsumerRegTable.getProviderInvoker(providerModel.getServiceName());
Set<ProviderInvokerWrapper> providerInvokerWrapperSet = ProviderConsumerRegTable.getProviderInvoker(providerModel.getServiceKey());
for (ProviderInvokerWrapper providerInvokerWrapper : providerInvokerWrapperSet) {
if (!providerInvokerWrapper.isReg()) {
continue;

View File

@ -53,9 +53,9 @@ public class Online implements BaseCommand {
Collection<ProviderModel> providerModelList = ApplicationModel.allProviderModels();
for (ProviderModel providerModel : providerModelList) {
if (providerModel.getServiceName().matches(servicePattern)) {
if (providerModel.getServiceKey().matches(servicePattern)) {
hasService = true;
Set<ProviderInvokerWrapper> providerInvokerWrapperSet = ProviderConsumerRegTable.getProviderInvoker(providerModel.getServiceName());
Set<ProviderInvokerWrapper> providerInvokerWrapperSet = ProviderConsumerRegTable.getProviderInvoker(providerModel.getServiceKey());
for (ProviderInvokerWrapper providerInvokerWrapper : providerInvokerWrapperSet) {
if (providerInvokerWrapper.isReg()) {
continue;

View File

@ -25,14 +25,15 @@ import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ConsumerModel;
import org.apache.dubbo.rpc.model.ProviderModel;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.Map;
import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -40,9 +41,9 @@ public class LsTest {
@Test
public void testExecute() throws Exception {
ConsumerModel consumerModel = mock(ConsumerModel.class);
when(consumerModel.getServiceName()).thenReturn("org.apache.dubbo.FooService");
when(consumerModel.getServiceKey()).thenReturn("org.apache.dubbo.FooService");
ProviderModel providerModel = mock(ProviderModel.class);
when(providerModel.getServiceName()).thenReturn("org.apache.dubbo.BarService");
when(providerModel.getServiceKey()).thenReturn("org.apache.dubbo.BarService");
ApplicationModel.initConsumerModel("org.apache.dubbo.FooService", consumerModel);
ApplicationModel.initProviderModel("org.apache.dubbo.BarService", providerModel);

View File

@ -24,13 +24,14 @@ import org.apache.dubbo.registry.support.ProviderInvokerWrapper;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ProviderModel;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -38,7 +39,7 @@ public class OfflineTest {
@Test
public void testExecute() throws Exception {
ProviderModel providerModel = mock(ProviderModel.class);
when(providerModel.getServiceName()).thenReturn("org.apache.dubbo.BarService");
when(providerModel.getServiceKey()).thenReturn("org.apache.dubbo.BarService");
ApplicationModel.initProviderModel("org.apache.dubbo.BarService", providerModel);
Invoker providerInvoker = mock(Invoker.class);

View File

@ -24,11 +24,12 @@ import org.apache.dubbo.registry.support.ProviderInvokerWrapper;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ProviderModel;
import org.junit.jupiter.api.Test;
import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -37,7 +38,7 @@ public class OnlineTest {
@Test
public void testExecute() throws Exception {
ProviderModel providerModel = mock(ProviderModel.class);
when(providerModel.getServiceName()).thenReturn("org.apache.dubbo.BarService");
when(providerModel.getServiceKey()).thenReturn("org.apache.dubbo.BarService");
ApplicationModel.initProviderModel("org.apache.dubbo.BarService", providerModel);
Invoker providerInvoker = mock(Invoker.class);

View File

@ -34,6 +34,7 @@ import org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance;
import org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance;
import org.apache.dubbo.rpc.cluster.router.script.ScriptRouterFactory;
import org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker;
import org.apache.dubbo.rpc.service.GenericService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@ -49,19 +50,12 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import static org.apache.dubbo.rpc.cluster.Constants.INVOCATION_NEED_MOCK;
import static org.apache.dubbo.rpc.cluster.Constants.LOADBALANCE_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.MOCK_PROTOCOL;
import static org.apache.dubbo.rpc.cluster.Constants.ROUTER_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.RULE_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.TYPE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.DISABLED_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
@ -70,6 +64,13 @@ import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGO
import static org.apache.dubbo.common.constants.RegistryConstants.ROUTE_PROTOCOL;
import static org.apache.dubbo.rpc.Constants.$INVOKE;
import static org.apache.dubbo.rpc.Constants.MOCK_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.INVOCATION_NEED_MOCK;
import static org.apache.dubbo.rpc.cluster.Constants.LOADBALANCE_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.MOCK_PROTOCOL;
import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.ROUTER_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.RULE_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.TYPE_KEY;
import static org.junit.jupiter.api.Assertions.fail;
@SuppressWarnings({"rawtypes", "unchecked"})
@ -482,7 +483,7 @@ public class RegistryDirectoryTest {
registryDirectory.notify(serviceUrls);
// Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException;
invocation = new RpcInvocation($INVOKE, new Class[]{String.class, String[].class, Object[].class},
invocation = new RpcInvocation($INVOKE, GenericService.class.getName(), new Class[]{String.class, String[].class, Object[].class},
new Object[]{"getXXX1", "", new Object[]{}});
List<Invoker> invokers = registryDirectory.list(invocation);
@ -511,7 +512,7 @@ public class RegistryDirectoryTest {
registryDirectory.notify(serviceUrls);
invocation = new RpcInvocation($INVOKE,
invocation = new RpcInvocation($INVOKE, GenericService.class.getName(),
new Class[]{String.class, String[].class, Object[].class},
new Object[]{"getXXX1", new String[]{"Enum"}, new Object[]{Param.MORGAN}});

View File

@ -35,6 +35,13 @@ public interface Invocation {
*/
String getMethodName();
/**
* get the interface name
* @return
*/
String getServiceName();
/**
* get parameter types.
*

View File

@ -18,9 +18,11 @@ package org.apache.dubbo.rpc;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@ -43,8 +45,10 @@ public class RpcInvocation implements Invocation, Serializable {
private static final long serialVersionUID = -4355285085441097045L;
private String methodName;
private String serviceName;
private Class<?>[] parameterTypes;
private transient Class<?>[] parameterTypes;
private String parameterTypesDesc;
private Object[] arguments;
@ -54,13 +58,15 @@ public class RpcInvocation implements Invocation, Serializable {
private transient Class<?> returnType;
private transient Type[] returnTypes;
private transient InvokeMode invokeMode;
public RpcInvocation() {
}
public RpcInvocation(Invocation invocation, Invoker<?> invoker) {
this(invocation.getMethodName(), invocation.getParameterTypes(),
this(invocation.getMethodName(), invocation.getServiceName(), invocation.getParameterTypes(),
invocation.getArguments(), new HashMap<String, String>(invocation.getAttachments()),
invocation.getInvoker());
if (invoker != null) {
@ -88,33 +94,40 @@ public class RpcInvocation implements Invocation, Serializable {
}
public RpcInvocation(Invocation invocation) {
this(invocation.getMethodName(), invocation.getParameterTypes(),
this(invocation.getMethodName(), invocation.getServiceName(), invocation.getParameterTypes(),
invocation.getArguments(), invocation.getAttachments(), invocation.getInvoker());
}
public RpcInvocation(Method method, Object[] arguments) {
this(method.getName(), method.getParameterTypes(), arguments, null, null);
public RpcInvocation(Method method, String serviceName, Object[] arguments) {
this(method, serviceName, arguments, null);
}
public RpcInvocation(Method method, Object[] arguments, Map<String, String> attachment) {
this(method.getName(), method.getParameterTypes(), arguments, attachment, null);
public RpcInvocation(Method method, String serviceName, Object[] arguments, Map<String, String> attachment) {
this(method.getName(), serviceName, method.getParameterTypes(), arguments, attachment, null);
this.returnType = method.getReturnType();
}
public RpcInvocation(String methodName, Class<?>[] parameterTypes, Object[] arguments) {
this(methodName, parameterTypes, arguments, null, null);
public RpcInvocation(String methodName, String serviceName, Class<?>[] parameterTypes, Object[] arguments) {
this(methodName, serviceName, parameterTypes, arguments, null, null);
}
public RpcInvocation(String methodName, Class<?>[] parameterTypes, Object[] arguments, Map<String, String> attachments) {
this(methodName, parameterTypes, arguments, attachments, null);
public RpcInvocation(String methodName, String serviceName, Class<?>[] parameterTypes, Object[] arguments, Map<String, String> attachments) {
this(methodName, serviceName, parameterTypes, arguments, attachments, null);
}
public RpcInvocation(String methodName, Class<?>[] parameterTypes, Object[] arguments, Map<String, String> attachments, Invoker<?> invoker) {
public RpcInvocation(String methodName, String serviceName, Class<?>[] parameterTypes, Object[] arguments, Map<String, String> attachments, Invoker<?> invoker) {
this.methodName = methodName;
this.parameterTypes = parameterTypes == null ? new Class<?>[0] : parameterTypes;
this.arguments = arguments == null ? new Object[0] : arguments;
this.attachments = attachments == null ? new HashMap<String, String>() : attachments;
this.invoker = invoker;
ApplicationModel.getServiceModel(serviceName).ifPresent(serviceModel ->
serviceModel.getMethod(methodName, parameterTypes)
.ifPresent(methodModel -> {
this.parameterTypesDesc = methodModel.getParamDesc();
this.returnTypes = methodModel.getReturnTypes();
})
);
}
@Override
@ -131,6 +144,15 @@ public class RpcInvocation implements Invocation, Serializable {
return methodName;
}
@Override
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
@ -144,6 +166,14 @@ public class RpcInvocation implements Invocation, Serializable {
this.parameterTypes = parameterTypes == null ? new Class<?>[0] : parameterTypes;
}
public String getParameterTypesDesc() {
return parameterTypesDesc;
}
public void setParameterTypesDesc(String parameterTypesDesc) {
this.parameterTypesDesc = parameterTypesDesc;
}
@Override
public Object[] getArguments() {
return arguments;
@ -227,6 +257,14 @@ public class RpcInvocation implements Invocation, Serializable {
this.returnType = returnType;
}
public Type[] getReturnTypes() {
return returnTypes;
}
public void setReturnTypes(Type[] returnTypes) {
this.returnTypes = returnTypes;
}
public InvokeMode getInvokeMode() {
return invokeMode;
}

View File

@ -138,7 +138,7 @@ public class GenericFilter extends ListenableFilter {
args[0].getClass().getName());
}
}
return invoker.invoke(new RpcInvocation(method, args, inv.getAttachments()));
return invoker.invoke(new RpcInvocation(method, invoker.getInterface().getName(), args, inv.getAttachments()));
} catch (NoSuchMethodException e) {
throw new RpcException(e.getMessage(), e);
} catch (ClassNotFoundException e) {

View File

@ -54,6 +54,8 @@ public class GenericImplFilter extends ListenableFilter {
private static final Class<?>[] GENERIC_PARAMETER_TYPES = new Class<?>[]{String.class, String[].class, Object[].class};
private static final String GENERIC_PARAMETER_DESC = "Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;";
public GenericImplFilter() {
super.listener = new GenericImplListener();
}
@ -90,6 +92,7 @@ public class GenericImplFilter extends ListenableFilter {
invocation2.setMethodName($INVOKE);
}
invocation2.setParameterTypes(GENERIC_PARAMETER_TYPES);
invocation2.setParameterTypesDesc(GENERIC_PARAMETER_DESC);
invocation2.setArguments(new Object[]{methodName, types, args});
return invoker.invoke(invocation2);
} else if ((invocation.getMethodName().equals($INVOKE) || invocation.getMethodName().equals($INVOKE_ASYNC))

View File

@ -20,6 +20,7 @@ import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@ -37,14 +38,21 @@ public class ApplicationModel {
protected static final Logger LOGGER = LoggerFactory.getLogger(ApplicationModel.class);
/**
* full qualified class name -> provided service
* serviceKey -> exported service
* each service may has different group, version, path, instance and so on, but they point to the same {@link ServiceModel}
*/
private static final ConcurrentMap<String, ProviderModel> PROVIDED_SERVICES = new ConcurrentHashMap<>();
/**
* full qualified class name -> subscribe service
* serviceKey -> referred service
* each service may has different group, version, path, instance and so on, but they point to the same {@link ServiceModel}
*/
private static final ConcurrentMap<String, ConsumerModel> CONSUMED_SERVICES = new ConcurrentHashMap<>();
/**
* The description of a unique service (interface definition in Dubbo)
*/
private static final ConcurrentHashMap<String, ServiceModel> SERVICES = new ConcurrentHashMap<>();
private static String application;
public static Collection<ConsumerModel> allConsumerModels() {
@ -55,12 +63,12 @@ public class ApplicationModel {
return PROVIDED_SERVICES.values();
}
public static ProviderModel getProviderModel(String serviceName) {
return PROVIDED_SERVICES.get(serviceName);
public static ProviderModel getProviderModel(String serviceKey) {
return PROVIDED_SERVICES.get(serviceKey);
}
public static ConsumerModel getConsumerModel(String serviceName) {
return CONSUMED_SERVICES.get(serviceName);
public static ConsumerModel getConsumerModel(String serviceKey) {
return CONSUMED_SERVICES.get(serviceKey);
}
public static void initConsumerModel(String serviceName, ConsumerModel consumerModel) {
@ -75,6 +83,38 @@ public class ApplicationModel {
}
}
public static ServiceModel registerServiceModel(Class<?> interfaceClass) {
return SERVICES.computeIfAbsent(interfaceClass.getName(), (k) -> new ServiceModel(interfaceClass));
}
/**
* See {@link #registerServiceModel(Class)}
*
* we assume:
* 1. services with different interface are not allowed to have the same path.
* 2. services with the same interface but different group/version can share the same path.
* 3. path's default value is the name of the interface.
* @param path
* @param interfaceClass
* @return
*/
public static ServiceModel registerServiceModel(String path, Class<?> interfaceClass) {
ServiceModel serviceModel = registerServiceModel(interfaceClass);
// register path
if (!interfaceClass.getName().equals(path)) {
SERVICES.putIfAbsent(path, serviceModel);
}
return serviceModel;
}
public static Optional<ServiceModel> getServiceModel (String interfaceName) {
return Optional.ofNullable(SERVICES.get(interfaceName));
}
public static Optional<ServiceModel> getServiceModel (Class<?> interfaceClass) {
return Optional.ofNullable(SERVICES.get(interfaceClass.getName()));
}
public static String getApplication() {
return application;
}
@ -90,4 +130,5 @@ public class ApplicationModel {
PROVIDED_SERVICES.clear();
CONSUMED_SERVICES.clear();
}
}

View File

@ -1,161 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.model;
import java.lang.reflect.Method;
import java.util.Map;
import static org.apache.dubbo.rpc.Constants.$INVOKE;
public class ConsumerMethodModel {
private final Method method;
// private final boolean isCallBack;
// private final boolean isFuture;
private final String[] parameterTypes;
private final Class<?>[] parameterClasses;
private final Class<?> returnClass;
private final String methodName;
private final boolean generic;
private final AsyncMethodInfo asyncInfo;
public ConsumerMethodModel(Method method, Map<String, Object> attributes) {
this.method = method;
this.parameterClasses = method.getParameterTypes();
this.returnClass = method.getReturnType();
this.parameterTypes = this.createParamSignature(parameterClasses);
this.methodName = method.getName();
this.generic = methodName.equals($INVOKE) && parameterTypes != null && parameterTypes.length == 3;
if (attributes != null) {
asyncInfo = (AsyncMethodInfo) attributes.get(methodName);
} else {
asyncInfo = null;
}
}
public Method getMethod() {
return method;
}
public Class<?> getReturnClass() {
return returnClass;
}
public AsyncMethodInfo getAsyncInfo() {
return asyncInfo;
}
public String getMethodName() {
return methodName;
}
public String[] getParameterTypes() {
return parameterTypes;
}
private String[] createParamSignature(Class<?>[] args) {
if (args == null || args.length == 0) {
return new String[]{};
}
String[] paramSig = new String[args.length];
for (int x = 0; x < args.length; x++) {
paramSig[x] = args[x].getName();
}
return paramSig;
}
public boolean isGeneric() {
return generic;
}
public Class<?>[] getParameterClasses() {
return parameterClasses;
}
public static class AsyncMethodInfo {
// callback instance when async-call is invoked
private Object oninvokeInstance;
// callback method when async-call is invoked
private Method oninvokeMethod;
// callback instance when async-call is returned
private Object onreturnInstance;
// callback method when async-call is returned
private Method onreturnMethod;
// callback instance when async-call has exception thrown
private Object onthrowInstance;
// callback method when async-call has exception thrown
private Method onthrowMethod;
public Object getOninvokeInstance() {
return oninvokeInstance;
}
public void setOninvokeInstance(Object oninvokeInstance) {
this.oninvokeInstance = oninvokeInstance;
}
public Method getOninvokeMethod() {
return oninvokeMethod;
}
public void setOninvokeMethod(Method oninvokeMethod) {
this.oninvokeMethod = oninvokeMethod;
}
public Object getOnreturnInstance() {
return onreturnInstance;
}
public void setOnreturnInstance(Object onreturnInstance) {
this.onreturnInstance = onreturnInstance;
}
public Method getOnreturnMethod() {
return onreturnMethod;
}
public void setOnreturnMethod(Method onreturnMethod) {
this.onreturnMethod = onreturnMethod;
}
public Object getOnthrowInstance() {
return onthrowInstance;
}
public void setOnthrowInstance(Object onthrowInstance) {
this.onthrowInstance = onthrowInstance;
}
public Method getOnthrowMethod() {
return onthrowMethod;
}
public void setOnthrowMethod(Method onthrowMethod) {
this.onthrowMethod = onthrowMethod;
}
}
}

View File

@ -17,50 +17,49 @@
package org.apache.dubbo.rpc.model;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.common.utils.CollectionUtils;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
/**
* Consumer Model which is about subscribed services.
* This model is bind to your reference's configuration, for example, group, version or method level configuration.
*/
public class ConsumerModel {
private final String serviceKey;
private final Object proxyObject;
private final String serviceName;
private final Class<?> serviceInterfaceClass;
private final Map<Method, ConsumerMethodModel> methodModels = new IdentityHashMap<Method, ConsumerMethodModel>();
private final ServiceModel serviceModel;
private final Map<String, AsyncMethodInfo> methodConfigs = new HashMap<>();
/**
* This constructor create an instance of ConsumerModel and passed objects should not be null.
* If service name, service instance, proxy object,methods should not be null. If these are null
* then this constructor will throw {@link IllegalArgumentException}
* @param serviceName Name of the service.
* @param serviceKey Name of the service.
* @param serviceInterfaceClass Service interface class.
* @param proxyObject Proxy object.
* @param methods Methods of service class
* @param attributes Attributes of methods.
*/
public ConsumerModel(String serviceName
public ConsumerModel(String serviceKey
, Class<?> serviceInterfaceClass
, Object proxyObject
, Method[] methods
, ServiceModel serviceModel
, Map<String, Object> attributes) {
Assert.notEmptyString(serviceName, "Service name can't be null or blank");
Assert.notEmptyString(serviceKey, "Service name can't be null or blank");
Assert.notNull(serviceInterfaceClass, "Service interface class can't null");
Assert.notNull(proxyObject, "Proxy object can't be null");
Assert.notNull(methods, "Methods can't be null");
this.serviceName = serviceName;
this.serviceInterfaceClass = serviceInterfaceClass;
this.serviceKey = serviceKey;
this.proxyObject = proxyObject;
for (Method method : methods) {
methodModels.put(method, new ConsumerMethodModel(method, attributes));
this.serviceModel = serviceModel;
if (CollectionUtils.isNotEmptyMap(attributes)) {
attributes.forEach((method, object) -> {
methodConfigs.put(method, (AsyncMethodInfo) object);
});
}
}
@ -72,41 +71,96 @@ public class ConsumerModel {
return proxyObject;
}
/**
* Return method model for the given method on consumer side
*
* @param method method object
* @return method model
*/
public ConsumerMethodModel getMethodModel(Method method) {
return methodModels.get(method);
}
/**
* Return method model for the given method on consumer side
*
* @param method method object
* @return method model
*/
public ConsumerMethodModel getMethodModel(String method) {
Optional<Map.Entry<Method, ConsumerMethodModel>> consumerMethodModelEntry = methodModels.entrySet().stream().filter(entry -> entry.getKey().getName().equals(method)).findFirst();
return consumerMethodModelEntry.map(Map.Entry::getValue).orElse(null);
}
/**
* Return all method models for the current service
*
* @return method model list
*/
public List<ConsumerMethodModel> getAllMethods() {
return new ArrayList<ConsumerMethodModel>(methodModels.values());
public Set<MethodModel> getAllMethods() {
return serviceModel.getAllMethods();
}
public Class<?> getServiceInterfaceClass() {
return serviceInterfaceClass;
return serviceModel.getServiceInterfaceClass();
}
public String getServiceName() {
return serviceName;
public String getServiceKey() {
return serviceKey;
}
public AsyncMethodInfo getMethodConfig(String methodName) {
return methodConfigs.get(methodName);
}
public ServiceModel getServiceModel() {
return serviceModel;
}
public static class AsyncMethodInfo {
// callback instance when async-call is invoked
private Object oninvokeInstance;
// callback method when async-call is invoked
private Method oninvokeMethod;
// callback instance when async-call is returned
private Object onreturnInstance;
// callback method when async-call is returned
private Method onreturnMethod;
// callback instance when async-call has exception thrown
private Object onthrowInstance;
// callback method when async-call has exception thrown
private Method onthrowMethod;
public Object getOninvokeInstance() {
return oninvokeInstance;
}
public void setOninvokeInstance(Object oninvokeInstance) {
this.oninvokeInstance = oninvokeInstance;
}
public Method getOninvokeMethod() {
return oninvokeMethod;
}
public void setOninvokeMethod(Method oninvokeMethod) {
this.oninvokeMethod = oninvokeMethod;
}
public Object getOnreturnInstance() {
return onreturnInstance;
}
public void setOnreturnInstance(Object onreturnInstance) {
this.onreturnInstance = onreturnInstance;
}
public Method getOnreturnMethod() {
return onreturnMethod;
}
public void setOnreturnMethod(Method onreturnMethod) {
this.onreturnMethod = onreturnMethod;
}
public Object getOnthrowInstance() {
return onthrowInstance;
}
public void setOnthrowInstance(Object onthrowInstance) {
this.onthrowInstance = onthrowInstance;
}
public Method getOnthrowMethod() {
return onthrowMethod;
}
public void setOnthrowMethod(Method onthrowMethod) {
this.onthrowMethod = onthrowMethod;
}
}
}

View File

@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.model;
import org.apache.dubbo.common.utils.ReflectUtils;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import static org.apache.dubbo.rpc.Constants.$INVOKE;
import static org.apache.dubbo.rpc.Constants.$INVOKE_ASYNC;
/**
*
*/
public class MethodModel {
private final Method method;
// private final boolean isCallBack;
// private final boolean isFuture;
private final String paramDesc;
private final Class<?>[] parameterClasses;
private final Class<?> returnClass;
private final Type[] returnTypes;
private final String methodName;
private final boolean generic;
public MethodModel (Method method) {
this.method = method;
this.parameterClasses = method.getParameterTypes();
this.returnClass = method.getReturnType();
this.returnTypes = ReflectUtils.getReturnTypes(method);
this.paramDesc = ReflectUtils.getDesc(method);
this.methodName = method.getName();
this.generic = (methodName.equals($INVOKE) || methodName.equals($INVOKE_ASYNC)) && parameterClasses.length == 3;
}
public boolean matchParams (String params) {
return paramDesc.equalsIgnoreCase(params);
}
public Method getMethod() {
return method;
}
public String getParamDesc() {
return paramDesc;
}
public Class<?>[] getParameterClasses() {
return parameterClasses;
}
public Class<?> getReturnClass() {
return returnClass;
}
public Type[] getReturnTypes() {
return returnTypes;
}
public String getMethodName() {
return methodName;
}
public boolean isGeneric() {
return generic;
}
}

View File

@ -1,63 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.model;
import java.lang.reflect.Method;
public class ProviderMethodModel {
private transient final Method method;
private final String methodName;
private final String[] methodArgTypes;
private final String serviceName;
public ProviderMethodModel(Method method, String serviceName) {
this.method = method;
this.serviceName = serviceName;
this.methodName = method.getName();
this.methodArgTypes = getArgTypes(method);
}
public Method getMethod() {
return method;
}
public String getMethodName() {
return methodName;
}
public String[] getMethodArgTypes() {
return methodArgTypes;
}
public String getServiceName() {
return serviceName;
}
private static String[] getArgTypes(Method method) {
String[] methodArgTypes = new String[0];
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length > 0) {
methodArgTypes = new String[parameterTypes.length];
int index = 0;
for (Class<?> paramType : parameterTypes) {
methodArgTypes[index++] = paramType.getName();
}
}
return methodArgTypes;
}
}

View File

@ -16,81 +16,43 @@
*/
package org.apache.dubbo.rpc.model;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* ProviderModel which is about published services
*/
public class ProviderModel {
private final String serviceName;
private final String serviceKey;
private final Object serviceInstance;
private final Class<?> serviceInterfaceClass;
private final Map<String, List<ProviderMethodModel>> methods = new HashMap<String, List<ProviderMethodModel>>();
private final ServiceModel serviceModel;
public ProviderModel(String serviceName, Object serviceInstance, Class<?> serviceInterfaceClass) {
public ProviderModel(String serviceKey, Object serviceInstance, ServiceModel serviceModel) {
if (null == serviceInstance) {
throw new IllegalArgumentException("Service[" + serviceName + "]Target is NULL.");
throw new IllegalArgumentException("Service[" + serviceKey + "]Target is NULL.");
}
this.serviceName = serviceName;
this.serviceKey = serviceKey;
this.serviceInstance = serviceInstance;
this.serviceInterfaceClass = serviceInterfaceClass;
initMethod();
this.serviceModel = serviceModel;
}
public String getServiceName() {
return serviceName;
public String getServiceKey() {
return serviceKey;
}
public Class<?> getServiceInterfaceClass() {
return serviceInterfaceClass;
return serviceModel.getServiceInterfaceClass();
}
public Object getServiceInstance() {
return serviceInstance;
}
public List<ProviderMethodModel> getAllMethods() {
List<ProviderMethodModel> result = new ArrayList<ProviderMethodModel>();
for (List<ProviderMethodModel> models : methods.values()) {
result.addAll(models);
}
return result;
public Set<MethodModel> getAllMethods() {
return serviceModel.getAllMethods();
}
public ProviderMethodModel getMethodModel(String methodName, String[] argTypes) {
List<ProviderMethodModel> methodModels = methods.get(methodName);
if (methodModels != null) {
for (ProviderMethodModel methodModel : methodModels) {
if (Arrays.equals(argTypes, methodModel.getMethodArgTypes())) {
return methodModel;
}
}
}
return null;
public ServiceModel getServiceModel() {
return serviceModel;
}
private void initMethod() {
Method[] methodsToExport = null;
methodsToExport = this.serviceInterfaceClass.getMethods();
for (Method method : methodsToExport) {
method.setAccessible(true);
List<ProviderMethodModel> methodModels = methods.get(method.getName());
if (methodModels == null) {
methodModels = new ArrayList<ProviderMethodModel>(1);
methods.put(method.getName(), methodModels);
}
methodModels.add(new ProviderMethodModel(method, serviceName));
}
}
}

View File

@ -0,0 +1,100 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.model;
import org.apache.dubbo.common.utils.CollectionUtils;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
public class ServiceModel {
private final String serviceName;
private final Class<?> serviceInterfaceClass;
// to accelarate search
private final Map<String, Set<MethodModel>> methods = new HashMap<>();
private final Map<String, Map<String, MethodModel>> descToMethods = new HashMap<>();
public ServiceModel (Class<?> interfaceClass) {
this.serviceInterfaceClass = interfaceClass;
this.serviceName = interfaceClass.getName();
initMethods();
}
private void initMethods() {
Method[] methodsToExport = null;
methodsToExport = this.serviceInterfaceClass.getMethods();
for (Method method : methodsToExport) {
method.setAccessible(true);
Set<MethodModel> methodModels = methods.computeIfAbsent(method.getName(), (k) ->new HashSet<>(1));
methodModels.add(new MethodModel(method));
}
methods.forEach((methodName, methodList) -> {
Map<String, MethodModel> descMap = descToMethods.computeIfAbsent(methodName, k -> new HashMap<>());
methodList.forEach(methodModel -> descMap.put(methodModel.getParamDesc(), methodModel));
// Map<Class<?>[], MethodModel> typesMap = typeToMethods.computeIfAbsent(methodName, k -> new HashMap<>());
// methodList.forEach(methodModel -> typesMap.put(methodModel.getParameterClasses(), methodModel));
});
}
public String getServiceName() {
return serviceName;
}
public Class<?> getServiceInterfaceClass() {
return serviceInterfaceClass;
}
public Set<MethodModel> getAllMethods () {
Set<MethodModel> methodModels = new HashSet<>();
methods.forEach((k, v) -> methodModels.addAll(v));
return methodModels;
}
public Optional<MethodModel> getMethod (String methodName, String params) {
Map<String, MethodModel> methods = descToMethods.get(methodName);
if (CollectionUtils.isNotEmptyMap(methods)) {
return Optional.ofNullable(methods.get(params));
}
return Optional.empty();
}
public Optional<MethodModel> getMethod (String methodName, Class<?>[] paramTypes) {
Set<MethodModel> methodModels = methods.get(methodName);
if (CollectionUtils.isNotEmpty(methodModels)) {
for (MethodModel methodModel : methodModels) {
if (Arrays.equals(paramTypes, methodModel.getParameterClasses())) {
return Optional.of(methodModel);
}
}
}
return Optional.empty();
}
public Set<MethodModel> getMethods (String methodName) {
return methods.get(methodName);
}
}

View File

@ -27,7 +27,6 @@ import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.support.RpcUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.CompletableFuture;
@ -112,10 +111,7 @@ public abstract class AbstractProxyInvoker<T> implements Invoker<T> {
private CompletableFuture<Object> wrapWithFuture (Object value, Invocation invocation) {
if (RpcContext.getContext().isAsyncStarted()) {
return ((AsyncContextImpl)(RpcContext.getContext().getAsyncContext())).getInternalFuture();
} else if (RpcUtils.isReturnTypeFuture(invocation)) {
if (value == null) {
return CompletableFuture.completedFuture(null);
}
} else if (value instanceof CompletableFuture) {
return (CompletableFuture<Object>) value;
}
return CompletableFuture.completedFuture(value);

View File

@ -52,6 +52,6 @@ public class InvokerInvocationHandler implements InvocationHandler {
return invoker.equals(args[0]);
}
return invoker.invoke(new RpcInvocation(method, args)).recreate();
return invoker.invoke(new RpcInvocation(method, invoker.getInterface().getName(), args)).recreate();
}
}

View File

@ -27,8 +27,6 @@ import org.apache.dubbo.rpc.RpcInvocation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;
@ -36,7 +34,6 @@ import static org.apache.dubbo.rpc.Constants.$INVOKE;
import static org.apache.dubbo.rpc.Constants.$INVOKE_ASYNC;
import static org.apache.dubbo.rpc.Constants.ASYNC_KEY;
import static org.apache.dubbo.rpc.Constants.AUTO_ATTACH_INVOCATIONID_KEY;
import static org.apache.dubbo.rpc.Constants.FUTURE_GENERATED_KEY;
import static org.apache.dubbo.rpc.Constants.ID_KEY;
import static org.apache.dubbo.rpc.Constants.RETURN_KEY;
/**
@ -171,7 +168,12 @@ public class RpcUtils {
}
public static boolean isReturnTypeFuture(Invocation inv) {
Class<?> clazz = getReturnType(inv);
Class<?> clazz;
if (inv instanceof RpcInvocation) {
clazz = ((RpcInvocation) inv).getReturnType();
} else {
clazz = getReturnType(inv);
}
return (clazz != null && CompletableFuture.class.isAssignableFrom(clazz)) || isGenericAsync(inv);
}
@ -198,12 +200,4 @@ public class RpcUtils {
}
return isOneway;
}
public static Map<String, String> getNecessaryAttachments(Invocation inv) {
Map<String, String> attachments = new HashMap<>(inv.getAttachments());
attachments.remove(ASYNC_KEY);
attachments.remove(FUTURE_GENERATED_KEY);
return attachments;
}
}

View File

@ -28,7 +28,6 @@ import org.apache.dubbo.rpc.support.DemoService;
import org.apache.dubbo.rpc.support.LocalException;
import com.alibaba.com.caucho.hessian.HessianException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@ -52,7 +51,7 @@ public class ExceptionFilterTest {
RpcException exception = new RpcException("TestRpcException");
ExceptionFilter exceptionFilter = new ExceptionFilter();
RpcInvocation invocation = new RpcInvocation("sayHello", new Class<?>[]{String.class}, new Object[]{"world"});
RpcInvocation invocation = new RpcInvocation("sayHello", DemoService.class.getName(), new Class<?>[]{String.class}, new Object[]{"world"});
Invoker<DemoService> invoker = mock(Invoker.class);
given(invoker.getInterface()).willReturn(DemoService.class);
given(invoker.invoke(eq(invocation))).willThrow(exception);
@ -76,7 +75,7 @@ public class ExceptionFilterTest {
public void testJavaException() {
ExceptionFilter exceptionFilter = new ExceptionFilter();
RpcInvocation invocation = new RpcInvocation("sayHello", new Class<?>[]{String.class}, new Object[]{"world"});
RpcInvocation invocation = new RpcInvocation("sayHello", DemoService.class.getName(), new Class<?>[]{String.class}, new Object[]{"world"});
AppResponse appResponse = new AppResponse();
appResponse.setException(new IllegalArgumentException("java"));
@ -96,7 +95,7 @@ public class ExceptionFilterTest {
public void testRuntimeException() {
ExceptionFilter exceptionFilter = new ExceptionFilter();
RpcInvocation invocation = new RpcInvocation("sayHello", new Class<?>[]{String.class}, new Object[]{"world"});
RpcInvocation invocation = new RpcInvocation("sayHello", DemoService.class.getName(), new Class<?>[]{String.class}, new Object[]{"world"});
AppResponse appResponse = new AppResponse();
appResponse.setException(new LocalException("localException"));
@ -116,7 +115,7 @@ public class ExceptionFilterTest {
public void testConvertToRunTimeException() throws Exception {
ExceptionFilter exceptionFilter = new ExceptionFilter();
RpcInvocation invocation = new RpcInvocation("sayHello", new Class<?>[]{String.class}, new Object[]{"world"});
RpcInvocation invocation = new RpcInvocation("sayHello", DemoService.class.getName(), new Class<?>[]{String.class}, new Object[]{"world"});
AppResponse mockRpcResult = new AppResponse();
mockRpcResult.setException(new HessianException("hessian"));

View File

@ -36,11 +36,11 @@ import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import static org.apache.dubbo.rpc.Constants.$INVOKE;
import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
import static org.apache.dubbo.rpc.Constants.GENERIC_SERIALIZATION_NATIVE_JAVA;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.apache.dubbo.rpc.Constants.$INVOKE;
import static org.apache.dubbo.rpc.Constants.GENERIC_SERIALIZATION_NATIVE_JAVA;
import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
public class GenericFilterTest {
GenericFilter genericFilter = new GenericFilter();
@ -54,7 +54,7 @@ public class GenericFilterTest {
person.put("name", "dubbo");
person.put("age", 10);
RpcInvocation invocation = new RpcInvocation($INVOKE, genericInvoke.getParameterTypes(),
RpcInvocation invocation = new RpcInvocation($INVOKE, GenericService.class.getName(), genericInvoke.getParameterTypes(),
new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}});
URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" +
@ -82,7 +82,7 @@ public class GenericFilterTest {
person.put("name", "dubbo");
person.put("age", 10);
RpcInvocation invocation = new RpcInvocation($INVOKE, genericInvoke.getParameterTypes(),
RpcInvocation invocation = new RpcInvocation($INVOKE, GenericService.class.getName(), genericInvoke.getParameterTypes(),
new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}});
invocation.setAttachment(GENERIC_KEY, GENERIC_SERIALIZATION_NATIVE_JAVA);
@ -106,7 +106,7 @@ public class GenericFilterTest {
person.put("name", "dubbo");
person.put("age", 10);
RpcInvocation invocation = new RpcInvocation("sayHi", genericInvoke.getParameterTypes()
RpcInvocation invocation = new RpcInvocation("sayHi", GenericService.class.getName(), genericInvoke.getParameterTypes()
, new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}});
URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" +
@ -130,7 +130,7 @@ public class GenericFilterTest {
person.put("name", "dubbo");
person.put("age", 10);
RpcInvocation invocation = new RpcInvocation($INVOKE, genericInvoke.getParameterTypes()
RpcInvocation invocation = new RpcInvocation($INVOKE, GenericService.class.getName(), genericInvoke.getParameterTypes()
, new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}});
URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" +

View File

@ -36,11 +36,10 @@ import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;
import static org.apache.dubbo.rpc.Constants.$INVOKE;
import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;
public class GenericImplFilterTest {
@ -49,7 +48,7 @@ public class GenericImplFilterTest {
@Test
public void testInvoke() throws Exception {
RpcInvocation invocation = new RpcInvocation("getPerson",
RpcInvocation invocation = new RpcInvocation("getPerson", "org.apache.dubbo.rpc.support.DemoService",
new Class[]{Person.class}, new Object[]{new Person("dubbo", 10)});
@ -77,7 +76,7 @@ public class GenericImplFilterTest {
@Test
public void testInvokeWithException() throws Exception {
RpcInvocation invocation = new RpcInvocation("getPerson",
RpcInvocation invocation = new RpcInvocation("getPerson", "org.apache.dubbo.rpc.support.DemoService",
new Class[]{Person.class}, new Object[]{new Person("dubbo", 10)});
URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" +
@ -105,7 +104,7 @@ public class GenericImplFilterTest {
person.put("name", "dubbo");
person.put("age", 10);
RpcInvocation invocation = new RpcInvocation($INVOKE, genericInvoke.getParameterTypes(),
RpcInvocation invocation = new RpcInvocation($INVOKE, GenericService.class.getName(), genericInvoke.getParameterTypes(),
new Object[]{"getPerson", new String[]{Person.class.getCanonicalName()}, new Object[]{person}});
URL url = URL.valueOf("test://test:11/org.apache.dubbo.rpc.support.DemoService?" +

View File

@ -51,7 +51,7 @@ public abstract class AbstractProxyTest {
//Assertions.assertEquals(proxy.toString(), invoker.toString());
//Assertions.assertEquals(proxy.hashCode(), invoker.hashCode());
Assertions.assertEquals(invoker.invoke(new RpcInvocation("echo", new Class[]{String.class}, new Object[]{"aa"})).getValue()
Assertions.assertEquals(invoker.invoke(new RpcInvocation("echo", DemoService.class.getName(), new Class[]{String.class}, new Object[]{"aa"})).getValue()
, proxy.echo("aa"));
}
@ -65,7 +65,7 @@ public abstract class AbstractProxyTest {
Assertions.assertEquals(invoker.getInterface(), DemoService.class);
Assertions.assertEquals(invoker.invoke(new RpcInvocation("echo", new Class[]{String.class}, new Object[]{"aa"})).getValue(),
Assertions.assertEquals(invoker.invoke(new RpcInvocation("echo", DemoService.class.getName(), new Class[]{String.class}, new Object[]{"aa"})).getValue(),
origin.echo("aa"));
}

View File

@ -38,6 +38,11 @@ public class MockInvocation implements Invocation {
return "echo";
}
@Override
public String getServiceName() {
return "DemoService";
}
public Class<?>[] getParameterTypes() {
return new Class[]{String.class};
}

View File

@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcInvocation;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@ -28,6 +29,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.apache.dubbo.rpc.Constants.AUTO_ATTACH_INVOCATIONID_KEY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
@ -35,8 +37,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.apache.dubbo.rpc.Constants.AUTO_ATTACH_INVOCATIONID_KEY;
public class RpcUtilsTest {
/**
@ -48,7 +48,7 @@ public class RpcUtilsTest {
URL url = URL.valueOf("dubbo://localhost/?test.async=true");
Map<String, String> attachments = new HashMap<String, String>();
attachments.put("aa", "bb");
Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{}, attachments);
Invocation inv = new RpcInvocation("test", "DemoService", new Class[]{}, new String[]{}, attachments);
RpcUtils.attachInvocationIdIfAsync(url, inv);
long id1 = RpcUtils.getInvocationId(inv);
RpcUtils.attachInvocationIdIfAsync(url, inv);
@ -65,7 +65,7 @@ public class RpcUtilsTest {
@Test
public void testAttachInvocationIdIfAsync_sync() {
URL url = URL.valueOf("dubbo://localhost/");
Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{});
Invocation inv = new RpcInvocation("test", "DemoService", new Class[]{}, new String[]{});
RpcUtils.attachInvocationIdIfAsync(url, inv);
assertNull(RpcUtils.getInvocationId(inv));
}
@ -77,7 +77,7 @@ public class RpcUtilsTest {
@Test
public void testAttachInvocationIdIfAsync_nullAttachments() {
URL url = URL.valueOf("dubbo://localhost/?test.async=true");
Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{});
Invocation inv = new RpcInvocation("test", "DemoService", new Class[]{}, new String[]{});
RpcUtils.attachInvocationIdIfAsync(url, inv);
assertTrue(RpcUtils.getInvocationId(inv) >= 0l);
}
@ -89,7 +89,7 @@ public class RpcUtilsTest {
@Test
public void testAttachInvocationIdIfAsync_forceNotAttache() {
URL url = URL.valueOf("dubbo://localhost/?test.async=true&" + AUTO_ATTACH_INVOCATIONID_KEY + "=false");
Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{});
Invocation inv = new RpcInvocation("test", "DemoService", new Class[]{}, new String[]{});
RpcUtils.attachInvocationIdIfAsync(url, inv);
assertNull(RpcUtils.getInvocationId(inv));
}
@ -101,7 +101,7 @@ public class RpcUtilsTest {
@Test
public void testAttachInvocationIdIfAsync_forceAttache() {
URL url = URL.valueOf("dubbo://localhost/?" + AUTO_ATTACH_INVOCATIONID_KEY + "=true");
Invocation inv = new RpcInvocation("test", new Class[]{}, new String[]{});
Invocation inv = new RpcInvocation("test", "DemoService", new Class[]{}, new String[]{});
RpcUtils.attachInvocationIdIfAsync(url, inv);
assertNotNull(RpcUtils.getInvocationId(inv));
}
@ -109,40 +109,41 @@ public class RpcUtilsTest {
@Test
public void testGetReturnTypes() throws Exception {
Invoker invoker = mock(Invoker.class);
String service = "org.apache.dubbo.rpc.support.DemoService";
given(invoker.getUrl()).willReturn(URL.valueOf("test://127.0.0.1:1/org.apache.dubbo.rpc.support.DemoService?interface=org.apache.dubbo.rpc.support.DemoService"));
Invocation inv = new RpcInvocation("testReturnType", new Class<?>[]{String.class}, null, null, invoker);
Invocation inv = new RpcInvocation("testReturnType", service, new Class<?>[]{String.class}, null, null, invoker);
java.lang.reflect.Type[] types = RpcUtils.getReturnTypes(inv);
Assertions.assertEquals(2, types.length);
Assertions.assertEquals(String.class, types[0]);
Assertions.assertEquals(String.class, types[1]);
Invocation inv1 = new RpcInvocation("testReturnType1", new Class<?>[]{String.class}, null, null, invoker);
Invocation inv1 = new RpcInvocation("testReturnType1", service, new Class<?>[]{String.class}, null, null, invoker);
java.lang.reflect.Type[] types1 = RpcUtils.getReturnTypes(inv1);
Assertions.assertEquals(2, types1.length);
Assertions.assertEquals(List.class, types1[0]);
Assertions.assertEquals(DemoService.class.getMethod("testReturnType1", new Class<?>[]{String.class}).getGenericReturnType(), types1[1]);
Invocation inv2 = new RpcInvocation("testReturnType2", new Class<?>[]{String.class}, null, null, invoker);
Invocation inv2 = new RpcInvocation("testReturnType2", service, new Class<?>[]{String.class}, null, null, invoker);
java.lang.reflect.Type[] types2 = RpcUtils.getReturnTypes(inv2);
Assertions.assertEquals(2, types2.length);
Assertions.assertEquals(String.class, types2[0]);
Assertions.assertEquals(String.class, types2[1]);
Invocation inv3 = new RpcInvocation("testReturnType3", new Class<?>[]{String.class}, null, null, invoker);
Invocation inv3 = new RpcInvocation("testReturnType3", service, new Class<?>[]{String.class}, null, null, invoker);
java.lang.reflect.Type[] types3 = RpcUtils.getReturnTypes(inv3);
Assertions.assertEquals(2, types3.length);
Assertions.assertEquals(List.class, types3[0]);
java.lang.reflect.Type genericReturnType3 = DemoService.class.getMethod("testReturnType3", new Class<?>[]{String.class}).getGenericReturnType();
Assertions.assertEquals(((ParameterizedType) genericReturnType3).getActualTypeArguments()[0], types3[1]);
Invocation inv4 = new RpcInvocation("testReturnType4", new Class<?>[]{String.class}, null, null, invoker);
Invocation inv4 = new RpcInvocation("testReturnType4", service, new Class<?>[]{String.class}, null, null, invoker);
java.lang.reflect.Type[] types4 = RpcUtils.getReturnTypes(inv4);
Assertions.assertEquals(2, types4.length);
Assertions.assertNull(types4[0]);
Assertions.assertNull(types4[1]);
Invocation inv5 = new RpcInvocation("testReturnType5", new Class<?>[]{String.class}, null, null, invoker);
Invocation inv5 = new RpcInvocation("testReturnType5", service, new Class<?>[]{String.class}, null, null, invoker);
java.lang.reflect.Type[] types5 = RpcUtils.getReturnTypes(inv5);
Assertions.assertEquals(2, types5.length);
Assertions.assertEquals(Map.class, types5[0]);

View File

@ -22,7 +22,6 @@ import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.serialize.Cleanable;
import org.apache.dubbo.common.serialize.ObjectInput;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.remoting.Channel;
import org.apache.dubbo.remoting.Codec;
@ -30,17 +29,21 @@ import org.apache.dubbo.remoting.Decodeable;
import org.apache.dubbo.remoting.exchange.Request;
import org.apache.dubbo.remoting.transport.CodecSupport;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.MethodModel;
import org.apache.dubbo.rpc.model.ServiceModel;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.CallbackServiceCodec.decodeInvocationArgument;
import static org.apache.dubbo.remoting.Constants.DUBBO_VERSION_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.CallbackServiceCodec.decodeInvocationArgument;
public class DecodeableRpcInvocation extends RpcInvocation implements Codec, Decodeable {
@ -97,27 +100,37 @@ public class DecodeableRpcInvocation extends RpcInvocation implements Codec, Dec
request.setVersion(dubboVersion);
setAttachment(DUBBO_VERSION_KEY, dubboVersion);
setAttachment(PATH_KEY, in.readUTF());
String path = in.readUTF();
setAttachment(PATH_KEY, path);
setAttachment(VERSION_KEY, in.readUTF());
setMethodName(in.readUTF());
String desc = in.readUTF();
setParameterTypesDesc(desc);
try {
Object[] args;
Class<?>[] pts;
String desc = in.readUTF();
if (desc.length() == 0) {
pts = DubboCodec.EMPTY_CLASS_ARRAY;
args = DubboCodec.EMPTY_OBJECT_ARRAY;
} else {
pts = ReflectUtils.desc2classArray(desc);
args = new Object[pts.length];
for (int i = 0; i < args.length; i++) {
try {
args[i] = in.readObject(pts[i]);
} catch (Exception e) {
if (log.isWarnEnabled()) {
log.warn("Decode argument failed: " + e.getMessage(), e);
Object[] args = DubboCodec.EMPTY_OBJECT_ARRAY;
Class<?>[] pts = DubboCodec.EMPTY_CLASS_ARRAY;
if (desc.length() > 0) {
// TODO, lambda function requires variables to be final.
Optional<ServiceModel> serviceModel = ApplicationModel.getServiceModel(path);
if (serviceModel.isPresent()) {
Optional<MethodModel> methodOptional = serviceModel.get().getMethod(getMethodName(), desc);
if (methodOptional.isPresent()) {
pts = methodOptional.get().getParameterClasses();
args = new Object[pts.length];
for (int i = 0; i < args.length; i++) {
try {
args[i] = in.readObject(pts[i]);
} catch (Exception e) {
if (log.isWarnEnabled()) {
log.warn("Decode argument failed: " + e.getMessage(), e);
}
}
}
this.setReturnTypes(methodOptional.get().getReturnTypes());
}
}
}
@ -132,6 +145,7 @@ public class DecodeableRpcInvocation extends RpcInvocation implements Codec, Dec
attachment.putAll(map);
setAttachments(attachment);
}
//decode argument ,may be callback
for (int i = 0; i < args.length; i++) {
args[i] = decodeInvocationArgument(channel, this, pts, i, args[i]);

View File

@ -30,6 +30,7 @@ import org.apache.dubbo.remoting.exchange.Response;
import org.apache.dubbo.remoting.transport.CodecSupport;
import org.apache.dubbo.rpc.AppResponse;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.support.RpcUtils;
import java.io.IOException;
@ -38,6 +39,7 @@ import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.Map;
public class DecodeableRpcResult extends AppResponse implements Codec, Decodeable {
private static final Logger log = LoggerFactory.getLogger(DecodeableRpcResult.class);
@ -124,7 +126,12 @@ public class DecodeableRpcResult extends AppResponse implements Codec, Decodeabl
private void handleValue(ObjectInput in) throws IOException {
try {
Type[] returnTypes = RpcUtils.getReturnTypes(invocation);
Type[] returnTypes;
if (invocation instanceof RpcInvocation) {
returnTypes = ((RpcInvocation)invocation).getReturnTypes();
} else {
returnTypes = RpcUtils.getReturnTypes(invocation);
}
Object value = null;
if (ArrayUtils.isEmpty(returnTypes)) {
value = in.readObject();

View File

@ -23,7 +23,6 @@ import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.serialize.ObjectInput;
import org.apache.dubbo.common.serialize.ObjectOutput;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.remoting.Channel;
import org.apache.dubbo.remoting.exchange.Request;
@ -33,17 +32,16 @@ import org.apache.dubbo.remoting.transport.CodecSupport;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.support.RpcUtils;
import java.io.IOException;
import java.io.InputStream;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.remoting.Constants.DUBBO_VERSION_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.CallbackServiceCodec.encodeInvocationArgument;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.DECODE_IN_IO_THREAD_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.DEFAULT_DECODE_IN_IO_THREAD;
import static org.apache.dubbo.remoting.Constants.DUBBO_VERSION_KEY;
/**
* Dubbo codec.
@ -179,14 +177,14 @@ public class DubboCodec extends ExchangeCodec {
out.writeUTF(inv.getAttachment(VERSION_KEY));
out.writeUTF(inv.getMethodName());
out.writeUTF(ReflectUtils.getDesc(inv.getParameterTypes()));
out.writeUTF(inv.getParameterTypesDesc());
Object[] args = inv.getArguments();
if (args != null) {
for (int i = 0; i < args.length; i++) {
out.writeObject(encodeInvocationArgument(channel, inv, i));
}
}
out.writeObject(RpcUtils.getNecessaryAttachments(inv));
out.writeObject(inv.getAttachments());
}
@Override

View File

@ -62,27 +62,27 @@ import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.rpc.Constants.LAZY_CONNECT_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.ON_CONNECT_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.ON_DISCONNECT_KEY;
import static org.apache.dubbo.remoting.Constants.DEFAULT_HEARTBEAT;
import static org.apache.dubbo.remoting.Constants.HEARTBEAT_KEY;
import static org.apache.dubbo.remoting.Constants.CHANNEL_READONLYEVENT_SENT_KEY;
import static org.apache.dubbo.remoting.Constants.CLIENT_KEY;
import static org.apache.dubbo.remoting.Constants.CODEC_KEY;
import static org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY;
import static org.apache.dubbo.remoting.Constants.DEFAULT_HEARTBEAT;
import static org.apache.dubbo.remoting.Constants.DEFAULT_REMOTING_CLIENT;
import static org.apache.dubbo.remoting.Constants.HEARTBEAT_KEY;
import static org.apache.dubbo.remoting.Constants.SERVER_KEY;
import static org.apache.dubbo.rpc.Constants.DEFAULT_REMOTING_SERVER;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.CALLBACK_SERVICE_KEY;
import static org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY;
import static org.apache.dubbo.rpc.Constants.DEFAULT_STUB_EVENT;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.IS_CALLBACK_SERVICE;
import static org.apache.dubbo.rpc.Constants.IS_SERVER_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.OPTIMIZER_KEY;
import static org.apache.dubbo.rpc.Constants.LAZY_CONNECT_KEY;
import static org.apache.dubbo.rpc.Constants.STUB_EVENT_KEY;
import static org.apache.dubbo.rpc.Constants.STUB_EVENT_METHODS_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.SHARE_CONNECTIONS_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.CALLBACK_SERVICE_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.DEFAULT_SHARE_CONNECTIONS;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.IS_CALLBACK_SERVICE;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.ON_CONNECT_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.ON_DISCONNECT_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.OPTIMIZER_KEY;
import static org.apache.dubbo.rpc.protocol.dubbo.Constants.SHARE_CONNECTIONS_KEY;
/**
@ -187,13 +187,22 @@ public class DubboProtocol extends AbstractProtocol {
}
}
/**
* FIXME channel.getUrl() always binds to a fixed service, and this service is random.
* we can choose to use a common service to carry onConnect event if there's no easy way to get the specific
* service this connection is binding to.
* @param channel
* @param url
* @param methodKey
* @return
*/
private Invocation createInvocation(Channel channel, URL url, String methodKey) {
String method = url.getParameter(methodKey);
if (method == null || method.length() == 0) {
return null;
}
RpcInvocation invocation = new RpcInvocation(method, new Class<?>[0], new Object[0]);
RpcInvocation invocation = new RpcInvocation(method, url.getParameter(INTERFACE_KEY), new Class<?>[0], new Object[0]);
invocation.setAttachment(PATH_KEY, url.getPath());
invocation.setAttachment(GROUP_KEY, url.getParameter(GROUP_KEY));
invocation.setAttachment(INTERFACE_KEY, url.getParameter(INTERFACE_KEY));

View File

@ -26,7 +26,6 @@ import org.apache.dubbo.rpc.ListenableFilter;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ConsumerMethodModel;
import org.apache.dubbo.rpc.model.ConsumerModel;
import java.lang.reflect.InvocationTargetException;
@ -55,7 +54,7 @@ public class FutureFilter extends ListenableFilter {
}
private void fireInvokeCallback(final Invoker<?> invoker, final Invocation invocation) {
final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
final ConsumerModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
if (asyncMethodInfo == null) {
return;
}
@ -83,7 +82,7 @@ public class FutureFilter extends ListenableFilter {
}
private void fireReturnCallback(final Invoker<?> invoker, final Invocation invocation, final Object result) {
final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
final ConsumerModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
if (asyncMethodInfo == null) {
return;
}
@ -129,7 +128,7 @@ public class FutureFilter extends ListenableFilter {
}
private void fireThrowCallback(final Invoker<?> invoker, final Invocation invocation, final Throwable exception) {
final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
final ConsumerModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
if (asyncMethodInfo == null) {
return;
}
@ -175,7 +174,7 @@ public class FutureFilter extends ListenableFilter {
}
}
private ConsumerMethodModel.AsyncMethodInfo getAsyncMethodInfo(Invoker<?> invoker, Invocation invocation) {
private ConsumerModel.AsyncMethodInfo getAsyncMethodInfo(Invoker<?> invoker, Invocation invocation) {
final ConsumerModel consumerModel = ApplicationModel.getConsumerModel(invoker.getUrl().getServiceKey());
if (consumerModel == null) {
return null;
@ -186,12 +185,7 @@ public class FutureFilter extends ListenableFilter {
methodName = (String) invocation.getArguments()[0];
}
ConsumerMethodModel methodModel = consumerModel.getMethodModel(methodName);
if (methodModel == null) {
return null;
}
final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = methodModel.getAsyncInfo();
final ConsumerModel.AsyncMethodInfo asyncMethodInfo = consumerModel.getMethodConfig(methodName);
if (asyncMethodInfo == null) {
return null;
}

View File

@ -25,7 +25,7 @@ import org.apache.dubbo.remoting.telnet.TelnetHandler;
import org.apache.dubbo.remoting.telnet.support.Help;
import org.apache.dubbo.rpc.AppResponse;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ProviderMethodModel;
import org.apache.dubbo.rpc.model.MethodModel;
import org.apache.dubbo.rpc.model.ProviderModel;
import com.alibaba.fastjson.JSON;
@ -35,6 +35,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.apache.dubbo.common.utils.PojoUtils.realize;
@ -152,15 +153,15 @@ public class InvokeTelnetHandler implements TelnetHandler {
private boolean isServiceMatch(String service, ProviderModel provider) {
return provider.getServiceName().equalsIgnoreCase(service)
return provider.getServiceKey().equalsIgnoreCase(service)
|| provider.getServiceInterfaceClass().getSimpleName().equalsIgnoreCase(service)
|| provider.getServiceInterfaceClass().getName().equalsIgnoreCase(service)
|| StringUtils.isEmpty(service);
}
private List<Method> findSameSignatureMethod(List<ProviderMethodModel> methods, String lookupMethodName, List<Object> args) {
private List<Method> findSameSignatureMethod(Set<MethodModel> methods, String lookupMethodName, List<Object> args) {
List<Method> sameSignatureMethods = new ArrayList<>();
for (ProviderMethodModel model : methods) {
for (MethodModel model : methods) {
Method method = model.getMethod();
if (method.getName().equals(lookupMethodName) && method.getParameterTypes().length == args.size()) {
sameSignatureMethods.add(method);

View File

@ -23,9 +23,8 @@ import org.apache.dubbo.remoting.Channel;
import org.apache.dubbo.remoting.telnet.TelnetHandler;
import org.apache.dubbo.remoting.telnet.support.Help;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ConsumerMethodModel;
import org.apache.dubbo.rpc.model.ConsumerModel;
import org.apache.dubbo.rpc.model.ProviderMethodModel;
import org.apache.dubbo.rpc.model.MethodModel;
import org.apache.dubbo.rpc.model.ProviderModel;
import java.lang.reflect.Method;
@ -87,11 +86,11 @@ public class ListTelnetHandler implements TelnetHandler {
}
for (ProviderModel provider : ApplicationModel.allProviderModels()) {
buf.append(provider.getServiceName());
buf.append(provider.getServiceKey());
if (detail) {
buf.append(" -> ");
buf.append(" published: ");
buf.append(isRegistered(provider.getServiceName()) ? "Y" : "N");
buf.append(isRegistered(provider.getServiceKey()) ? "Y" : "N");
}
buf.append("\r\n");
}
@ -103,11 +102,11 @@ public class ListTelnetHandler implements TelnetHandler {
}
for (ConsumerModel consumer : ApplicationModel.allConsumerModels()) {
buf.append(consumer.getServiceName());
buf.append(consumer.getServiceKey());
if (detail) {
buf.append(" -> ");
buf.append(" addresses: ");
buf.append(getConsumerAddressNum(consumer.getServiceName()));
buf.append(getConsumerAddressNum(consumer.getServiceKey()));
}
}
}
@ -120,8 +119,8 @@ public class ListTelnetHandler implements TelnetHandler {
private void printSpecifiedProvidedService(String service, StringBuilder buf, boolean detail) {
for (ProviderModel provider : ApplicationModel.allProviderModels()) {
if (isProviderMatched(service,provider)) {
buf.append(provider.getServiceName()).append(" (as provider):\r\n");
for (ProviderMethodModel method : provider.getAllMethods()) {
buf.append(provider.getServiceKey()).append(" (as provider):\r\n");
for (MethodModel method : provider.getAllMethods()) {
printMethod(method.getMethod(), buf, detail);
}
}
@ -131,8 +130,8 @@ public class ListTelnetHandler implements TelnetHandler {
private void printSpecifiedReferredService(String service, StringBuilder buf, boolean detail) {
for (ConsumerModel consumer : ApplicationModel.allConsumerModels()) {
if (isConsumerMatcher(service,consumer)) {
buf.append(consumer.getServiceName()).append(" (as consumer):\r\n");
for (ConsumerMethodModel method : consumer.getAllMethods()) {
buf.append(consumer.getServiceKey()).append(" (as consumer):\r\n");
for (MethodModel method : consumer.getAllMethods()) {
printMethod(method.getMethod(), buf, detail);
}
}
@ -149,13 +148,13 @@ public class ListTelnetHandler implements TelnetHandler {
}
private boolean isProviderMatched(String service, ProviderModel provider) {
return service.equalsIgnoreCase(provider.getServiceName())
return service.equalsIgnoreCase(provider.getServiceKey())
|| service.equalsIgnoreCase(provider.getServiceInterfaceClass().getName())
|| service.equalsIgnoreCase(provider.getServiceInterfaceClass().getSimpleName());
}
private boolean isConsumerMatcher(String service,ConsumerModel consumer) {
return service.equalsIgnoreCase(consumer.getServiceName())
return service.equalsIgnoreCase(consumer.getServiceKey())
|| service.equalsIgnoreCase(consumer.getServiceInterfaceClass().getName())
|| service.equalsIgnoreCase(consumer.getServiceInterfaceClass().getSimpleName());
}

View File

@ -23,7 +23,6 @@ import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ConsumerMethodModel;
import org.apache.dubbo.rpc.model.ConsumerModel;
import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils;
@ -108,31 +107,31 @@ public class ImplicitCallBackTest {
public void initImplicitCallBackURL_onlyOnthrow() throws Exception {
Map<String, Object> attitudes = new HashMap<>();
ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = new ConsumerMethodModel.AsyncMethodInfo();
ConsumerModel.AsyncMethodInfo asyncMethodInfo = new ConsumerModel.AsyncMethodInfo();
asyncMethodInfo.setOnthrowInstance(notify);
asyncMethodInfo.setOnthrowMethod(onThrowMethod);
attitudes.put("get", asyncMethodInfo);
ApplicationModel.initConsumerModel(consumerUrl.getServiceKey(), new ConsumerModel(consumerUrl.getServiceKey(), IDemoService.class, demoProxy, IDemoService.class.getMethods(), attitudes));
ApplicationModel.initConsumerModel(consumerUrl.getServiceKey(), new ConsumerModel(consumerUrl.getServiceKey(), IDemoService.class, demoProxy, ApplicationModel.registerServiceModel(IDemoService.class), attitudes));
}
//================================================================================================
public void initImplicitCallBackURL_onlyOnreturn() throws Exception {
Map<String, Object> attitudes = new HashMap<>();
ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = new ConsumerMethodModel.AsyncMethodInfo();
ConsumerModel.AsyncMethodInfo asyncMethodInfo = new ConsumerModel.AsyncMethodInfo();
asyncMethodInfo.setOnreturnInstance(notify);
asyncMethodInfo.setOnreturnMethod(onReturnMethod);
attitudes.put("get", asyncMethodInfo);
ApplicationModel.initConsumerModel(consumerUrl.getServiceKey(), new ConsumerModel(consumerUrl.getServiceKey(), IDemoService.class, demoProxy, IDemoService.class.getMethods(), attitudes));
ApplicationModel.initConsumerModel(consumerUrl.getServiceKey(), new ConsumerModel(consumerUrl.getServiceKey(), IDemoService.class, demoProxy, ApplicationModel.registerServiceModel(IDemoService.class), attitudes));
}
public void initImplicitCallBackURL_onlyOninvoke() throws Exception {
Map<String, Object> attitudes = new HashMap<>();
ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = new ConsumerMethodModel.AsyncMethodInfo();
ConsumerModel.AsyncMethodInfo asyncMethodInfo = new ConsumerModel.AsyncMethodInfo();
asyncMethodInfo.setOninvokeInstance(notify);
asyncMethodInfo.setOninvokeMethod(onInvokeMethod);
attitudes.put("get", asyncMethodInfo);
ApplicationModel.initConsumerModel(consumerUrl.getServiceKey(), new ConsumerModel(consumerUrl.getServiceKey(), IDemoService.class, demoProxy, IDemoService.class.getMethods(), attitudes));
ApplicationModel.initConsumerModel(consumerUrl.getServiceKey(), new ConsumerModel(consumerUrl.getServiceKey(), IDemoService.class, demoProxy, ApplicationModel.registerServiceModel(IDemoService.class), attitudes));
}
@Test

View File

@ -27,6 +27,7 @@ import org.apache.dubbo.rpc.model.ProviderModel;
import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService;
import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl;
import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -68,7 +69,7 @@ public class InvokerTelnetHandlerTest {
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel);
String result = invoke.telnet(mockChannel, "echo(\"ok\")");
@ -83,7 +84,7 @@ public class InvokerTelnetHandlerTest {
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel);
String result = invoke.telnet(mockChannel, "DemoService.echo(\"ok\")");
@ -98,7 +99,7 @@ public class InvokerTelnetHandlerTest {
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel);
try {
invoke.telnet(mockChannel, "sayHello(null)");
@ -114,7 +115,7 @@ public class InvokerTelnetHandlerTest {
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel);
String result = invoke.telnet(mockChannel, "getType(\"High\")");
@ -130,7 +131,7 @@ public class InvokerTelnetHandlerTest {
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel);
String result = invoke.telnet(mockChannel, "getPerson({\"name\":\"zhangsan\",\"age\":12,\"class\":\"org.apache.dubbo.rpc.protocol.dubbo.support.Person\"})");
assertTrue(result.contains("result: 12"));
@ -144,7 +145,7 @@ public class InvokerTelnetHandlerTest {
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel);
String param = "{\"name\":\"Dubbo\",\"age\":8}";
String result = invoke.telnet(mockChannel, "getPerson(" + param + ")");
@ -161,7 +162,7 @@ public class InvokerTelnetHandlerTest {
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel);
String param = "{\"name\":\"Dubbo\",\"age\":8},{\"name\":\"Apache\",\"age\":20}";
String result = invoke.telnet(mockChannel, "getPerson(" + param + ")");

View File

@ -27,8 +27,8 @@ import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl;
import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
@ -66,7 +66,7 @@ public class ListTelnetHandlerTest {
mockChannel = mock(Channel.class);
given(mockChannel.getAttribute("telnet.service")).willReturn("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService");
ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel);
String result = list.telnet(mockChannel, "-l DemoService");
@ -80,7 +80,7 @@ public class ListTelnetHandlerTest {
mockChannel = mock(Channel.class);
given(mockChannel.getAttribute("telnet.service")).willReturn("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService");
ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel);
String result = list.telnet(mockChannel, "DemoService");
@ -94,7 +94,7 @@ public class ListTelnetHandlerTest {
mockChannel = mock(Channel.class);
given(mockChannel.getAttribute("telnet.service")).willReturn(null);
ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel);
String result = list.telnet(mockChannel, "");
@ -106,7 +106,7 @@ public class ListTelnetHandlerTest {
mockChannel = mock(Channel.class);
given(mockChannel.getAttribute("telnet.service")).willReturn(null);
ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel);
String result = list.telnet(mockChannel, "-l");
@ -118,7 +118,7 @@ public class ListTelnetHandlerTest {
mockChannel = mock(Channel.class);
given(mockChannel.getAttribute("telnet.service")).willReturn("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService");
ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService", providerModel);
String result = list.telnet(mockChannel, "");

View File

@ -25,6 +25,7 @@ import org.apache.dubbo.rpc.model.ProviderModel;
import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService;
import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl;
import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -71,7 +72,7 @@ public class SelectTelnetHandlerTest {
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel);
String result = select.telnet(mockChannel, "1");
@ -86,7 +87,7 @@ public class SelectTelnetHandlerTest {
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel);
String result = select.telnet(mockChannel, "index");
@ -107,7 +108,7 @@ public class SelectTelnetHandlerTest {
given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class);
ProviderModel providerModel = new ProviderModel(DemoService.class.getName(), new DemoServiceImpl(), ApplicationModel.registerServiceModel(DemoService.class));
ApplicationModel.initProviderModel(DemoService.class.getName(), providerModel);
String result = select.telnet(mockChannel, null);

View File

@ -36,11 +36,10 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.apache.dubbo.remoting.Constants.SERVER_KEY;
import static org.apache.dubbo.rpc.protocol.rest.Constants.EXTENSION_KEY;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.apache.dubbo.rpc.protocol.rest.Constants.EXTENSION_KEY;
public class RestProtocolTest {
private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest");
private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
@ -163,7 +162,7 @@ public class RestProtocolTest {
Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, exportUrl));
RpcInvocation rpcInvocation = new RpcInvocation("hello", new Class[]{Integer.class, Integer.class}, new Integer[]{2, 3});
RpcInvocation rpcInvocation = new RpcInvocation("hello", DemoService.class.getName(), new Class[]{Integer.class, Integer.class}, new Integer[]{2, 3});
Result result = exporter.getInvoker().invoke(rpcInvocation);
assertThat(result.getValue(), CoreMatchers.<Object>is(5));