current position:Home>How does Dubbo support local calls? Injvm mode analysis, java basic programming steps

How does Dubbo support local calls? Injvm mode analysis, java basic programming steps

2022-01-27 02:33:02 Alibaba_ Open Source

InjvmProtocol Source code learning

injvm The relevant code is implemented in Dubbo-rpc-injvm Module , Mainly InjvmExporter、InjvmInvoker and InjvmProtocol.

InjvmProtocol Inherit 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 Exporter export(Invoker invoker) throws RpcException {
return new InjvmExporter(invoker, invoker.getUrl().getServiceKey(), exporterMap);
}

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

except export and refer Method ,InjvmProtocol Provides

《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing

isInjvmRefer() Method ,

isInjvmRefer Will read the configuration file , Determine whether local call is enabled .

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;
}
}

The local call also goes through Filter chain

What's different from a real local method call is ,Dubbo The local call will go through Filter chain , Which includes Consumer Terminal Filter Chain and Provider Terminal Filter chain .

Through such a mechanism , Local consumers and other consumers are treated in a unified way , Unified monitoring , Services are managed in a unified way .

How to enable local call

By default , The local call is enabled automatically , There is no need for additional configuration . Only when it needs to be closed , To get through scope Configuration to explicitly close .

however , In particular, it should be noted that , In the following cases , Local calls are not available :

First of all , You can't use a local call when making a generic call .

second , The consumer explicitly specifies URL Initiate direct connect call . Of course , If the consumer specifies injvm Of URL, The final call is also a local call , such as :

First of all , You can't use a local call when making a generic call .

second , The consumer explicitly specifies URL Initiate direct connect call . Of course , If the consumer specifies injvm Of URL, The final call is also a local call , such as :

copyright notice
author[Alibaba_ Open Source],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270233014214.html

Random recommended