Dubbo bit SPI Primer

Source: Internet
Author: User

SPI (service Providerinterface) is a powerful guarantee for Dubbo. Core support class Extensionloader.

The concrete analysis can refer to the <DUBBO principle to analyze the-dubbo kernel implementation based on the SPI Idea Dubbo kernel implementation;

1. More important annotations

@SPI: Identity of the extension point interface: scope on class;

@Adaptive: Provides parameters for generating Adaptive instances, scoped on classes or methods;

@Activate: The load extension can be automatically activated by the framework, and this annotation is used to configure the extension to automatically activate the load condition.

1.1 Test object code

#1. Declare spi  default to Imp1@spi ("Impl1") public interface simpleext {    //  No @adaptive ! with key      @Adaptive     string echo (url url, string s);          @Adaptive ({"Key1",  "Key2"})     string  yell (url url, string s);    //  no @adaptive !     string bang (url url, int i);} Implementing Class 1public class simpleextimpl1 implements simpleext {     Public string echo (url url, string s)  {         return  "Ext1impl1-echo";    }         public string yell (url url, string s)  {         return  "Ext1impl1-yell";    }        public string bang (URL url,  Int i)  {        return  "Bang1";     }}//Implementation Class 2public class simpleextimpl2 implements simpleext {     Public string echo (url url, string s)  {         return  "Ext1impl2-echo";    }         public string yell (url url, string s)  {         return  "Ext1impl2-yell";    }    public  String bang (url url, int i)  {        return   "Bang2";     }    }//implementation class 3PUBLIC&NBSP;CLASS&NBSP;SIMPLEEXTIMPL3  implements simpleext&nBsp {    public string echo (url url, string s)  {         return  "Ext1impl3-echo";    }         public string yell (url url, string s)  {         return  "Ext1impl3-yell";    }     public string bang (url url, int i)  {         return  "Bang3";     }    }

1.2 Configuration file Com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt

Position to be placed in the following position

private static final String services_directory = "meta-inf/services/";p rivate static final String dubbo_directory = "meta- inf/dubbo/";p rivate static final String dubbo_internal_directory = dubbo_directory +" internal/";

Specific content is as follows

# Comment 1impl1=com.alibaba.dubbo.common.extensionloader.ext1.impl.simpleextimpl1#hello Worldimpl2= COM.ALIBABA.DUBBO.COMMON.EXTENSIONLOADER.EXT1.IMPL.SIMPLEEXTIMPL2 # Comment 2 impl3= COM.ALIBABA.DUBBO.COMMON.EXTENSIONLOADER.EXT1.IMPL.SIMPLEEXTIMPL3 # with head space

Defines 3 implementations.

1.4 Testing

@Testpublic void Test_getdefaultextension () throws Exception {Simpleext ext = Extensionloader.getextensionloader (simpl    Eext.class). Getdefaultextension ();        Assertthat (ext, instanceof (simpleextimpl1.class));    String name = Extensionloader.getextensionloader (simpleext.class). Getdefaultextensionname (); Assertequals ("Impl1", name);}

Because of @spi ("Impl1"), the default implementation is defined with the name IMP1.

@Testpublic void Test_getextension () throws Exception {Asserttrue (Extensionloader.getextensionloader (    Simpleext.class). GetExtension ("Impl1") instanceof SimpleExtImpl1); Asserttrue (Extensionloader.getextensionloader (Simpleext.class) getextension ("ImpL2") instanceof SimpleExtImpl2);}

Getextensionloader (class<t> type): Returns the concrete implementation class based on the class name. These configuration information is configured in the meta-counterpart file. Of course, you can also use the @extention annotation configuration (except that this annotation is deprecated)

@Testpublic  void test_getadaptiveextension_defaultadaptivekey ()  throws exception {     {        SimpleExt ext =  Extensionloader.getextensionloader (Simpleext.class). Getadaptiveextension ();         map<string, string> map = new hashmap<string, string > ();         //does not specify a specific parameters parameter, so the default implementation is selected, and finally returns impl1         url url = new url ("P1",  "1.2.3.4",  1010,   "path1",  map)         //if the default SPI implementation class is not set, the exception is reported          //java.lang.IllegalStateException: Fail to get  Extension (COM.ALIBABA.DUBBO.COMMON.EXTENSIONLOADER.EXT1.SIMPLEEXT)  name from url (p1:// 1.2.3.4:1010/PATH1)  use keys ([Simple.ext]) &Nbsp;       string echo = ext.echo (url,  "haha");         assertequals ("Ext1impl1-echo",  echo);     }     {        SimpleExt ext =  Extensionloader.getextensionloader (Simpleext.class). Getadaptiveextension ();         map<string, string> map = new hashmap<string, string > ();         map.put ("Simple.ext",  "IMPL2");//Manually configure IMPL2 in the parameters, The parameter is Simple.ext        url url = new url ("P1",  "1.2.3.4", 1010,  "path1",  map);         string echo  = ext.echo (url,  "haha");         assertequals (" Ext1impl2-echo ",  echo);     }} 

@Adaptive Test

@Adaptive ({"Key1", "Key2"}) because the Yell method is declared

 @Testpublic  void test_getadaptiveextension_ Customizeadaptivekey ()  throws exception {    simpleext ext =  extensionloader.getextensionloader (Simpleext.class). Getadaptiveextension ();     Map <String, String> map = new HashMap<String, String> ();     map.put ("Key2",  "ImpL2");     url url = new url ("P1",   "1.2.3.4", 1010,  "path1",  map);    string echo =  Ext.yell (url,  "haha");     assertequals ("Ext1impl2-yell",  echo);     url = url.addparameter ("Key1",  "IMPL3"); //  Note:  url is a value type      echo = ext.yell (url,  "haha");     assertequals ("Ext1Impl3-yell",  echo);} 

If the parameter is not key1,key2, even the parameter value input IMPL1,IMPL2 is meaningless.


Because the Bang method is not modified by the @adaptive, the following code will report an exception

Extensionloader.getextensionloader (Simpleext.class). Getadaptiveextension (). Bang (..);

of interface Com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt is not adaptive method!

The above content, through the way of code walkthrough, explains the Power of the Dubbo SPI mechanism.
If you are interested in the underlying implementation, see the blog.

Reference: Dubbo principle Analysis-dubbo kernel implementation based on SPI thought Dubbo kernel implementation

This article is from a "simple" blog, so be sure to keep this source http://dba10g.blog.51cto.com/764602/1880962

Dubbo bit SPI Primer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.