Search in sources :

Example 1 with MockInvoker

use of org.apache.dubbo.rpc.cluster.router.MockInvoker in project dubbo by alibaba.

the class StaticDirectoryTest method testStaticDirectory.

@Test
public void testStaticDirectory() {
    Router router = new ConditionRouterFactory().getRouter(getRouteUrl(" => " + " host = " + NetUtils.getLocalHost()));
    List<Router> routers = new ArrayList<Router>();
    routers.add(router);
    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>> filteredInvokers = router.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation());
    StaticDirectory<String> staticDirectory = new StaticDirectory<>(filteredInvokers);
    boolean isAvailable = staticDirectory.isAvailable();
    Assertions.assertTrue(!isAvailable);
    List<Invoker<String>> newInvokers = staticDirectory.list(new MockDirInvocation());
    Assertions.assertTrue(newInvokers.size() > 0);
    staticDirectory.destroy();
    Assertions.assertEquals(0, newInvokers.size());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ArrayList(java.util.ArrayList) Router(org.apache.dubbo.rpc.cluster.Router) Invoker(org.apache.dubbo.rpc.Invoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) ConditionRouterFactory(org.apache.dubbo.rpc.cluster.router.condition.ConditionRouterFactory) Test(org.junit.jupiter.api.Test)

Example 2 with MockInvoker

use of org.apache.dubbo.rpc.cluster.router.MockInvoker in project dubbo by alibaba.

the class AddressRouterTest method testAddressRouteSelector.

@Test
public void testAddressRouteSelector() {
    Router router = new AddressRouterFactory().getRouter(URL.valueOf("url"));
    List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
    invokers.add(new MockInvoker<>(new URL("dubbo", "129.34.56.7", 8809), true));
    invokers.add(new MockInvoker<>(new URL("dubbo", "129.34.56.8", 8809), true));
    invokers.add(new MockInvoker<>(new URL("dubbo", "129.34.56.9", 8809), true));
    Invocation invocation = new RpcInvocation();
    Address address = new Address("129.34.56.9", 8809);
    invocation.setObjectAttachment("address", address);
    List<Invoker<String>> list = router.route(invokers, URL.valueOf("url"), invocation);
    Assertions.assertEquals(address.getIp(), list.get(0).getUrl().getHost());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ArrayList(java.util.ArrayList) Router(org.apache.dubbo.rpc.cluster.Router) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 3 with MockInvoker

use of org.apache.dubbo.rpc.cluster.router.MockInvoker in project dubbo by alibaba.

the class ConditionRouterTest method testRoute_Placeholder.

@Test
public void testRoute_Placeholder() {
    Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + LOCAL_HOST + " => " + " host = $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));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(org.apache.dubbo.rpc.cluster.Router) Test(org.junit.jupiter.api.Test)

Example 4 with MockInvoker

use of org.apache.dubbo.rpc.cluster.router.MockInvoker in project dubbo by alibaba.

the class ConditionRouterTest method testRoute_matchFilter.

@Test
public void testRoute_matchFilter() {
    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?serialization=fastjson"));
    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);
    System.err.println("The localhost address: " + invoker2.getUrl().getAddress());
    System.err.println(invoker3.getUrl().getAddress());
    Router router1 = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + LOCAL_HOST + " => " + " host = 10.20.3.3").addParameter(FORCE_KEY, String.valueOf(true)));
    Router router2 = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + LOCAL_HOST + " => " + " host = 10.20.3.* & host != 10.20.3.3").addParameter(FORCE_KEY, String.valueOf(true)));
    Router router3 = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + LOCAL_HOST + " => " + " host = 10.20.3.3  & host != 10.20.3.3").addParameter(FORCE_KEY, String.valueOf(true)));
    Router router4 = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + LOCAL_HOST + " => " + " host = 10.20.3.2,10.20.3.3,10.20.3.4").addParameter(FORCE_KEY, String.valueOf(true)));
    Router router5 = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + LOCAL_HOST + " => " + " host != 10.20.3.3").addParameter(FORCE_KEY, String.valueOf(true)));
    Router router6 = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + LOCAL_HOST + " => " + " serialization = fastjson").addParameter(FORCE_KEY, String.valueOf(true)));
    List<Invoker<String>> filteredInvokers1 = router1.route(invokers, URL.valueOf("consumer://" + LOCAL_HOST + "/com.foo.BarService"), new RpcInvocation());
    List<Invoker<String>> filteredInvokers2 = router2.route(invokers, URL.valueOf("consumer://" + LOCAL_HOST + "/com.foo.BarService"), new RpcInvocation());
    List<Invoker<String>> filteredInvokers3 = router3.route(invokers, URL.valueOf("consumer://" + LOCAL_HOST + "/com.foo.BarService"), new RpcInvocation());
    List<Invoker<String>> filteredInvokers4 = router4.route(invokers, URL.valueOf("consumer://" + LOCAL_HOST + "/com.foo.BarService"), new RpcInvocation());
    List<Invoker<String>> filteredInvokers5 = router5.route(invokers, URL.valueOf("consumer://" + LOCAL_HOST + "/com.foo.BarService"), new RpcInvocation());
    List<Invoker<String>> filteredInvokers6 = router6.route(invokers, URL.valueOf("consumer://" + LOCAL_HOST + "/com.foo.BarService"), new RpcInvocation());
    Assertions.assertEquals(1, filteredInvokers1.size());
    Assertions.assertEquals(0, filteredInvokers2.size());
    Assertions.assertEquals(0, filteredInvokers3.size());
    Assertions.assertEquals(1, filteredInvokers4.size());
    Assertions.assertEquals(2, filteredInvokers5.size());
    Assertions.assertEquals(1, filteredInvokers6.size());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(org.apache.dubbo.rpc.cluster.Router) Test(org.junit.jupiter.api.Test)

Example 5 with MockInvoker

use of org.apache.dubbo.rpc.cluster.router.MockInvoker in project dubbo by alibaba.

the class ConditionRouterTest method testRoute_HostFilter.

@Test
public void testRoute_HostFilter() {
    Router router = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + LOCAL_HOST + " => " + " 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));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(org.apache.dubbo.rpc.cluster.Router) Test(org.junit.jupiter.api.Test)

Aggregations

ArrayList (java.util.ArrayList)18 Invoker (org.apache.dubbo.rpc.Invoker)18 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)18 Router (org.apache.dubbo.rpc.cluster.Router)18 MockInvoker (org.apache.dubbo.rpc.cluster.router.MockInvoker)18 Test (org.junit.jupiter.api.Test)18 Invocation (org.apache.dubbo.rpc.Invocation)2 URL (org.apache.dubbo.common.URL)1 ConditionRouterFactory (org.apache.dubbo.rpc.cluster.router.condition.ConditionRouterFactory)1