In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the introduction of the SPI mechanism of Dubbo and the use of Activate". In the daily operation, I believe many people have doubts about the introduction of the SPI mechanism of Dubbo and the use of Activate. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "the introduction of the SPI mechanism of Dubbo and the use of Activate". Next, please follow the editor to study!
1. @ Activate example of Dubbo @ SPIpublic interface ActivateExt {String echo (String msg);} @ Activate (group = {"default"}) public class ActivateExtImpl1 implements ActivateExt {@ Override public String echo (String msg) {return msg;}} @ Activate (group = {"group1", "group2"}) public class GroupActivateExtImpl implements ActivateExt {@ Override public String echo (String msg) {return msg } @ Activate (order = 2, group = {"order"}) public class OrderActivateExtImpl1 implements ActivateExt {@ Override public String echo (String msg) {return msg;}} @ Activate (order = 1, group = {"order"}) public class OrderActivateExtImpl2 implements ActivateExt {@ Override public String echo (String msg) {return msg } @ Activate (value = {"value"}, group = {"group"}) public class ValueActivateExtImpl implements ActivateExt {@ Override public String echo (String msg) {return msg;}}
Create a new META-INF/dubbo/internal folder under resources, and create a new com.alibaba.dubbo.demo.provider.activate.ActivateExt, that is, the fully qualified name of the API. The file content is as follows:
Group=com.alibaba.dubbo.demo.provider.activate.impl.GroupActivateExtImplkey=com.alibaba.dubbo.demo.provider.activate.impl.ValueActivateExtImplorder1=com.alibaba.dubbo.demo.provider.activate.impl.OrderActivateExtImpl1order2=com.alibaba.dubbo.demo.provider.activate.impl.OrderActivateExtImpl2com.alibaba.dubbo.demo.provider.activate.impl.ActivateExtImpl1
The following are several unit tests and their test results. By observing the test results, we can draw the following conclusions:
If the group parameter in loader.getActivateExtension (URL url, String [] values, String group) can match, that's what we need to choose. If ActivateExtImpl1 is matched according to group=default in test1, it also needs to be activated under this condition.
Value in @ Activate is the second layer check parameter, and the first layer is group. If the group check passes, if the key parameter in URL is consistent with the value parameter in the class @ Activate annotation and the corresponding value of the key parameter is not empty, it will be selected
The smaller order, the higher priority
/ * 1 * class com.alibaba.dubbo.demo.provider.activate.impl.ActivateExtImpl1 * / @ Testpublic void test1 () {ExtensionLoader loader = ExtensionLoader.getExtensionLoader (ActivateExt.class); URL url = URL.valueOf ("test://localhost/test"); List list = loader.getActivateExtension (url, new String [] {}, "default"); System.out.println (list.size ()); list.forEach (item-> System.out.println (item.getClass () } / * * 1 * class com.alibaba.dubbo.demo.provider.activate.impl.GroupActivateExtImpl * / @ Testpublic void test2 () {ExtensionLoader loader = ExtensionLoader.getExtensionLoader (ActivateExt.class); URL url = URL.valueOf ("test://localhost/test"); List list = loader.getActivateExtension (url, new String [] {}, "group1"); System.out.println (list.size ()); list.forEach (item-> System.out.println (item.getClass () } / * 2 * class com.alibaba.dubbo.demo.provider.activate.impl.OrderActivateExtImpl1 * class com.alibaba.dubbo.demo.provider.activate.impl.ValueActivateExtImpl * / @ Testpublic void test3 () {ExtensionLoader loader = ExtensionLoader.getExtensionLoader (ActivateExt.class); URL url = URL.valueOf ("test://localhost/test"); / / Note that url is used to receive here, and cannot directly url.addParameter () url = url.addParameter ("value", "test") List list = loader.getActivateExtension (url, new String [] {"order1", "default"}, "group"); System.out.println (list.size ()); list.forEach (item-> System.out.println (item.getClass () } / * 2 * class com.alibaba.dubbo.demo.provider.activate.impl.OrderActivateExtImpl2 * class com.alibaba.dubbo.demo.provider.activate.impl.OrderActivateExtImpl1 * / @ Testpublic void test4 () {ExtensionLoader loader = ExtensionLoader.getExtensionLoader (ActivateExt.class); URL url = URL.valueOf ("test://localhost/test"); List list = loader.getActivateExtension (url, new String [] {}, "order"); System.out.println (list.size ()) List.forEach (item-> System.out.println (item.getClass ();} 2. Source code analysis public void test3 () {ExtensionLoader loader = ExtensionLoader.getExtensionLoader (ActivateExt.class); URL url = URL.valueOf ("test://localhost/test"); / / Note that url is used for receiving, not url.addParameter () url = url.addParameter ("value", "test") directly. List list = loader.getActivateExtension (url, new String [] {"order1", "default"}, "group"); System.out.println (list.size ()); list.forEach (item-> System.out.println (item.getClass ();} public List getActivateExtension (URL url, String [] values, String group) {List exts = new ArrayList (); / / Packaging the passed values as names List names = values = = null? New ArrayList (0): Arrays.asList (values); / / the packaged data does not contain "- default" if (! names.contains (Constants.REMOVE_VALUE_PREFIX + Constants.DEFAULT_KEY)) {/ / get all the extended information of this type of data getExtensionClasses () For (Map.Entry entry: cachedActivates.entrySet ()) {/ / get the name of the extension, the key value String name = entry.getKey () in the key=value configured in the configuration file; / / get the annotation Activate activate = entry.getValue () / / determine whether group matches. Group is the value passed in by the program. If it is not set, return true to indicate matching. / / if you set the need to compare and match if (isMatchGroup (group, activate.group () {/ / get the key value of the instance T ext = getExtension (name) corresponding to name. If (! names.contains (name) & &! names.contains (Constants.REMOVE_VALUE_PREFIX + name) & & isActive (activate, url)) {exts.add (ext);} / sort Collections.sort (exts, ActivateComparator.COMPARATOR) } List usrs = new ArrayList (); for (int I = 0; I
< names.size(); i++) { String name = names.get(i); if (!name.startsWith(Constants.REMOVE_VALUE_PREFIX) && !names.contains(Constants.REMOVE_VALUE_PREFIX + name)) { if (Constants.DEFAULT_KEY.equals(name)) { // 如果name是default,就将usrs加入exts头部并清空usrs if (!usrs.isEmpty()) { exts.addAll(0, usrs); usrs.clear(); } } else { // 获取name对应的拓展并加入usrs T ext = getExtension(name); usrs.add(ext); } } } if (!usrs.isEmpty()) { exts.addAll(usrs); } return exts;} 上述代码中用到的cachedActivates是在getExtensionClasses()方法中赋值的,具体可以参考: Dubbo的SPI机制分析1-SPI加载class Activate activate = clazz.getAnnotation(Activate.class);if (activate != null) { cachedActivates.put(names[0], activate);}// 判断group是否匹配,很好理解private boolean isMatchGroup(String group, String[] groups) { if (group == null || group.length() == 0) { return true; } if (groups != null && groups.length >0) {for (String g: groups) {if (group.equals (g)) {return true;} return false;} / / match the key key in url with the value value in Activate comments, and the value value corresponding to key cannot be empty private boolean isActive (Activate activate, URL url) {String [] keys = activate.value () If (keys.length = = 0) {return true;} for (String key: keys) {for (Map.Entry entry: url.getParameters (). EntrySet ()) {String k = entry.getKey (); String v = entry.getValue () If ((k.equals (key) | | k.endsWith ("." + key)) & & ConfigUtils.isNotEmpty (v) {return true;} return false;}
Note that if it is in the following form and there is a-default in the incoming values, then all matches that can be passed through @ Activate will not be activated, only those specified in values can be activated. If an extension class name that matches the beginning of the "-" is passed in, the extension point will not be activated.
/ / output class com.alibaba.dubbo.demo.provider.activate.impl.OrderActivateExtImpl1ExtensionLoader loader = ExtensionLoader.getExtensionLoader (ActivateExt.class); URL url = URL.valueOf ("test://localhost/test"); / / Note that url is used to receive here, not directly url.addParameter () url = url.addParameter ("value", "test"); List list = loader.getActivateExtension (url, new String [] {"order1", "- default"}, "group") System.out.println (list.size ()); list.forEach (item-> System.out.println (item.getClass (); at this point, the study of "introduction to the SPI mechanism of Dubbo and how to use Activate" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.