use of org.apache.dubbo.rpc.cluster.Router in project dubbo by alibaba.
the class ConditionRouterTest method testRoute_Empty_HostFilter.
@Test
public void testRoute_Empty_HostFilter() {
Router router = new ConditionRouterFactory().getRouter(getRouteUrl(" => " + " host = " + LOCAL_HOST));
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://" + LOCAL_HOST + ":20880/com.foo.BarService"));
Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + LOCAL_HOST + ":20880/com.foo.BarService"));
invokers.add(invoker1);
invokers.add(invoker2);
invokers.add(invoker3);
List<Invoker<String>> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + LOCAL_HOST + "/com.foo.BarService"), new RpcInvocation());
Assertions.assertEquals(2, filteredInvokers.size());
Assertions.assertEquals(invoker2, filteredInvokers.get(0));
Assertions.assertEquals(invoker3, filteredInvokers.get(1));
}
use of org.apache.dubbo.rpc.cluster.Router in project dubbo by alibaba.
the class ScriptRouterTest method testRoute_throwException.
@Test
public void testRoute_throwException() {
List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
MockInvoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.1:20880/com.dubbo.HelloService"));
MockInvoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.2:20880/com.dubbo.HelloService"));
MockInvoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.3:20880/com.dubbo.HelloService"));
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());
}
use of org.apache.dubbo.rpc.cluster.Router in project dubbo by alibaba.
the class ScriptRouterTest method testRouteHostFilter.
@Test
public void testRouteHostFilter() {
List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
MockInvoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.1:20880/com.dubbo.HelloService"));
MockInvoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.2:20880/com.dubbo.HelloService"));
MockInvoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.3:20880/com.dubbo.HelloService"));
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));
}
use of org.apache.dubbo.rpc.cluster.Router in project dubbo by alibaba.
the class ScriptRouterTest method testRoutePickInvokers.
@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));
}
Aggregations