The agile way of web development, html offline caching

Dubbo is a framework for remote invocation. For a service provider, it exposes an interface for external consumers to invoke,
Do you need any special processing for whether the provider can call this interface?

This article will share Dubbo's implementation mechanism of local calls and how to turn on and off local calls.

The injvm supports local calls

No special configuration is required to use Dubbo local call, and the service can be exposed according to the normal Dubbo service.
While exposing remote services, any service will also expose local services with the injvm protocol.
injvm is a pseudo protocol. It does not open ports like other protocols. It is only used for local calls.

Learning the source code of InjvmProtocol

The code related to injvm is implemented in the Dubbo RPC injvm module, mainly including InjvmExporter, InjvmInvoker and InjvmProtocol.

InjvmProtocol inherits AbstractProtocol,

public class InjvmProtocol extends AbstractProtocol implements Protocol {

    public static final String NAME = Constants.LOCAL_PROTOCOL;

    public static final int DEFAULT_PORT = 0;
    private static InjvmProtocol INSTANCE;

    public InjvmProtocol() {
        INSTANCE = this;
    }

    public static InjvmProtocol getInjvmProtocol() {
        if (INSTANCE == null) {
            ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(InjvmProtocol.NAME); // load
        }
        return INSTANCE;
    }

    @Override
    public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
        return new InjvmExporter<T>(invoker, invoker.getUrl().getServiceKey(), exporterMap);
    }

    @Override
    public <T> Invoker<T> refer(Class<T> serviceType, URL url) throws RpcException {
        return new InjvmInvoker<T>(serviceType, url, url.getServiceKey(), exporterMap);
    }
}

In addition to the export and refer methods, the InjvmProtocol provides the isInjvmRefer() method,

isInjvmRefer will read the configuration file to determine whether to enable local calls.

    public boolean isInjvmRefer(URL url) {
        String scope = url.getParameter(Constants.SCOPE_KEY);
        // Since injvm protocol is configured explicitly, we don't need to set any extra flag, use normal refer process.
        if (Constants.SCOPE_LOCAL.equals(scope) || (url.getParameter(Constants.LOCAL_PROTOCOL, false))) {
            // if it's declared as local reference
            // 'scope=local' is equivalent to 'injvm=true', injvm will be deprecated in the future release
            return true;
        } else if (Constants.SCOPE_REMOTE.equals(scope)) {
            // it's declared as remote reference
            return false;
        } else if (url.getParameter(Constants.GENERIC_KEY, false)) {
            // generic invocation is not local reference
            return false;
        } else if (getExporter(exporterMap, url) != null) {
            // by default, go through local reference if there's the service exposed locally
            return true;
        } else {
            return false;
        }
    }

Local calls also go through the Filter chain

Different from the real local method call, Dubbo local call will pass through the Filter chain, including the Filter chain on the Consumer side and the Filter chain on the Provider side.

Through such a mechanism, local consumers and other consumers are treated, monitored and managed in a unified manner.

How to enable local call

By default, local calls are enabled automatically, and no additional configuration is required. Only when it needs to be closed, it needs to be closed explicitly through the configuration of scope.

However, it should be noted that local calls cannot be used in the following cases:

First, local calls cannot be used when generalizing calls.

Second, the consumer explicitly specifies the URL to initiate a direct call. Of course, if the consumer specifies the URL of the injvm, the final call is also called locally, such as:

<Dubbo:reference id="demoService" interface="org.apache.Dubbo.samples.local.api.DemoService" url="injvm://127.0.0.1/org.apache.Dubbo.samples.local.api.DemoService"/>

How to close a local call

The local call can be closed. In this way, the service provider can treat the remote service consumer and the local consumer equally.

The specific method is to close the exposure of the injvm protocol through scope = "remote". In this way, even the local caller needs to obtain the service address list from the registry before initiating the call,

The calling process at this time is consistent with that of the remote service consumer.

last

Share a set of front-end materials sorted out by Alibaba Daniel for you, Click the front-end proofreading and face-to-face test question compilation and analysis You can download it for free

❤️ Thank you for your support. Don't forget to pay attention and praise if you like.

Share a set of front-end materials sorted out by Alibaba Daniel for you, Click the front-end proofreading and face-to-face test question compilation and analysis You can download it for free*

❤️ Thank you for your support. Don't forget to pay attention and praise if you like.

Keywords: Front-end Interview Programmer

Added by Decipher on Sun, 16 Jan 2022 00:43:06 +0200