[2020-03-28] Source topics for Dubbo

Foreword

    Use of free time this week, sixty to seventy percent of the way. The main document Dubbo official website and native code debug combine learning basic reading the service export, service introduction and process service calls, yet involves routing, dictionaries and other functions. Following harvest this week to sum comb.

First, export-based event-driven service

   Lift service exports, its name not to be misled, popular point that is exposed and registration services. The service is open exposure refers to the service side of the port, waiting for the end consumer to connect. Registration Service of the upcoming service registration information to the registry. Specific procedures for exposure and service registration, see the article may blogger before   https://www.cnblogs.com/zzq6032010/p/11275478.html  , tells more detail, it will not go into details.

   Dubbo is time to pay attention to the way of exposure and to start the service registration process, the use of event-driven triggers, with the SpringBoot somewhat similar. This is triggered by a specific event-driven logic of the way, also can be used flexibly in the actual development work.

Second, the service is introduced and SPI

    The SPI for Dubbo adaptation extension, see an article before bloggers  https://www.cnblogs.com/zzq6032010/p/11219611.html , but this article was written in relatively simple and obvious, not yet realized the whole .

Below Protocol class as an example, look at the member variables Protocol protocol in ServiceConfig class = ExtensionLoader.getExtensionLoader (Protocol.class) .getAdaptiveExtension () looks like.

 1 package org.apache.dubbo.rpc;
 2 import org.apache.dubbo.common.extension.ExtensionLoader;
 3 public class Protocol$Adaptive implements org.apache.dubbo.rpc.Protocol {
 4 
 5     public void destroy()  {
 6         throw new UnsupportedOperationException("The method public abstract void org.apache.dubbo.rpc.Protocol.destroy() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
 7     }
 8 
 9     public int getDefaultPort()  {
10         throw new UnsupportedOperationException("The method public abstract int org.apache.dubbo.rpc.Protocol.getDefaultPort() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
11     }
12 
13     public org.apache.dubbo.rpc.Exporter export(org.apache.dubbo.rpc.Invoker arg0) throws org.apache.dubbo.rpc.RpcException {
14         if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null");
15         if (arg0.getUrl() == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null");
16         org.apache.dubbo.common.URL url = arg0.getUrl();
17         String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );
18         if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.Protocol) name from url (" + url.toString() + ") use keys([protocol])");
19         org.apache.dubbo.rpc.Protocol extension = (org.apache.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName);
20         return extension.export(arg0);
21     }
22 
23     public org.apache.dubbo.rpc.Invoker refer(java.lang.Class arg0, org.apache.dubbo.common.URL arg1) throws org.apache.dubbo.rpc.RpcException {
24         if (arg1 == null) throw new IllegalArgumentException("url == null");
25         org.apache.dubbo.common.URL url = arg1;
26         String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );
27         if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.Protocol) name from url (" + url.toString() + ") use keys([protocol])");
28         org.apache.dubbo.rpc.Protocol extension = (org.apache.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName);
29         return extension.refer(arg0, arg1);
30     }
31 
32     public java.util.List getServers()  {
33         throw new UnsupportedOperationException("The method public default java.util.List org.apache.dubbo.rpc.Protocol.getServers() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
34     }
35 }

This is obtained after getAdaptiveExtension () proxy class, first seen in the initialization ServiceConfig acquisition protocol is only a proxy Protocol class, and then to determine the specific use to which Protocol implementation class by passing in the Url program is running. This is the essence of SPI adaptation extension.

In addition, at the time of acquisition by getExtension final implementation class method, but also through the packaging wrapper class. See ExtensionLoader class as follows:

 1 private T createExtension(String name) {
 2         Class<?> clazz = getExtensionClasses().get(name);
 3         if (clazz == null) {
 4             throw findException(name);
 5         }
 6         try {
 7             T instance = (T) EXTENSION_INSTANCES.get(clazz);
 8             if (instance == null) {
 9                 EXTENSION_INSTANCES.putIfAbsent(clazz, clazz.newInstance());
10                 instance = (T) EXTENSION_INSTANCES.get(clazz);
11             }
12             injectExtension(instance);
13             Set<Class<?>> wrapperClasses = cachedWrapperClasses;
14             if (CollectionUtils.isNotEmpty(wrapperClasses)) {
15                 for (Class<?> wrapperClass : wrapperClasses) {
16                     instance = injectExtension((T) wrapperClass.getConstructor(type).newInstance(instance));
17                 }
18             }
19             initExtension(instance);
20             return instance;
21         } catch (Throwable t) {
22             throw new IllegalStateException("Extension instance (name: " + name + ", class: " +
23                     type + ") couldn't be instantiated: " + t.getMessage(), t);
24         }
25     }

If there is an interface wrapper, the process in line 16 of wrapper classes, the current class instance encapsulated package, and then returns an instance of packaging, i.e., the extension class realized by this line of code Decorator transformation.

Here likewise Protocol class, for example, Url is in agreement registry, so I finally called when the execution stack method to export RegistryProtocol path like this:

 

 I.e. after three intermediate Wrapper packaging, each has its own specific function, and independently of each other between the layers. Dubbo expansion in many adaptive interface plus a decoration like this extension, the program's scalable design can also play this way, Interesting!

The process is generally introduced into service so: the consumer side information acquired from the registry server, packaged Invoker, and then packaged into consumer side proxy class injection Spring container. The process is relatively simple, self according to the content on one of the debug.

Three questions, service call

    Always had time before did not look at Dubbo source a question: dubbo consumer side proxy class to call the server end interface when consumption is by netty sending a message in the past, the service end after receiving the news, how to call server-side target class the method? Reflecting it? You can call to reflection method, but can not solve dependency problems, but normal service call to end the Spring container should also have good examples of clients, it is how to find objects in Spring by netty news?

    Dubbo actual process is very simple, as long as the services are exposed when the service will expose themselves to save up enough, time and other consumer end pass over the message, go directly to the map inside to take, is to take the service object encapsulated in Spring , very easy.

    Service call process generally is this: after the call connection to the remote client server, the server side of a maintenance service exposure map, after receiving the request to end service map acquiring Exporter, exporter server has encapsulated Invoker, services held in bean Spring ultimately complete the call. Intermediate further relates many details, such as the package and the call netty, deserialization sequence, load balancing and fault tolerance and the like.

summary

    Dubbo rpc service as an excellent framework, its advantage lies in its more than rpc process, but also in more detail to achieve modules and scalable design, such as a serial process, thread scheduling load balancing, fault tolerance, netty, the routing, Dictionary ... a lot of content, later going to do for four load balancing algorithm dubbo of about research, a little, careless!

 

Guess you like

Origin www.cnblogs.com/zzq6032010/p/12588538.html