Search in sources :

Example 16 with Router

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

the class ScriptRouterTest method testRoute_PickInvokers.

@Test
public void testRoute_PickInvokers() {
    String rule = "var result = new java.util.ArrayList(invokers.size());" + "for (i=0;i<invokers.size(); i++){ " + "if (invokers.get(i).isAvailable()) {" + "result.add(invokers.get(i)) ;" + "}" + "} ; " + "return result;";
    String script = "function route(invokers,invocation,context){" + rule + "} route(invokers,invocation,context)";
    Router router = new ScriptRouterFactory().getRouter(getRouteUrl(script));
    List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
    Invoker<String> invoker1 = new MockInvoker<String>(false);
    Invoker<String> invoker2 = new MockInvoker<String>(true);
    Invoker<String> invoker3 = new MockInvoker<String>(true);
    invokers.add(invoker1);
    invokers.add(invoker2);
    invokers.add(invoker3);
    List<Invoker<String>> fileredInvokers = router.route(invokers, invokers.get(0).getUrl(), 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)

Example 17 with Router

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

the class AbstractDirectory method list.

public List<Invoker<T>> list(Invocation invocation) throws RpcException {
    if (destroyed) {
        throw new RpcException("Directory already destroyed .url: " + getUrl());
    }
    List<Invoker<T>> invokers = doList(invocation);
    // local reference
    List<Router> localRouters = this.routers;
    if (localRouters != null && !localRouters.isEmpty()) {
        for (Router router : localRouters) {
            try {
                if (router.getUrl() == null || router.getUrl().getParameter(Constants.RUNTIME_KEY, false)) {
                    invokers = router.route(invokers, getConsumerUrl(), invocation);
                }
            } catch (Throwable t) {
                logger.error("Failed to execute router: " + getUrl() + ", cause: " + t.getMessage(), t);
            }
        }
    }
    return invokers;
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) RpcException(com.alibaba.dubbo.rpc.RpcException) Router(com.alibaba.dubbo.rpc.cluster.Router)

Example 18 with Router

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

the class RegistryDirectoryTest method testNotifyRouterUrls.

/**
 * 1. notify twice, the second time notified router rules should completely replace the former one. 2. notify with
 * no router url, do nothing to current routers 3. notify with only one router url, with router=clean, clear all
 * current routers
 */
@Test
public void testNotifyRouterUrls() {
    if (isScriptUnsupported)
        return;
    RegistryDirectory registryDirectory = getRegistryDirectory();
    URL routerurl = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9096/");
    URL routerurl2 = URL.valueOf(Constants.ROUTE_PROTOCOL + "://127.0.0.1:9097/");
    List<URL> serviceUrls = new ArrayList<URL>();
    // without ROUTER_KEY, the first router should not be created.
    serviceUrls.add(routerurl.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY, "notsupported").addParameter(Constants.RULE_KEY, "function test1(){}"));
    serviceUrls.add(routerurl2.addParameter(Constants.CATEGORY_KEY, Constants.ROUTERS_CATEGORY).addParameter(Constants.TYPE_KEY, "javascript").addParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME).addParameter(Constants.RULE_KEY, "function test1(){}"));
    registryDirectory.notify(serviceUrls);
    List<Router> routers = registryDirectory.getRouters();
    // default invocation selector
    Assert.assertEquals(1 + 1, routers.size());
    Assert.assertTrue(ScriptRouter.class == routers.get(1).getClass() || ScriptRouter.class == routers.get(0).getClass());
    registryDirectory.notify(new ArrayList<URL>());
    routers = registryDirectory.getRouters();
    Assert.assertEquals(1 + 1, routers.size());
    Assert.assertTrue(ScriptRouter.class == routers.get(1).getClass() || ScriptRouter.class == routers.get(0).getClass());
    serviceUrls.clear();
    serviceUrls.add(routerurl.addParameter(Constants.ROUTER_KEY, Constants.ROUTER_TYPE_CLEAR));
    registryDirectory.notify(serviceUrls);
    routers = registryDirectory.getRouters();
    Assert.assertEquals(0 + 1, routers.size());
}
Also used : RegistryDirectory(com.alibaba.dubbo.registry.integration.RegistryDirectory) ArrayList(java.util.ArrayList) Router(com.alibaba.dubbo.rpc.cluster.Router) ScriptRouter(com.alibaba.dubbo.rpc.cluster.router.script.ScriptRouter) URL(com.alibaba.dubbo.common.URL) Test(org.junit.Test)

Aggregations

Router (com.alibaba.dubbo.rpc.cluster.Router)18 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)15 ArrayList (java.util.ArrayList)15 Test (org.junit.Test)15 Invoker (com.alibaba.dubbo.rpc.Invoker)14 MockInvoker (com.alibaba.dubbo.rpc.cluster.router.MockInvoker)13 Invocation (com.alibaba.dubbo.rpc.Invocation)3 URL (com.alibaba.dubbo.common.URL)2 RegistryDirectory (com.alibaba.dubbo.registry.integration.RegistryDirectory)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