Search in sources :

Example 36 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class CallbackServiceCodec method referOrDestroyCallbackService.

/**
 * refer or destroy callback service on server side
 *
 * @param url
 */
@SuppressWarnings("unchecked")
private static Object referOrDestroyCallbackService(Channel channel, URL url, Class<?> clazz, Invocation inv, int instid, boolean isRefer) {
    Object proxy;
    String invokerCacheKey = getServerSideCallbackInvokerCacheKey(channel, clazz.getName(), instid);
    String proxyCacheKey = getServerSideCallbackServiceCacheKey(channel, clazz.getName(), instid);
    proxy = channel.getAttribute(proxyCacheKey);
    String countkey = getServerSideCountKey(channel, clazz.getName());
    if (isRefer) {
        if (proxy == null) {
            URL referurl = URL.valueOf("callback://" + url.getAddress() + "/" + clazz.getName() + "?" + INTERFACE_KEY + "=" + clazz.getName());
            referurl = referurl.addParametersIfAbsent(url.getParameters()).removeParameter(METHODS_KEY);
            if (!isInstancesOverLimit(channel, referurl, clazz.getName(), instid, true)) {
                ApplicationModel.getServiceRepository().registerService(clazz);
                @SuppressWarnings("rawtypes") Invoker<?> invoker = new ChannelWrappedInvoker(clazz, channel, referurl, String.valueOf(instid));
                proxy = PROXY_FACTORY.getProxy(new AsyncToSyncInvoker<>(invoker));
                channel.setAttribute(proxyCacheKey, proxy);
                channel.setAttribute(invokerCacheKey, invoker);
                increaseInstanceCount(channel, countkey);
                // convert error fail fast .
                // ignore concurrent problem.
                Set<Invoker<?>> callbackInvokers = (Set<Invoker<?>>) channel.getAttribute(CHANNEL_CALLBACK_KEY);
                if (callbackInvokers == null) {
                    callbackInvokers = new ConcurrentHashSet<>(1);
                    channel.setAttribute(CHANNEL_CALLBACK_KEY, callbackInvokers);
                }
                callbackInvokers.add(invoker);
                logger.info("method " + inv.getMethodName() + " include a callback service :" + invoker.getUrl() + ", a proxy :" + invoker + " has been created.");
            }
        }
    } else {
        if (proxy != null) {
            Invoker<?> invoker = (Invoker<?>) channel.getAttribute(invokerCacheKey);
            try {
                Set<Invoker<?>> callbackInvokers = (Set<Invoker<?>>) channel.getAttribute(CHANNEL_CALLBACK_KEY);
                if (callbackInvokers != null) {
                    callbackInvokers.remove(invoker);
                }
                invoker.destroy();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            // cancel refer, directly remove from the map
            channel.removeAttribute(proxyCacheKey);
            channel.removeAttribute(invokerCacheKey);
            decreaseInstanceCount(channel, countkey);
        }
    }
    return proxy;
}
Also used : Set(java.util.Set) ConcurrentHashSet(org.apache.dubbo.common.utils.ConcurrentHashSet) AsyncToSyncInvoker(org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker) Invoker(org.apache.dubbo.rpc.Invoker) AsyncToSyncInvoker(org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker) URL(org.apache.dubbo.common.URL) RemotingException(org.apache.dubbo.remoting.RemotingException) IOException(java.io.IOException)

Example 37 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class TimeoutFilterTest method testInvokeWithoutTimeout.

@Test
public void testInvokeWithoutTimeout() throws Exception {
    int timeout = 3000;
    Invoker invoker = Mockito.mock(Invoker.class);
    when(invoker.invoke(any(Invocation.class))).thenReturn(new AppResponse("result"));
    when(invoker.getUrl()).thenReturn(URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&timeout=" + timeout));
    Invocation invocation = Mockito.mock(Invocation.class);
    when(invocation.getMethodName()).thenReturn("testInvokeWithoutTimeout");
    Result result = timeoutFilter.invoke(invoker, invocation);
    Assertions.assertEquals("result", result.getValue());
}
Also used : BlockMyInvoker(org.apache.dubbo.rpc.support.BlockMyInvoker) Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) AppResponse(org.apache.dubbo.rpc.AppResponse) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 38 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class TokenFilterTest method testInvokeWithoutToken.

@Test
public void testInvokeWithoutToken() throws Exception {
    Assertions.assertThrows(RpcException.class, () -> {
        String token = "token";
        Invoker invoker = Mockito.mock(Invoker.class);
        URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&token=" + token);
        when(invoker.getUrl()).thenReturn(url);
        when(invoker.invoke(any(Invocation.class))).thenReturn(new AppResponse("result"));
        Invocation invocation = Mockito.mock(Invocation.class);
        tokenFilter.invoke(invoker, invocation);
    });
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) AppResponse(org.apache.dubbo.rpc.AppResponse) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 39 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class TokenFilterTest method testInvokeWithWrongToken.

@Test
public void testInvokeWithWrongToken() throws Exception {
    Assertions.assertThrows(RpcException.class, () -> {
        String token = "token";
        Invoker invoker = Mockito.mock(Invoker.class);
        URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&token=" + token);
        when(invoker.getUrl()).thenReturn(url);
        when(invoker.invoke(any(Invocation.class))).thenReturn(new AppResponse("result"));
        Map<String, Object> attachments = new HashMap<>();
        attachments.put(TOKEN_KEY, "wrongToken");
        Invocation invocation = Mockito.mock(Invocation.class);
        when(invocation.getObjectAttachments()).thenReturn(attachments);
        tokenFilter.invoke(invoker, invocation);
    });
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) HashMap(java.util.HashMap) AppResponse(org.apache.dubbo.rpc.AppResponse) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 40 with Invoker

use of org.apache.dubbo.rpc.Invoker 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)

Aggregations

Invoker (org.apache.dubbo.rpc.Invoker)128 Test (org.junit.jupiter.api.Test)104 URL (org.apache.dubbo.common.URL)74 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)66 ArrayList (java.util.ArrayList)48 Invocation (org.apache.dubbo.rpc.Invocation)42 Result (org.apache.dubbo.rpc.Result)26 AppResponse (org.apache.dubbo.rpc.AppResponse)21 MockClusterInvoker (org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker)21 RegistryDirectory (org.apache.dubbo.registry.integration.RegistryDirectory)20 Router (org.apache.dubbo.rpc.cluster.Router)18 MockInvoker (org.apache.dubbo.rpc.cluster.router.MockInvoker)18 HashMap (java.util.HashMap)13 LoadBalance (org.apache.dubbo.rpc.cluster.LoadBalance)13 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)12 RpcException (org.apache.dubbo.rpc.RpcException)12 LeastActiveLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)8 RandomLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)8 RoundRobinLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8