com.alibaba.dubbo.rpc.cluster.Router Java Examples

The following examples show how to use com.alibaba.dubbo.rpc.cluster.Router. 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: AbstractDirectory.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
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);
    List<Router> localRouters = this.routers; // local reference
    if (localRouters != null && localRouters.size() > 0) {
        for (Router router: localRouters){
            try {
                if (router.getUrl() == null || router.getUrl().getParameter(Constants.RUNTIME_KEY, true)) {
                    invokers = router.route(invokers, getConsumerUrl(), invocation);
                }
            } catch (Throwable t) {
                logger.error("Failed to execute router: " + getUrl() + ", cause: " + t.getMessage(), t);
            }
        }
    }
    return invokers;
}
 
Example #2
Source File: AbstractDirectory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
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);
    List<Router> localRouters = this.routers; // local reference
    if (localRouters != null && localRouters.size() > 0) {
        for (Router router: localRouters){
            try {
                if (router.getUrl() == null || router.getUrl().getParameter(Constants.RUNTIME_KEY, true)) {
                    invokers = router.route(invokers, getConsumerUrl(), invocation);
                }
            } catch (Throwable t) {
                logger.error("Failed to execute router: " + getUrl() + ", cause: " + t.getMessage(), t);
            }
        }
    }
    return invokers;
}
 
Example #3
Source File: FileRouterFactory.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public Router getRouter(URL url) {
    try {
        // File URL 转换成 其它Route URL,然后Load
        // file:///d:/path/to/route.js?router=script ==> script:///d:/path/to/route.js?type=js&rule=<file-content>
        String protocol = url.getParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME); // 将原类型转为协议
        String type = null; // 使用文件后缀做为类型
        String path = url.getPath();
        if (path != null) {
            int i = path.lastIndexOf('.');
            if (i > 0) {
                type = path.substring(i + 1);
            }
        }
        String rule = IOUtils.read(new FileReader(new File(url.getAbsolutePath())));
        URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type).addParameterAndEncoded(Constants.RULE_KEY, rule);
        
        return routerFactory.getRouter(script);
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #4
Source File: RegistryDirectory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param urls
 * @return null : no routers ,do nothing
 *         else :routers list
 */
private List<Router> toRouters(List<URL> urls) {
    List<Router> routers = new ArrayList<Router>();
    if(urls == null || urls.size() < 1){
        return routers ;
    }
    if (urls != null && urls.size() > 0) {
        for (URL url : urls) {
            if (Constants.EMPTY_PROTOCOL.equals(url.getProtocol())) {
                continue;
            }
            String routerType = url.getParameter(Constants.ROUTER_KEY);
            if (routerType != null && routerType.length() > 0){
                url = url.setProtocol(routerType);
            }
            try{
                Router router = routerFactory.getRouter(url);
                if (!routers.contains(router))
                    routers.add(router);
            } catch (Throwable t) {
                logger.error("convert router url to router error, url: "+ url, t);
            }
        }
    }
    return routers;
}
 
Example #5
Source File: ScriptRouterTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #6
Source File: RegistryDirectory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param urls
 * @return null : no routers ,do nothing
 *         else :routers list
 */
private List<Router> toRouters(List<URL> urls) {
    List<Router> routers = new ArrayList<Router>();
    if(urls == null || urls.size() < 1){
        return routers ;
    }
    if (urls != null && urls.size() > 0) {
        for (URL url : urls) {
            if (Constants.EMPTY_PROTOCOL.equals(url.getProtocol())) {
                continue;
            }
            String routerType = url.getParameter(Constants.ROUTER_KEY);
            if (routerType != null && routerType.length() > 0){
                url = url.setProtocol(routerType);
            }
            try{
                Router router = routerFactory.getRouter(url);
                if (!routers.contains(router))
                    routers.add(router);
            } catch (Throwable t) {
                logger.error("convert router url to router error, url: "+ url, t);
            }
        }
    }
    return routers;
}
 
Example #7
Source File: ScriptRouterTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #8
Source File: ScriptRouterTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #9
Source File: FileRouterFactory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Router getRouter(URL url) {
    try {
        // File URL 转换成 其它Route URL,然后Load
        // file:///d:/path/to/route.js?router=script ==> script:///d:/path/to/route.js?type=js&rule=<file-content>
        String protocol = url.getParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME); // 将原类型转为协议
        String type = null; // 使用文件后缀做为类型
        String path = url.getPath();
        if (path != null) {
            int i = path.lastIndexOf('.');
            if (i > 0) {
                type = path.substring(i + 1);
            }
        }
        String rule = IOUtils.read(new FileReader(new File(url.getAbsolutePath())));
        URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type).addParameterAndEncoded(Constants.RULE_KEY, rule);
        
        return routerFactory.getRouter(script);
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #10
Source File: AbstractDirectory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
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);
    List<Router> localRouters = this.routers; // local reference
    if (localRouters != null && localRouters.size() > 0) {
        for (Router router: localRouters){
            try {
                if (router.getUrl() == null || router.getUrl().getParameter(Constants.RUNTIME_KEY, true)) {
                    invokers = router.route(invokers, getConsumerUrl(), invocation);
                }
            } catch (Throwable t) {
                logger.error("Failed to execute router: " + getUrl() + ", cause: " + t.getMessage(), t);
            }
        }
    }
    return invokers;
}
 
Example #11
Source File: FileRouterFactory.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
public Router getRouter(URL url) {
    try {
        // Transform File URL into Script Route URL, and Load
        // file:///d:/path/to/route.js?router=script ==> script:///d:/path/to/route.js?type=js&rule=<file-content>
        String protocol = url.getParameter(Constants.ROUTER_KEY, ScriptRouterFactory.NAME); // Replace original protocol (maybe 'file') with 'script'
        String type = null; // Use file suffix to config script type, e.g., js, groovy ...
        String path = url.getPath();
        if (path != null) {
            int i = path.lastIndexOf('.');
            if (i > 0) {
                type = path.substring(i + 1);
            }
        }
        String rule = IOUtils.read(new FileReader(new File(url.getAbsolutePath())));

        boolean runtime = url.getParameter(Constants.RUNTIME_KEY, false);
        URL script = url.setProtocol(protocol).addParameter(Constants.TYPE_KEY, type).addParameter(Constants.RUNTIME_KEY, runtime).addParameterAndEncoded(Constants.RULE_KEY, rule);

        return routerFactory.getRouter(script);
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #12
Source File: AbstractDirectory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
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);
    List<Router> localRouters = this.routers; // local reference
    if (localRouters != null && localRouters.size() > 0) {
        for (Router router: localRouters){
            try {
                if (router.getUrl() == null || router.getUrl().getParameter(Constants.RUNTIME_KEY, true)) {
                    invokers = router.route(invokers, getConsumerUrl(), invocation);
                }
            } catch (Throwable t) {
                logger.error("Failed to execute router: " + getUrl() + ", cause: " + t.getMessage(), t);
            }
        }
    }
    return invokers;
}
 
Example #13
Source File: ScriptRouterTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #14
Source File: ScriptRouterTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoute_ReturnAll(){
    Router router = new ScriptRouterFactory().getRouter(getRouteUrl("function route(op1,op2){return op1} route(invokers)"));
    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, invokers.get(0).getUrl(), new RpcInvocation());
    Assert.assertEquals(invokers, fileredInvokers);
}
 
Example #15
Source File: AbstractDirectory.java    From dubbox with Apache License 2.0 5 votes vote down vote up
protected void setRouters(List<Router> routers){
    // copy list
    routers = routers == null ? new  ArrayList<Router>() : new ArrayList<Router>(routers);
    // append url router
	String routerkey = url.getParameter(Constants.ROUTER_KEY);
    if (routerkey != null && routerkey.length() > 0) {
        RouterFactory routerFactory = ExtensionLoader.getExtensionLoader(RouterFactory.class).getExtension(routerkey);
        routers.add(routerFactory.getRouter(url));
    }
    // append mock invoker selector
    routers.add(new MockInvokersSelector());
    Collections.sort(routers);
	this.routers = routers;
}
 
Example #16
Source File: ConditionRouterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #17
Source File: ConditionRouterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoute_HostFilter(){
    Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " 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));
}
 
Example #18
Source File: ConditionRouterTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #19
Source File: ConditionRouter.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public int compareTo(Router o) {
    if (o == null || o.getClass() != ConditionRouter.class) {
        return 1;
    }
    ConditionRouter c = (ConditionRouter) o;
    return this.priority == c.priority ? url.toFullString().compareTo(c.url.toFullString()) : (this.priority > c.priority ? 1 : -1);
}
 
Example #20
Source File: ConditionRouterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #21
Source File: AbstractDirectory.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public AbstractDirectory(URL url, URL consumerUrl, List<Router> routers) {
    if (url == null)
        throw new IllegalArgumentException("url == null");
    this.url = url;
    this.consumerUrl = consumerUrl;
    setRouters(routers);
}
 
Example #22
Source File: ConditionRouterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoute_NoForce(){
    Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = 1.2.3.4"));
    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(invokers, fileredInvokers);
}
 
Example #23
Source File: ConditionRouterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #24
Source File: ConditionRouterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoute_Empty_HostFilter(){
    Router router = new ConditionRouterFactory().getRouter(getRouteUrl(" => " + " 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));
}
 
Example #25
Source File: ConditionRouter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public int compareTo(Router o) {
    if (o == null || o.getClass() != ConditionRouter.class) {
        return 1;
    }
    ConditionRouter c = (ConditionRouter) o;
    return this.priority == c.priority ? url.toFullString().compareTo(c.url.toFullString()) : (this.priority > c.priority ? 1 : -1);
}
 
Example #26
Source File: ConditionRouterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoute_ReturnAll(){
    Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = " + NetUtils.getLocalHost()));
    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(invokers, fileredInvokers);
}
 
Example #27
Source File: ConditionRouter.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public int compareTo(Router o) {
    if (o == null || o.getClass() != ConditionRouter.class) {
        return 1;
    }
    ConditionRouter c = (ConditionRouter) o;
    return this.priority == c.priority ? url.toFullString().compareTo(c.url.toFullString()) : (this.priority > c.priority ? 1 : -1);
}
 
Example #28
Source File: ConditionRouterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #29
Source File: ScriptRouterTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoute_ReturnAll(){
    Router router = new ScriptRouterFactory().getRouter(getRouteUrl("function route(op1,op2){return op1} route(invokers)"));
    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, invokers.get(0).getUrl(), new RpcInvocation());
    Assert.assertEquals(invokers, fileredInvokers);
}
 
Example #30
Source File: AbstractDirectory.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
public AbstractDirectory(URL url, URL consumerUrl, List<Router> routers) {
    if (url == null)
        throw new IllegalArgumentException("url == null");
    this.url = url;
    this.consumerUrl = consumerUrl;
    setRouters(routers);
}