What 4.dubbo of spi idea is?

Author: Chinese Shi Shan

Interview questions

What dubbo of spi idea is?

Interviewer psychological analysis

Continue in-depth asked chanting in front of some basic things to ask over, you should determine all ok, understand some basic things dubbo, then ask a little bit more difficult problem is spi, spi to ask you is what? Then ask your dubbo of spi is how to achieve?

In fact, you see how the mastery of dubbo.

Face questions analysis

spi is what?

spi, simply put, is  service provider interfacethat white What does it mean, for example, you have an interface, this interface has now achieved three classes, then in the end choose which implementation class of this interface when the system is running it? This requires the spi, need to specify the configuration or default configuration, to find the corresponding implementation classes loaded in, and then the implementation class of the object instance.

For chestnuts.

Do you have an interface A. A1 / A2 / A3 are different implementations of the interfaces A. You through configuration  接口 A = 实现 A2, then when the actual operation of the system will load your configuration, achieved with A2 instantiate an object to provide services.

spi mechanisms are generally used where? Plug-in extensions scene, for example, you develop an open source framework used to someone else, if you want others to write their own plug-ins, which plugs into your open-source framework to extend a feature, this time spi idea to use it .

Reflects the thinking of Java spi

spi classic thinking is reflected, we usually use in, for example, jdbc.

Jdbc Java defines a set of interfaces, but does not provide jdbc Java implementation class.

But in fact, the project ran, what you want to use jdbc interface implementation class it? Generally speaking, we should use according to their own database, such as mysql, you will be  mysql-jdbc-connector.jar introduced come in; oracle, you will be  oracle-jdbc-connector.jar introduced to come.

When the system is running, you encounter using jdbc interface, he will use to achieve that kind of jar you into provided at the bottom.

dubbo idea of ​​spi

dubbo also used the spi thought, but did not use spi jdk mechanism is a mechanism spi own implementation.

Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();

Protocol Interface, when the system is running ,, dubbo will determine what should be selected which this Protocol interface implementation class to instantiate an object to use.

It will go to a Protocol your configuration, you will configure the Protocol implementation class, loaded into the jvm in the past, and then instantiate an object, use your implementation class that Protocol on it.

The above line of code is dubbo in heavy use, that is, many components are retained and multiple implementations of an interface, and then dynamically to find the corresponding implementation class depending on the configuration when the system is running. If you do not configure the default implementation would go well, no problem.

@SPI("dubbo")  
public interface Protocol { int getDefaultPort(); @Adaptive <T> Exporter<T> export(Invoker<T> invoker) throws RpcException; @Adaptive <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException; void destroy(); } 

In dubbo own jar, in the /META_INF/dubbo/internal/com.alibaba.dubbo.rpc.Protocolfile:

dubbo=com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol
http=com.alibaba.dubbo.rpc.protocol.http.HttpProtocol
hessian=com.alibaba.dubbo.rpc.protocol.hessian.HessianProtocol

So, which saw the spi dubbo default mechanism of the play is how, in fact Protocol Interface, @SPI("dubbo") said that the implementation class provided by SPI mechanisms, implementation class is found by dubbo as the default key to the configuration file , the profile name with the interface fully qualified name of the same, as the key can be found by dubbo default implementation is  com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.

If you want to replace the default implementation of dynamic classes, you need to use  @Adaptive Interface, Protocol interface, there are two ways to add a  @Adaptive comment, that is to say that both interfaces are proxy implementation.

What does it mean?

For example, the two engage in this Protocol Interface  @Adaptive comment tagging methods will generate proxy classes for the Protocol at runtime, that both methods of this class of agents which will have the agent code, agent code dynamically based on the protocol to the url at runtime get the key, default is dubbo, you can also specify your own, if you are the other key is specified, it will get another instance of the implementation class.

How to expand your components in dubbo

Here is how to say in their own extension component dubbo.

Write their own works, and if that can be labeled jar package, inside the  src/main/resources directory, to engage in a  META-INF/services, which put a file called: com.alibaba.dubbo.rpc.Protocol, engage in a file my=com.bingo.MyProtocol. The nexus PW own jar to get going.

Then do yourself a  dubbo provider project, in this project which depend on your own out of the jar, and then to a configuration in the spring configuration file:

<dubbo:protocol name=”my” port=”20000” />

provider startup, will load the bag into our jar my=com.bingo.MyProtocol line in the configuration, then you can use a defined MyProtocol depending on your configuration, this is simple to explain, you the manner described above, you can replace a large number of dubbo internal components, it is throw your jar package, and then configure click.

dubbo-spi

dubbo which provides a large number of similar extension point above, that is to say, if you want to extend a thing, as long as they write a jar, let your consumer or provider works that rely on your jar, specify the directory where your jar under profiles good interface name corresponding to the inside through  key=实现类.

Then for the corresponding component, similar  <dubbo:protocol> with that of your key corresponding implementation class to implement an interface, you can extend yourself to various functions dubbo provide your own implementation.

Guess you like

Origin www.cnblogs.com/morganlin/p/11986080.html
Recommended