Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize SPI Adaptive extension of Dubbo

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to achieve the SPI adaptive extension of Dubbo". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve the SPI adaptive extension of Dubbo".

First define the interface class SpiTest, which has a mySpi method:

@ SPI / / marked as the extended interface public interface SpiTest {void mySpi (URL url, String name);}

There are two implementation classes S1 and S2:

Public class S1 implements SpiTest {@ Override public void mySpi (URL url,String name) {System.out.println ("This is S1:" + name);}} public class S2 implements SpiTest {@ Override public void mySpi (URL url,String name) {System.out.println ("This is S2:" + name);}}

Create the file com.xx.dubbospi.SpiTest under META-INF/dubbo

S1=com.zf.xx.dubbospi.S1S2=com.zf.xx.dubbospi.S2

In normal use, you can get an implementation of SpiTest through ExtensionLoader.getExtensionLoader (SpiTest.class) .getExtension ("S1"), but what if you are not sure about the specific implementation class when the method is called? You can define a wrapper class SpiWrapper. The wrapper class does not specify the implementation method, but only acquires the corresponding extension object according to the parameters, and gets S1 or S2 according to the passed parameters:

Public class SpiWrapper implements SpiTest {@ Override public void mySpi (URL url,String name) {SpiTest spiTest = ExtensionLoader.getExtensionLoader (SpiTest.class) .getExtension (url.getParameter ("spi.test")); / / specify the object to be loaded by parameter spiTest.mySpi (url,name);} public static void main (String [] args) {URL url = new URL ("dubbo", "123,999") / / the URL here is org.apache.dubbo.common.URL url = url.addParameter ("spi.test", "S2"); SpiWrapper spiWrapper = new SpiWrapper (); spiWrapper.mySpi (url, "tudou");}}

In SpiWrapper, according to the value class of the parameter spi.test on url, which implementation class of SpiTest is determined to the bottom, so that a dynamic extension of SPI is implemented. However, the specific use in Dubbo requires the transformation of SpiTest first:

@ SPIpublic interface SpiTest {@ Adaptive void mySpi (URL url, String name);}

Adding @ Adaptive,Adaptive to the method tells Dubbo which implementation class should be used to call the mySpi method. The implementation logic is to extract the key value from the URL (key-value) by convention, and determine the implementation class by the key value. For example, if we use protocol as Dubbo, then specify dubbo.protocol.name as dubbo through the yml file, and the format on URL is protocol=dubbo. Later, when we execute the service export export, we will load DubboProtocol to implement it.

@ SPI ("dubbo") / / default dubbopublic interface Protocol {@ Adaptive Exporter export (Invoker invoker) throws RpcException } profile filter=org.apache.dubbo.rpc.protocol.ProtocolFilterWrapperlistener=org.apache.dubbo.rpc.protocol.ProtocolListenerWrappermock=org.apache.dubbo.rpc.support.MockProtocoldubbo=org.apache.dubbo.rpc.protocol.dubbo.DubboProtocolinjvm=org.apache.dubbo.rpc.protocol.injvm.InjvmProtocolhttp=org.apache.dubbo.rpc.protocol.http.HttpProtocolrmi=org.apache.dubbo.rpc.protocol.rmi.RmiProtocolhessian=org.apache.dubbo.rpc.protocol.hessian.HessianProtocolorg.apache.dubbo.rpc.protocol.webservice.WebServiceProtocolthrift=org.apache.dubbo.rpc.protocol.thrift.ThriftProtocolnative -thrift=org.apache.dubbo.rpc.protocol.nativethrift.ThriftProtocolmemcached=org.apache.dubbo.rpc.protocol.memcached.MemcachedProtocolredis=org.apache.dubbo.rpc.protocol.redis.RedisProtocolrest=org.apache.dubbo.rpc.protocol.rest.RestProtocolxmlrpc=org.apache.dubbo.xml.rpc.protocol.xmlrpc.XmlRpcProtocolgrpc=org.apache.dubbo.rpc.protocol.grpc.GrpcProtocolregistry=org.apache.dubbo.registry.integration.RegistryProtocolservice-discovery-registry=org.apache.dubbo.registry.client.ServiceDiscoveryRegistryProtocolqos=org.apache.dubbo.qos.protocol.QosProtocolWrapper

Let's take a look at our example:

Public class SpiWrapper implements SpiTest {@ Override public void mySpi (URL url,String name) {/ / mainly by finding the value of the spi.test parameter on Url, ExtensionLoader.getExtensionLoader (SpiTest.class) .getExtension ("S1") SpiTest spiTest = ExtensionLoader.getExtensionLoader (SpiTest.class) .getAdaptiveExtension (); / / get the adaptive object through the url parameter / / SpiTest spiTest = ExtensionLoader.getExtensionLoader (SpiTest.class) .getExtension (url.getParameter ("spi.test")) SpiTest.mySpi (url,name);} public static void main (String [] args) {URL url = new URL ("dubbo", "123,999"); url = url.addParameter ("spi.test", "S1"); / / specify url parameter SpiWrapper spiWrapper = new SpiWrapper (); spiWrapper.mySpi (url, "tudou") }} Thank you for your reading. The above is the content of "how to achieve the SPI adaptive extension of Dubbo". After the study of this article, I believe you have a deeper understanding of how to achieve the SPI adaptive extension of Dubbo, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report