Search in sources :

Example 6 with Router

use of com.alibaba.dubbo.rpc.cluster.Router in project dubbo by alibaba.

the class RegistryDirectory method notify.

public synchronized void notify(List<URL> urls) {
    List<URL> invokerUrls = new ArrayList<URL>();
    List<URL> routerUrls = new ArrayList<URL>();
    List<URL> configuratorUrls = new ArrayList<URL>();
    for (URL url : urls) {
        String protocol = url.getProtocol();
        String category = url.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY);
        if (Constants.ROUTERS_CATEGORY.equals(category) || Constants.ROUTE_PROTOCOL.equals(protocol)) {
            routerUrls.add(url);
        } else if (Constants.CONFIGURATORS_CATEGORY.equals(category) || Constants.OVERRIDE_PROTOCOL.equals(protocol)) {
            configuratorUrls.add(url);
        } else if (Constants.PROVIDERS_CATEGORY.equals(category)) {
            invokerUrls.add(url);
        } else {
            logger.warn("Unsupported category " + category + " in notified url: " + url + " from registry " + getUrl().getAddress() + " to consumer " + NetUtils.getLocalHost());
        }
    }
    // configurators 
    if (configuratorUrls != null && configuratorUrls.size() > 0) {
        this.configurators = toConfigurators(configuratorUrls);
    }
    // routers
    if (routerUrls != null && routerUrls.size() > 0) {
        List<Router> routers = toRouters(routerUrls);
        if (routers != null) {
            // null - do nothing
            setRouters(routers);
        }
    }
    // local reference
    List<Configurator> localConfigurators = this.configurators;
    // 合并override参数
    this.overrideDirectoryUrl = directoryUrl;
    if (localConfigurators != null && localConfigurators.size() > 0) {
        for (Configurator configurator : localConfigurators) {
            this.overrideDirectoryUrl = configurator.configure(overrideDirectoryUrl);
        }
    }
    // providers
    refreshInvoker(invokerUrls);
}
Also used : Configurator(com.alibaba.dubbo.rpc.cluster.Configurator) ArrayList(java.util.ArrayList) Router(com.alibaba.dubbo.rpc.cluster.Router) URL(com.alibaba.dubbo.common.URL)

Example 7 with Router

use of com.alibaba.dubbo.rpc.cluster.Router in project dubbo by alibaba.

the class RegistryDirectory method route.

private List<Invoker<T>> route(List<Invoker<T>> invokers, String method) {
    Invocation invocation = new RpcInvocation(method, new Class<?>[0], new Object[0]);
    List<Router> routers = getRouters();
    if (routers != null) {
        for (Router router : routers) {
            if (router.getUrl() != null && !router.getUrl().getParameter(Constants.RUNTIME_KEY, true)) {
                invokers = router.route(invokers, getConsumerUrl(), invocation);
            }
        }
    }
    return invokers;
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Router(com.alibaba.dubbo.rpc.cluster.Router)

Example 8 with Router

use of com.alibaba.dubbo.rpc.cluster.Router in project dubbo by alibaba.

the class ConditionRouterTest method testRoute_ReturnFalse.

@Test
public void testRoute_ReturnFalse() {
    Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => false"));
    List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
    invokers.add(new MockInvoker<String>());
    invokers.add(new MockInvoker<String>());
    invokers.add(new MockInvoker<String>());
    List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
    Assert.assertEquals(0, fileredInvokers.size());
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invoker(com.alibaba.dubbo.rpc.Invoker) MockInvoker(com.alibaba.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(com.alibaba.dubbo.rpc.cluster.Router) Test(org.junit.Test)

Example 9 with Router

use of com.alibaba.dubbo.rpc.cluster.Router in project dubbo by alibaba.

the class ConditionRouterTest method testRoute_Force.

@Test
public void testRoute_Force() {
    Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = 1.2.3.4").addParameter(Constants.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> 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>> fileredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
    Assert.assertEquals(0, fileredInvokers.size());
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invoker(com.alibaba.dubbo.rpc.Invoker) MockInvoker(com.alibaba.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(com.alibaba.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(com.alibaba.dubbo.rpc.cluster.Router) Test(org.junit.Test)

Example 10 with Router

use of com.alibaba.dubbo.rpc.cluster.Router in project dubbo by alibaba.

the class ConditionRouterTest method testRoute_False_HostFilter.

@Test
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> 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>> fileredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
    Assert.assertEquals(2, fileredInvokers.size());
    Assert.assertEquals(invoker2, fileredInvokers.get(0));
    Assert.assertEquals(invoker3, fileredInvokers.get(1));
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invoker(com.alibaba.dubbo.rpc.Invoker) MockInvoker(com.alibaba.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(com.alibaba.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(com.alibaba.dubbo.rpc.cluster.Router) Test(org.junit.Test)

Aggregations

Router (com.alibaba.dubbo.rpc.cluster.Router)15 ArrayList (java.util.ArrayList)13 Invoker (com.alibaba.dubbo.rpc.Invoker)12 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)12 Test (org.junit.Test)12 MockInvoker (com.alibaba.dubbo.rpc.cluster.router.MockInvoker)11 URL (com.alibaba.dubbo.common.URL)2 ScriptRouterFactory (com.alibaba.dubbo.rpc.cluster.router.script.ScriptRouterFactory)2 RegistryDirectory (com.alibaba.dubbo.registry.integration.RegistryDirectory)1 Invocation (com.alibaba.dubbo.rpc.Invocation)1 RpcException (com.alibaba.dubbo.rpc.RpcException)1 Configurator (com.alibaba.dubbo.rpc.cluster.Configurator)1 ScriptRouter (com.alibaba.dubbo.rpc.cluster.router.script.ScriptRouter)1