ConditionRouter extends AbstractRouter { @Override public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException { if (invokers == null || invokers.isEmpty()) { return invokers; } try { if (!matchWhen(url, invocation)) { return invokers; } List<Invoker<T>> result = new ArrayList<Invoker<T>>(); if (thenCondition == null) { logger.warn("The current consumer in the service blacklist. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey()); return result; } for (Invoker<T> invoker : invokers) { if (matchThen(invoker.getUrl(), url)) { result.add(invoker); } } if (!result.isEmpty()) { return result; } else if (force) { logger.warn("The route result is empty and force execute. consumer: " + NetUtils.getLocalHost() + ", service: " + url.getServiceKey() + ", router: " + url.getParameterAndDecoded(Constants.RULE_KEY)); return result; } } catch (Throwable t) { logger.error("Failed to execute condition router rule: " + getUrl() + ", invokers: " + invokers + ", cause: " + t.getMessage(), t); } return invokers; } ConditionRouter(URL url); @Override List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation); @Override URL getUrl(); @Override int compareTo(Router o);  }
@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: Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo: Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo: invokers.add(invoker1); invokers.add(invoker2); invokers.add(invoker3); List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer: Assert.assertEquals(0, fileredInvokers.size()); }
@Test public void testRoute_matchFilter() { List<Invoker<String>> invokers = new ArrayList<Invoker<String>>(); Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf( "dubbo: Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo: + ":20880/com.foo.BarService")); Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo: + ":20880/com.foo.BarService")); invokers.add(invoker1); invokers.add(invoker2); invokers.add(invoker3); Router router1 = new ConditionRouterFactory().getRouter(getRouteUrl( "host = " + NetUtils.getLocalHost() + " => " + " host = 10.20.3.3").addParameter(Constants.FORCE_KEY, String.valueOf(true))); Router router2 = new ConditionRouterFactory().getRouter(getRouteUrl( "host = " + NetUtils.getLocalHost() + " => " + " host = 10.20.3.* & host != 10.20.3.3").addParameter( Constants.FORCE_KEY, String.valueOf(true))); Router router3 = new ConditionRouterFactory().getRouter(getRouteUrl( "host = " + NetUtils.getLocalHost() + " => " + " host = 10.20.3.3 & host != 10.20.3.3").addParameter( Constants.FORCE_KEY, String.valueOf(true))); Router router4 = new ConditionRouterFactory().getRouter(getRouteUrl( "host = " + NetUtils.getLocalHost() + " => " + " host = 10.20.3.2,10.20.3.3,10.20.3.4").addParameter( Constants.FORCE_KEY, String.valueOf(true))); Router router5 = new ConditionRouterFactory().getRouter(getRouteUrl( "host = " + NetUtils.getLocalHost() + " => " + " host != 10.20.3.3").addParameter(Constants.FORCE_KEY, String.valueOf(true))); Router router6 = new ConditionRouterFactory().getRouter(getRouteUrl( "host = " + NetUtils.getLocalHost() + " => " + " serialization = fastjson").addParameter( Constants.FORCE_KEY, String.valueOf(true))); List<Invoker<String>> fileredInvokers1 = router1.route(invokers, URL.valueOf("consumer: List<Invoker<String>> fileredInvokers2 = router2.route(invokers, URL.valueOf("consumer: List<Invoker<String>> fileredInvokers3 = router3.route(invokers, URL.valueOf("consumer: List<Invoker<String>> fileredInvokers4 = router4.route(invokers, URL.valueOf("consumer: List<Invoker<String>> fileredInvokers5 = router5.route(invokers, URL.valueOf("consumer: List<Invoker<String>> fileredInvokers6 = router6.route(invokers, URL.valueOf("consumer: Assert.assertEquals(1, fileredInvokers1.size()); Assert.assertEquals(0, fileredInvokers2.size()); Assert.assertEquals(0, fileredInvokers3.size()); Assert.assertEquals(1, fileredInvokers4.size()); Assert.assertEquals(2, fileredInvokers5.size()); Assert.assertEquals(1, fileredInvokers6.size()); }
@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: Assert.assertEquals(0, fileredInvokers.size()); }
@Test public void testRoute_ReturnEmpty() { Router router = new ConditionRouterFactory().getRouter(getRouteUrl("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: Assert.assertEquals(0, fileredInvokers.size()); }
@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: Assert.assertEquals(invokers, fileredInvokers); }
@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: Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo: Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo: invokers.add(invoker1); invokers.add(invoker2); invokers.add(invoker3); List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer: Assert.assertEquals(2, fileredInvokers.size()); Assert.assertEquals(invoker2, fileredInvokers.get(0)); Assert.assertEquals(invoker3, fileredInvokers.get(1)); }
@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: Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo: Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo: invokers.add(invoker1); invokers.add(invoker2); invokers.add(invoker3); List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer: Assert.assertEquals(2, fileredInvokers.size()); Assert.assertEquals(invoker2, fileredInvokers.get(0)); Assert.assertEquals(invoker3, fileredInvokers.get(1)); }
@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: Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo: Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo: invokers.add(invoker1); invokers.add(invoker2); invokers.add(invoker3); List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer: Assert.assertEquals(2, fileredInvokers.size()); Assert.assertEquals(invoker2, fileredInvokers.get(0)); Assert.assertEquals(invoker3, fileredInvokers.get(1)); }
@Test public void testRoute_Placeholder() { Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " => " + " host = $host")); List<Invoker<String>> invokers = new ArrayList<Invoker<String>>(); Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo: Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo: Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo: invokers.add(invoker1); invokers.add(invoker2); invokers.add(invoker3); List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer: Assert.assertEquals(2, fileredInvokers.size()); Assert.assertEquals(invoker2, fileredInvokers.get(0)); Assert.assertEquals(invoker3, fileredInvokers.get(1)); }
@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: Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo: Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo: invokers.add(invoker1); invokers.add(invoker2); invokers.add(invoker3); List<Invoker<String>> fileredInvokers = router.route(invokers, URL.valueOf("consumer: Assert.assertEquals(invokers, fileredInvokers); }