ScriptRouter extends AbstractRouter { @Override public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException { try { Bindings bindings = createBindings(invokers, invocation); if (function == null) { return invokers; } return getRoutedInvokers(function.eval(bindings)); } catch (ScriptException e) { logger.error("route error, rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e); return invokers; } } ScriptRouter(URL url); @Override List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation); @Override boolean isRuntime(); @Override boolean isForce(); static final String NAME; }
@Test public void testRouteReturnAll() { 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>> filteredInvokers = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation()); Assertions.assertEquals(invokers, filteredInvokers); }
@Test public void testRoutePickInvokers() { 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>> filteredInvokers = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation()); Assertions.assertEquals(2, filteredInvokers.size()); Assertions.assertEquals(invoker2, filteredInvokers.get(0)); Assertions.assertEquals(invoker3, filteredInvokers.get(1)); }
@Test public void testRouteHostFilter() { List<Invoker<String>> invokers = new ArrayList<Invoker<String>>(); MockInvoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo: MockInvoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo: MockInvoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo: invokers.add(invoker1); invokers.add(invoker2); invokers.add(invoker3); String script = "function route(invokers, invocation, context){ " + " var result = new java.util.ArrayList(invokers.size()); " + " var targetHost = new java.util.ArrayList(); " + " targetHost.add(\"10.134.108.2\"); " + " for (var i = 0; i < invokers.length; i++) { " + " if(targetHost.contains(invokers[i].getUrl().getHost())){ " + " result.add(invokers[i]); " + " } " + " } " + " return result; " + "} " + "route(invokers, invocation, context) "; Router router = new ScriptRouterFactory().getRouter(getRouteUrl(script)); List<Invoker<String>> routeResult = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation()); Assertions.assertEquals(1, routeResult.size()); Assertions.assertEquals(invoker2, routeResult.get(0)); }
@Test public void testRoute_throwException() { List<Invoker<String>> invokers = new ArrayList<Invoker<String>>(); MockInvoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo: MockInvoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo: MockInvoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo: invokers.add(invoker1); invokers.add(invoker2); invokers.add(invoker3); String script = "/"; Router router = new ScriptRouterFactory().getRouter(getRouteUrl(script)); List<Invoker<String>> routeResult = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation()); Assertions.assertEquals(3, routeResult.size()); }