Java Code Examples for com.alibaba.dubbo.registry.integration.RegistryDirectory#notify()

The following examples show how to use com.alibaba.dubbo.registry.integration.RegistryDirectory#notify() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * When the first arg of a method is String or Enum, Registry server can do parameter-value-based routing.
 */
@Test
public void testParmeterRoute() {
    RegistryDirectory registryDirectory = getRegistryDirectory();
    List<URL> serviceUrls = new ArrayList<URL>();
    serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1.napoli"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1.MORGAN,getXXX2"));
    serviceUrls.add(SERVICEURL3.addParameter("methods", "getXXX1.morgan,getXXX2,getXXX3"));

    registryDirectory.notify(serviceUrls);

    invocation = new RpcInvocation(
                                   Constants.$INVOKE,
                                   new Class[] { String.class, String[].class, Object[].class },
                                   new Object[] { "getXXX1", new String[] { "Enum" }, new Object[] { Param.MORGAN } });

    List invokers = registryDirectory.list(invocation);
    Assert.assertEquals(1, invokers.size());
}
 
Example 2
Source File: RegistryDirectoryTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
/**
 * 测试清除override规则,同时下发清除规则和其他override规则
 * 测试是否能够恢复到推送时的providerUrl
 */
@Test
public void testNofityOverrideUrls_Clean1(){
    RegistryDirectory registryDirectory = getRegistryDirectory();
    invocation = new RpcInvocation();
    
    List<URL> durls = new ArrayList<URL>();
    durls.add(SERVICEURL.setHost("10.20.30.140").addParameter("timeout", "1"));
    registryDirectory.notify(durls);
    
    durls = new ArrayList<URL>();
    durls.add(URL.valueOf("override://0.0.0.0?timeout=1000"));
    registryDirectory.notify(durls);
    
    durls = new ArrayList<URL>();
    durls.add(URL.valueOf("override://0.0.0.0?timeout=3"));
    durls.add(URL.valueOf("override://0.0.0.0"));
    registryDirectory.notify(durls);
    
    List<Invoker<?>> invokers = registryDirectory.list(invocation);
    Invoker<?> aInvoker = invokers.get(0);
    //需要恢复到最初的providerUrl
    Assert.assertEquals("1",aInvoker.getUrl().getParameter("timeout"));
}
 
Example 3
Source File: RegistryDirectoryTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void test_Notified_acceptProtocol2() {
	URL errorPathUrl  = URL.valueOf("notsupport:/xxx");
	errorPathUrl = errorPathUrl.addParameterAndEncoded(Constants.REFER_KEY, "interface="+service + "&protocol=dubbo,injvm");
    RegistryDirectory registryDirectory = getRegistryDirectory(errorPathUrl);
    List<URL> serviceUrls = new ArrayList<URL>();
    URL dubbo1URL = URL.valueOf("dubbo://127.0.0.1:9098?lazy=true&methods=getXXX");
    URL dubbo2URL = URL.valueOf("injvm://127.0.0.1:9099?lazy=true&methods=getXXX");
    serviceUrls.add(dubbo1URL);
    serviceUrls.add(dubbo2URL);
    registryDirectory.notify(serviceUrls);

    invocation = new RpcInvocation();

    List<Invoker<DemoService>> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
}
 
Example 4
Source File: DubboRegistryFactory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Registry createRegistry(URL url) {
    url = getRegistryURL(url);
    List<URL> urls = new ArrayList<URL>();
    urls.add(url.removeParameter(Constants.BACKUP_KEY));
    String backup = url.getParameter(Constants.BACKUP_KEY);
    if (backup != null && backup.length() > 0) {
        String[] addresses = Constants.COMMA_SPLIT_PATTERN.split(backup);
        for (String address : addresses) {
            urls.add(url.setAddress(address));
        }
    }
    RegistryDirectory<RegistryService> directory = new RegistryDirectory<RegistryService>(RegistryService.class, url.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()).addParameterAndEncoded(Constants.REFER_KEY, url.toParameterString()));
    Invoker<RegistryService> registryInvoker = cluster.join(directory);
    RegistryService registryService = proxyFactory.getProxy(registryInvoker);
    DubboRegistry registry = new DubboRegistry(registryInvoker, registryService);
    directory.setRegistry(registry);
    directory.setProtocol(protocol);
    directory.notify(urls);
    directory.subscribe(new URL(Constants.CONSUMER_PROTOCOL, NetUtils.getLocalHost(), 0, RegistryService.class.getName(), url.getParameters()));
    return registry;
}
 
Example 5
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_Notified_acceptProtocol2() {
	URL errorPathUrl  = URL.valueOf("notsupport:/xxx");
	errorPathUrl = errorPathUrl.addParameterAndEncoded(Constants.REFER_KEY, "interface="+service + "&protocol=dubbo,injvm");
    RegistryDirectory registryDirectory = getRegistryDirectory(errorPathUrl);
    List<URL> serviceUrls = new ArrayList<URL>();
    URL dubbo1URL = URL.valueOf("dubbo://127.0.0.1:9098?lazy=true&methods=getXXX");
    URL dubbo2URL = URL.valueOf("injvm://127.0.0.1:9099?lazy=true&methods=getXXX");
    serviceUrls.add(dubbo1URL);
    serviceUrls.add(dubbo2URL);
    registryDirectory.notify(serviceUrls);

    invocation = new RpcInvocation();

    List<Invoker<DemoService>> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
}
 
Example 6
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_NotifiedDubbo1() {
    URL             errorPathUrl            = URL.valueOf("notsupport:/" + "xxx"+"?refer=" + URL.encode("interface="+service));
    RegistryDirectory registryDirectory = getRegistryDirectory(errorPathUrl);
    List<URL> serviceUrls = new ArrayList<URL>();
    URL Dubbo1URL = URL.valueOf("dubbo://127.0.0.1:9098?lazy=true");
    serviceUrls.add(Dubbo1URL.addParameter("methods", "getXXX"));
    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());

    invocation = new RpcInvocation();

    List<Invoker<DemoService>> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(1, invokers.size());

    invocation.setMethodName("getXXX");
    invokers = registryDirectory.list(invocation);
    Assert.assertEquals(1, invokers.size());
    Assert.assertEquals(DemoService.class.getName(), invokers.get(0).getUrl().getPath());
}
 
Example 7
Source File: RegistryDirectoryTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void test_NotifiedDubbo1() {
    URL             errorPathUrl            = URL.valueOf("notsupport:/" + "xxx"+"?refer=" + URL.encode("interface="+service));
    RegistryDirectory registryDirectory = getRegistryDirectory(errorPathUrl);
    List<URL> serviceUrls = new ArrayList<URL>();
    URL Dubbo1URL = URL.valueOf("dubbo://127.0.0.1:9098?lazy=true");
    serviceUrls.add(Dubbo1URL.addParameter("methods", "getXXX"));
    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());

    invocation = new RpcInvocation();

    List<Invoker<DemoService>> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(1, invokers.size());

    invocation.setMethodName("getXXX");
    invokers = registryDirectory.list(invocation);
    Assert.assertEquals(1, invokers.size());
    Assert.assertEquals(DemoService.class.getName(), invokers.get(0).getUrl().getPath());
}
 
Example 8
Source File: RegistryDirectoryTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
private void test_Notified2invokers(RegistryDirectory registryDirectory) {
    List<URL> serviceUrls = new ArrayList<URL>();
    serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));

    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());

    invocation = new RpcInvocation();

    List invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());

    invocation.setMethodName("getXXX");
    invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());

    invocation.setMethodName("getXXX1");
    invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());

    invocation.setMethodName("getXXX2");
    invokers = registryDirectory.list(invocation);
    Assert.assertEquals(1, invokers.size());
}
 
Example 9
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_Notified_acceptProtocol2() {
	URL errorPathUrl  = URL.valueOf("notsupport:/xxx");
	errorPathUrl = errorPathUrl.addParameterAndEncoded(Constants.REFER_KEY, "interface="+service + "&protocol=dubbo,injvm");
    RegistryDirectory registryDirectory = getRegistryDirectory(errorPathUrl);
    List<URL> serviceUrls = new ArrayList<URL>();
    URL dubbo1URL = URL.valueOf("dubbo://127.0.0.1:9098?lazy=true&methods=getXXX");
    URL dubbo2URL = URL.valueOf("injvm://127.0.0.1:9099?lazy=true&methods=getXXX");
    serviceUrls.add(dubbo1URL);
    serviceUrls.add(dubbo2URL);
    registryDirectory.notify(serviceUrls);

    invocation = new RpcInvocation();

    List<Invoker<DemoService>> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
}
 
Example 10
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_Notified_acceptProtocol0() {
	URL errorPathUrl  = URL.valueOf("notsupport:/xxx?refer=" + URL.encode("interface="+service));
    RegistryDirectory registryDirectory = getRegistryDirectory(errorPathUrl);
    List<URL> serviceUrls = new ArrayList<URL>();
    URL dubbo1URL = URL.valueOf("dubbo://127.0.0.1:9098?lazy=true&methods=getXXX");
    URL dubbo2URL = URL.valueOf("injvm://127.0.0.1:9099?lazy=true&methods=getXXX");
    serviceUrls.add(dubbo1URL);
    serviceUrls.add(dubbo2URL);
    registryDirectory.notify(serviceUrls);

    invocation = new RpcInvocation();

    List<Invoker<DemoService>> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
}
 
Example 11
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * 测试override规则是否优先
 * 场景:与invoker 一起推override规则
 */
@Test
public void testNotifyoverrideUrls_withInvoker(){
    RegistryDirectory registryDirectory = getRegistryDirectory();
    
    List<URL> durls = new ArrayList<URL>();
    durls.add(SERVICEURL.addParameter("timeout", "1000"));
    durls.add(SERVICEURL2.addParameter("timeout", "1000").addParameter("connections", "10"));
    durls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5"));

    registryDirectory.notify(durls);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    
    //开始验证参数值

    invocation = new RpcInvocation();

    List<Invoker<?>> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
    
    Assert.assertEquals("override rute must be first priority", "1", invokers.get(0).getUrl().getParameter("timeout"));
    Assert.assertEquals("override rute must be first priority", "5", invokers.get(0).getUrl().getParameter("connections"));
}
 
Example 12
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotified_WithError() {
    RegistryDirectory registryDirectory = getRegistryDirectory();
    List<URL> serviceUrls = new ArrayList<URL>();
    // ignore error log
    URL badurl = URL.valueOf("notsupported://127.0.0.1/" + service);
    serviceUrls.add(badurl);
    serviceUrls.add(SERVICEURL);

    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    List invokers = registryDirectory.list(invocation);
    Assert.assertEquals(1, invokers.size());
}
 
Example 13
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * 测试override规则是否优先
 * 场景:先推送override,后推送invoker
 */
@Test
public void testNotifyoverrideUrls_beforeInvoker(){
    RegistryDirectory registryDirectory = getRegistryDirectory();
    List<URL> overrideUrls = new ArrayList<URL>();
    overrideUrls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5"));
    registryDirectory.notify(overrideUrls);
    //注册中心初始只推送override,dirctory状态应该是false,因为没有invoker存在。
    Assert.assertEquals(false, registryDirectory.isAvailable());
    
    //在推送两个provider,directory状态恢复为true
    List<URL> serviceUrls = new ArrayList<URL>();
    serviceUrls.add(SERVICEURL.addParameter("timeout", "1000"));
    serviceUrls.add(SERVICEURL2.addParameter("timeout", "1000").addParameter("connections", "10"));

    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    
    //开始验证参数值

    invocation = new RpcInvocation();

    List<Invoker<?>> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
    
    Assert.assertEquals("override rute must be first priority", "1", invokers.get(0).getUrl().getParameter("timeout"));
    Assert.assertEquals("override rute must be first priority", "5", invokers.get(0).getUrl().getParameter("connections"));
}
 
Example 14
Source File: RegistryDirectoryTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
/**
 * Empty notify cause forbidden, non-empty notify cancels forbidden state
 */
@Test
public void testEmptyNotifyCauseForbidden() {
    RegistryDirectory registryDirectory = getRegistryDirectory();
    List invokers = null;

    List<URL> serviceUrls = new ArrayList<URL>();
    registryDirectory.notify(serviceUrls);

    RpcInvocation inv = new RpcInvocation();
    try {
        invokers = registryDirectory.list(inv);
    } catch (RpcException e) {
        Assert.assertEquals(RpcException.FORBIDDEN_EXCEPTION, e.getCode());
        Assert.assertEquals(false, registryDirectory.isAvailable());
    }

    serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
    serviceUrls.add(SERVICEURL3.addParameter("methods", "getXXX1,getXXX2,getXXX3"));

    registryDirectory.notify(serviceUrls);
    inv.setMethodName("getXXX2");
    invokers = registryDirectory.list(inv);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    Assert.assertEquals(2, invokers.size());
}
 
Example 15
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotified_WithError() {
    RegistryDirectory registryDirectory = getRegistryDirectory();
    List<URL> serviceUrls = new ArrayList<URL>();
    // ignore error log
    URL badurl = URL.valueOf("notsupported://127.0.0.1/" + service);
    serviceUrls.add(badurl);
    serviceUrls.add(SERVICEURL);

    registryDirectory.notify(serviceUrls);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    List invokers = registryDirectory.list(invocation);
    Assert.assertEquals(1, invokers.size());
}
 
Example 16
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * When destroying, RegistryDirectory should: 1. be disconnected from Registry 2. destroy all invokers
 */
@Test
public void testDestroy() {
    RegistryDirectory registryDirectory = getRegistryDirectory();

    List<URL> serviceUrls = new ArrayList<URL>();
    serviceUrls.add(SERVICEURL.addParameter("methods", "getXXX1"));
    serviceUrls.add(SERVICEURL2.addParameter("methods", "getXXX1,getXXX2"));
    serviceUrls.add(SERVICEURL3.addParameter("methods", "getXXX1,getXXX2,getXXX3"));

    registryDirectory.notify(serviceUrls);
    List<Invoker> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(true, registryDirectory.isAvailable());
    Assert.assertEquals(true, invokers.get(0).isAvailable());

    registryDirectory.destroy();
    Assert.assertEquals(false, registryDirectory.isAvailable());
    Assert.assertEquals(false, invokers.get(0).isAvailable());
    registryDirectory.destroy();

    Map<String, List<Invoker<RegistryDirectoryTest>>> methodInvokerMap = registryDirectory.getMethodInvokerMap();
    Map<String, Invoker<RegistryDirectoryTest>> urlInvokerMap = registryDirectory.getUrlInvokerMap();

    Assert.assertTrue(methodInvokerMap == null);
    Assert.assertEquals(0, urlInvokerMap.size());
    // List<U> urls = mockRegistry.getSubscribedUrls();

    RpcInvocation inv = new RpcInvocation();
    try {
        registryDirectory.list(inv);
        fail();
    } catch (RpcException e) {
        Assert.assertTrue(e.getMessage().contains("already destroyed"));
    }
}
 
Example 17
Source File: RegistryDirectoryTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
/**
 * 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>();
    durls.add(SERVICEURL.addParameter("timeout", "1"));//One is the same, one is different
    durls.add(SERVICEURL2.addParameter("timeout", "1").addParameter("connections", "5"));
    registryDirectory.notify(durls);
    List<Invoker<?>> invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());
    Invoker<?> a1Invoker = invokers.get(0);
    Invoker<?> b1Invoker = invokers.get(1);

    durls = new ArrayList<URL>();
    durls.add(URL.valueOf("override://0.0.0.0?timeout=1&connections=5"));
    registryDirectory.notify(durls);
    Assert.assertEquals(true, registryDirectory.isAvailable());

    invokers = registryDirectory.list(invocation);
    Assert.assertEquals(2, invokers.size());

    Invoker<?> a2Invoker = invokers.get(0);
    Invoker<?> b2Invoker = invokers.get(1);
    //The parameters are different and must be rereferenced.
    Assert.assertFalse("object not same", a1Invoker == a2Invoker);

    //The parameters can not be rereferenced
    Assert.assertTrue("object same", b1Invoker == b2Invoker);
}
 
Example 18
Source File: RegistryDirectoryTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotified_WithDuplicateUrls() {
    List<URL> serviceUrls = new ArrayList<URL>();
    // ignore error log
    serviceUrls.add(SERVICEURL);
    serviceUrls.add(SERVICEURL);

    RegistryDirectory registryDirectory = getRegistryDirectory();
    registryDirectory.notify(serviceUrls);
    List invokers = registryDirectory.list(invocation);
    Assert.assertEquals(1, invokers.size());
}
 
Example 19
Source File: RegistryDirectoryTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
/**
 * The test clears the override rule and only sends the override cleanup rules
 * Whether the test can be restored to the providerUrl when it is pushed
 */
@Test
public void testNofityOverrideUrls_CleanOnly() {
    RegistryDirectory registryDirectory = getRegistryDirectory();
    invocation = new RpcInvocation();

    List<URL> durls = new ArrayList<URL>();
    durls.add(SERVICEURL.setHost("10.20.30.140").addParameter("timeout", "1"));
    registryDirectory.notify(durls);
    Assert.assertEquals(null, registryDirectory.getUrl().getParameter("mock"));

    //override
    durls = new ArrayList<URL>();
    durls.add(URL.valueOf("override://0.0.0.0?timeout=1000&mock=fail"));
    registryDirectory.notify(durls);
    List<Invoker<?>> invokers = registryDirectory.list(invocation);
    Invoker<?> aInvoker = invokers.get(0);
    Assert.assertEquals("1000", aInvoker.getUrl().getParameter("timeout"));
    Assert.assertEquals("fail", registryDirectory.getUrl().getParameter("mock"));

    //override clean
    durls = new ArrayList<URL>();
    durls.add(URL.valueOf("override://0.0.0.0/dubbo.test.api.HelloService"));
    registryDirectory.notify(durls);
    invokers = registryDirectory.list(invocation);
    aInvoker = invokers.get(0);
    //Need to be restored to the original providerUrl
    Assert.assertEquals("1", aInvoker.getUrl().getParameter("timeout"));

    Assert.assertEquals(null, registryDirectory.getUrl().getParameter("mock"));
}
 
Example 20
Source File: RegistryDirectoryTest.java    From dubbox with Apache License 2.0 4 votes vote down vote up
private void test_Notified_only_routers(RegistryDirectory registryDirectory) {
    List<URL> serviceUrls = new ArrayList<URL>();
    serviceUrls.add(URL.valueOf("empty://127.0.0.1/?category=routers"));
    registryDirectory.notify(serviceUrls);
}