use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.
the class AbstractLoadBalanceTest method testGetRegistryWeight.
@Test
public void testGetRegistryWeight() {
RpcInvocation invocation = new RpcInvocation();
invocation.setMethodName("say");
Invoker invoker1 = mock(Invoker.class, Mockito.withSettings().stubOnly());
URL url1 = new URL("", "", 0, "DemoService", new HashMap<>());
url1 = url1.addParameter(REGISTRY_KEY + "." + WEIGHT_KEY, 10);
given(invoker1.getUrl()).willReturn(url1);
Invoker invoker2 = mock(Invoker.class, Mockito.withSettings().stubOnly());
URL url2 = new URL("", "", 0, "org.apache.dubbo.registry.RegistryService", new HashMap<>());
url2 = url2.addParameter(REGISTRY_KEY + "." + WEIGHT_KEY, 20);
given(invoker2.getUrl()).willReturn(url2);
Assertions.assertEquals(100, balance.getWeight(invoker1, invocation));
Assertions.assertEquals(20, balance.getWeight(invoker2, invocation));
}
use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.
the class LeastActiveBalanceTest method testSelectByWeight.
@Test
public void testSelectByWeight() {
int sumInvoker1 = 0;
int sumInvoker2 = 0;
int loop = 10000;
LeastActiveLoadBalance lb = new LeastActiveLoadBalance();
for (int i = 0; i < loop; i++) {
Invoker selected = lb.select(weightInvokers, null, weightTestInvocation);
if (selected.getUrl().getProtocol().equals("test1")) {
sumInvoker1++;
}
if (selected.getUrl().getProtocol().equals("test2")) {
sumInvoker2++;
}
// never select invoker3 because it's active is more than invoker1 and invoker2
Assertions.assertTrue(!selected.getUrl().getProtocol().equals("test3"), "select is not the least active one");
}
// the sumInvoker1 : sumInvoker2 approximately equal to 1: 9
System.out.println(sumInvoker1);
System.out.println(sumInvoker2);
Assertions.assertEquals(sumInvoker1 + sumInvoker2, loop, "select failed!");
}
use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.
the class RandomLoadBalanceTest method testRandomLoadBalanceSelect.
@Test
public void testRandomLoadBalanceSelect() {
int runs = 1000;
Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, RandomLoadBalance.NAME);
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
Assertions.assertTrue(Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size()), "abs diff should < avg");
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j <= i; j++) {
RpcStatus.beginCount(invokers.get(i).getUrl(), invocation.getMethodName());
}
}
counter = getInvokeCounter(runs, LeastActiveLoadBalance.NAME);
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
}
Assertions.assertEquals(runs, counter.get(invoker1).intValue());
Assertions.assertEquals(0, counter.get(invoker2).intValue());
Assertions.assertEquals(0, counter.get(invoker3).intValue());
Assertions.assertEquals(0, counter.get(invoker4).intValue());
Assertions.assertEquals(0, counter.get(invoker5).intValue());
}
use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.
the class ReferenceConfig method createProxy.
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
private T createProxy(Map<String, String> map) {
if (shouldJvmRefer(map)) {
URL url = new URL(LOCAL_PROTOCOL, LOCALHOST_VALUE, 0, interfaceClass.getName()).addParameters(map);
invoker = REF_PROTOCOL.refer(interfaceClass, url);
if (logger.isInfoEnabled()) {
logger.info("Using injvm service " + interfaceClass.getName());
}
} else {
urls.clear();
if (url != null && url.length() > 0) {
// user specified URL, could be peer-to-peer address, or register center's address.
String[] us = SEMICOLON_SPLIT_PATTERN.split(url);
if (us != null && us.length > 0) {
for (String u : us) {
URL url = URL.valueOf(u);
if (StringUtils.isEmpty(url.getPath())) {
url = url.setPath(interfaceName);
}
if (UrlUtils.isRegistry(url)) {
urls.add(url.addParameterAndEncoded(REFER_KEY, StringUtils.toQueryString(map)));
} else {
urls.add(ClusterUtils.mergeUrl(url, map));
}
}
}
} else {
// if protocols not injvm checkRegistry
if (!LOCAL_PROTOCOL.equalsIgnoreCase(getProtocol())) {
checkRegistry();
List<URL> us = ConfigValidationUtils.loadRegistries(this, false);
if (CollectionUtils.isNotEmpty(us)) {
for (URL u : us) {
URL monitorUrl = ConfigValidationUtils.loadMonitor(this, u);
if (monitorUrl != null) {
map.put(MONITOR_KEY, URL.encode(monitorUrl.toFullString()));
}
urls.add(u.addParameterAndEncoded(REFER_KEY, StringUtils.toQueryString(map)));
}
}
if (urls.isEmpty()) {
throw new IllegalStateException("No such any registry to reference " + interfaceName + " on the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", please config <dubbo:registry address=\"...\" /> to your spring config.");
}
}
}
if (urls.size() == 1) {
invoker = REF_PROTOCOL.refer(interfaceClass, urls.get(0));
} else {
List<Invoker<?>> invokers = new ArrayList<Invoker<?>>();
URL registryURL = null;
for (URL url : urls) {
// For multi-registry scenarios, it is not checked whether each referInvoker is available.
// Because this invoker may become available later.
invokers.add(REF_PROTOCOL.refer(interfaceClass, url));
if (UrlUtils.isRegistry(url)) {
// use last registry url
registryURL = url;
}
}
if (registryURL != null) {
// registry url is available
// for multi-subscription scenario, use 'zone-aware' policy by default
String cluster = registryURL.getParameter(CLUSTER_KEY, ZoneAwareCluster.NAME);
// The invoker wrap sequence would be: ZoneAwareClusterInvoker(StaticDirectory) -> FailoverClusterInvoker(RegistryDirectory, routing happens here) -> Invoker
invoker = Cluster.getCluster(cluster, false).join(new StaticDirectory(registryURL, invokers));
} else {
// not a registry url, must be direct invoke.
String cluster = CollectionUtils.isNotEmpty(invokers) ? (invokers.get(0).getUrl() != null ? invokers.get(0).getUrl().getParameter(CLUSTER_KEY, ZoneAwareCluster.NAME) : Cluster.DEFAULT) : Cluster.DEFAULT;
invoker = Cluster.getCluster(cluster).join(new StaticDirectory(invokers));
}
}
}
if (logger.isInfoEnabled()) {
logger.info("Refer dubbo service " + interfaceClass.getName() + " from url " + invoker.getUrl());
}
URL consumerURL = new URL(CONSUMER_PROTOCOL, map.remove(REGISTER_IP_KEY), 0, map.get(INTERFACE_KEY), map);
MetadataUtils.publishServiceDefinition(consumerURL);
// create service proxy
return (T) PROXY_FACTORY.getProxy(invoker, ProtocolUtils.isGeneric(generic));
}
use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.
the class GenericServiceTest method testGenericCompatible.
@Test
public void testGenericCompatible() {
DemoService server = new DemoServiceImpl();
ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();
URL url = URL.valueOf("dubbo://127.0.0.1:5342/" + DemoService.class.getName() + "?version=1.0.0&generic=true$timeout=3000");
Exporter<DemoService> exporter = protocol.export(proxyFactory.getInvoker(server, DemoService.class, url));
// simulate normal invoke
ReferenceConfig<com.alibaba.dubbo.rpc.service.GenericService> oldReferenceConfig = new ReferenceConfig<>();
oldReferenceConfig.setGeneric(true);
oldReferenceConfig.setInterface(DemoService.class.getName());
oldReferenceConfig.checkAndUpdateSubConfigs();
Invoker invoker = protocol.refer(oldReferenceConfig.getInterfaceClass(), url);
com.alibaba.dubbo.rpc.service.GenericService client = (com.alibaba.dubbo.rpc.service.GenericService) proxyFactory.getProxy(invoker, true);
Object result = client.$invoke("sayHello", new String[] { "java.lang.String" }, new Object[] { "haha" });
Assertions.assertEquals("hello haha", result);
invoker.destroy();
exporter.unexport();
}
Aggregations