use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.
the class RegistryDirectoryTest method testNofityOverrideUrls_disabled_specifiedProvider.
/**
* Test override disables a specified service provider through enable=false
* It is expected that a specified service provider can be disable.
*/
@Test
public void testNofityOverrideUrls_disabled_specifiedProvider() {
RegistryDirectory registryDirectory = getRegistryDirectory();
invocation = new RpcInvocation();
List<URL> durls = new ArrayList<URL>();
durls.add(SERVICEURL.setHost("10.20.30.140"));
durls.add(SERVICEURL.setHost("10.20.30.141"));
registryDirectory.notify(durls);
durls = new ArrayList<URL>();
durls.add(URL.valueOf("override://10.20.30.140:9091?" + DISABLED_KEY + "=true"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(1, invokers.size());
Assertions.assertEquals("10.20.30.141", invokers.get(0).getUrl().getHost());
durls = new ArrayList<URL>();
durls.add(URL.valueOf("empty://0.0.0.0?" + DISABLED_KEY + "=true&" + CATEGORY_KEY + "=" + CONFIGURATORS_CATEGORY));
registryDirectory.notify(durls);
List<Invoker<?>> invokers2 = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers2.size());
}
use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.
the class RegistryDirectoryTest method test_Notified_withGroupFilter.
@Test
public void test_Notified_withGroupFilter() {
URL directoryUrl = noMeaningUrl.addParameterAndEncoded(REFER_KEY, "interface" + service + "&group=group1,group2");
RegistryDirectory directory = this.getRegistryDirectory(directoryUrl);
URL provider1 = URL.valueOf("dubbo://10.134.108.1:20880/" + service + "?methods=getXXX&group=group1&mock=false&application=mockApplication");
URL provider2 = URL.valueOf("dubbo://10.134.108.1:20880/" + service + "?methods=getXXX&group=group2&mock=false&application=mockApplication");
List<URL> providers = new ArrayList<>();
providers.add(provider1);
providers.add(provider2);
directory.notify(providers);
invocation = new RpcInvocation();
invocation.setMethodName("getXXX");
List<Invoker<DemoService>> invokers = directory.list(invocation);
Assertions.assertEquals(2, invokers.size());
Assertions.assertTrue(invokers.get(0) instanceof MockClusterInvoker);
Assertions.assertTrue(invokers.get(1) instanceof MockClusterInvoker);
directoryUrl = noMeaningUrl.addParameterAndEncoded(REFER_KEY, "interface" + service + "&group=group1");
directory = this.getRegistryDirectory(directoryUrl);
directory.notify(providers);
invokers = directory.list(invocation);
Assertions.assertEquals(2, invokers.size());
Assertions.assertFalse(invokers.get(0) instanceof MockClusterInvoker);
Assertions.assertFalse(invokers.get(1) instanceof MockClusterInvoker);
}
use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.
the class RegistryDirectoryTest method testNotifyoverrideUrls_Nouse.
/**
* Test whether the override rule have a high priority
* Scene: the rules of the push are the same as the parameters of the provider
* Expectation: no need to be re-referenced
*/
@Test
public void testNotifyoverrideUrls_Nouse() {
RegistryDirectory registryDirectory = getRegistryDirectory();
invocation = new RpcInvocation();
List<URL> durls = new ArrayList<URL>();
// One is the same, one is different
durls.add(SERVICEURL.addParameter("timeout", "1"));
durls.add(SERVICEURL2.addParameter("timeout", "1").addParameter("connections", "5"));
registryDirectory.notify(durls);
List<Invoker<?>> invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
Map<String, Invoker<?>> map = new HashMap<>();
map.put(invokers.get(0).getUrl().getAddress(), invokers.get(0));
map.put(invokers.get(1).getUrl().getAddress(), invokers.get(1));
durls = new ArrayList<URL>();
durls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5"));
registryDirectory.notify(durls);
Assertions.assertTrue(registryDirectory.isAvailable());
invokers = registryDirectory.list(invocation);
Assertions.assertEquals(2, invokers.size());
Map<String, Invoker<?>> map2 = new HashMap<>();
map2.put(invokers.get(0).getUrl().getAddress(), invokers.get(0));
map2.put(invokers.get(1).getUrl().getAddress(), invokers.get(1));
// The parameters are different and must be rereferenced.
Assertions.assertNotSame(map.get(SERVICEURL.getAddress()), map2.get(SERVICEURL.getAddress()), "object should not same");
// The parameters can not be rereferenced
Assertions.assertSame(map.get(SERVICEURL2.getAddress()), map2.get(SERVICEURL2.getAddress()), "object should not same");
}
use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.
the class StaticDirectoryTest method testStaticDirectory.
@Test
public void testStaticDirectory() {
Router router = new ConditionRouterFactory().getRouter(getRouteUrl(" => " + " host = " + NetUtils.getLocalHost()));
List<Router> routers = new ArrayList<Router>();
routers.add(router);
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> 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);
invokers.add(invoker2);
invokers.add(invoker3);
List<Invoker<String>> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
StaticDirectory<String> staticDirectory = new StaticDirectory<>(filteredInvokers);
boolean isAvailable = staticDirectory.isAvailable();
Assertions.assertTrue(!isAvailable);
List<Invoker<String>> newInvokers = staticDirectory.list(new MockDirInvocation());
Assertions.assertTrue(newInvokers.size() > 0);
staticDirectory.destroy();
Assertions.assertEquals(0, newInvokers.size());
}
use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.
the class FailoverClusterInvoker method doInvoke.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Result doInvoke(Invocation invocation, final List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
List<Invoker<T>> copyInvokers = invokers;
checkInvokers(copyInvokers, invocation);
String methodName = RpcUtils.getMethodName(invocation);
int len = calculateInvokeTimes(methodName);
// retry loop.
// last exception.
RpcException le = null;
// invoked invokers.
List<Invoker<T>> invoked = new ArrayList<Invoker<T>>(copyInvokers.size());
Set<String> providers = new HashSet<String>(len);
for (int i = 0; i < len; i++) {
// NOTE: if `invokers` changed, then `invoked` also lose accuracy.
if (i > 0) {
checkWhetherDestroyed();
copyInvokers = list(invocation);
// check again
checkInvokers(copyInvokers, invocation);
}
Invoker<T> invoker = select(loadbalance, invocation, copyInvokers, invoked);
invoked.add(invoker);
RpcContext.getContext().setInvokers((List) invoked);
try {
Result result = invoker.invoke(invocation);
if (le != null && logger.isWarnEnabled()) {
logger.warn("Although retry the method " + methodName + " in the service " + getInterface().getName() + " was successful by the provider " + invoker.getUrl().getAddress() + ", but there have been failed providers " + providers + " (" + providers.size() + "/" + copyInvokers.size() + ") from the registry " + directory.getUrl().getAddress() + " on the consumer " + NetUtils.getLocalHost() + " using the dubbo version " + Version.getVersion() + ". Last error is: " + le.getMessage(), le);
}
return result;
} catch (RpcException e) {
if (e.isBiz()) {
// biz exception.
throw e;
}
le = e;
} catch (Throwable e) {
le = new RpcException(e.getMessage(), e);
} finally {
providers.add(invoker.getUrl().getAddress());
}
}
throw new RpcException(le.getCode(), "Failed to invoke the method " + methodName + " in the service " + getInterface().getName() + ". Tried " + len + " times of the providers " + providers + " (" + providers.size() + "/" + copyInvokers.size() + ") from the registry " + directory.getUrl().getAddress() + " on the consumer " + NetUtils.getLocalHost() + " using the dubbo version " + Version.getVersion() + ". Last error is: " + le.getMessage(), le.getCause() != null ? le.getCause() : le);
}
Aggregations