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 the Dubbo source code parses the service reference principle

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Dubbo source code how to parse the principle of service reference, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

I think learning a framework is nothing more than three steps.

Master basic use

I have read the source code and know how it works.

Copy the source code and write a simple framework by yourself

In fact, we all know that the most important thing in programming is to do more. That is, the third step is the most important. But the reality is also very cruel, the vast majority of people stay in the first step. The second step alone has some fear in people's hearts. So when I was writing a service reference, I thought of a clip I saw when I was a child. At that time, A Dream of Red Mansions was banned. Ji Xiaolan changed the name of A Dream of Red Mansions into a stone in order to let the empress dowager see it. In this way, the Empress Dowager naturally had no burden on her heart. I think it might be more appropriate to describe it with a picture.

Of course, the process of copying the source code, according to Fei Chao's humble opinion, also needs to be divided into three processes, namely, the entry version (expressing the principle of the framework with the simplest code), the advanced version (adding design patterns, etc.). Optimize the code on the basis of the entry version), the advanced version (basically consistent with the framework code).

Insert interview questions

Describe the process and principle of dubbo service reference

Now that you have mentioned that dynamic proxies are used to encapsulate communication details in dubbo service references, what are the common ways to create dynamic proxies, and what is the difference between them? which one is used in dubbo? (high frequency questions)

Do you know any other ways to implement proxies besides JDK dynamic agents and CGLIB dynamic agents? (high degree of differentiation)

Forgive him.

Look at the source code for most people, the most difficult point is "where to start with the source code." Although I have answered this question in dozens of previous dubbo source code parsing, but every time I send out, there are still private messages from my friends asking me the same question. In this regard, of course, I choose to forgive him. Therefore, this article I once again rude point question, "how to look at the source code." Take this article as an example, where should we start to look at the principle of this service reference? Let's take a look at the official documents.

If you have implemented all the demo I posted in the previous article and see this picture again, it is not difficult to conclude that the service reference has done nothing more than two things.

Convert the schemas tag information of spring into bean, and then use the information of this bean to connect and subscribe to zookeeper node information to create an invoker

Create a dynamic proxy object with the information of invoker

Warm Tip: in addition to looking at the official documents, I also mentioned starting from the output log in the principle of dubbo source code parsing-service exposure. Of course, I listed two ways here just for your reference, not that there are only two ways, nor that these two are the best.

Time sequence diagram

Go straight to the topic

Some friends feedback that code mapping is not friendly to mobile phone reading, but if it is not posted, it will take a lot of time for many friends to find corresponding classes and methods after reading their own debug, so compromise, the combination of mapping and sticking code

Public Invoker refer (Class type, URL url) throws RpcException {url = url.setProtocol (url.getParameter (Constants.REGISTRY_KEY, Constants.DEFAULT_REGISTRY)) .removeParameter (Constants.REGISTRY_KEY); / / serial number 2. The logic here is basically the same as the 'zookeeper connection' shared before. If you are not familiar with it, you can go back and see Registry registry = registryFactory.getRegistry (url). If (RegistryService.class.equals (type)) {return proxyFactory.getInvoker ((T) registry, type, url);} / / group= "an or group=" * "Map qs = StringUtils.parseQueryString (url.getParameterAndDecoded (Constants.REFER_KEY)); String group= qs.get (Constants.GROUP_KEY) If (group! = null & & group.length () > 0) {if ((Constants.COMMA_SPLIT_PATTERN.split (group)). Length > 1 | | "*" .equals (group)) {return doRefer (getMergeableCluster (), registry, type, url);} return doRefer (cluster, registry, type, url) } private Invoker doRefer (Cluster cluster, Registry registry, Class type, URL url) {RegistryDirectory directory = new RegistryDirectory (type, url); directory.setRegistry (registry); directory.setProtocol (protocol); URL subscribeUrl = new URL (Constants.CONSUMER_PROTOCOL, NetUtils.getLocalHost (), 0, type.getName (), directory.getUrl (). GetParameters ()); if (! Constants.ANY_VALUE.equals (url.getServiceInterface ()) & & url.getParameter (Constants.REGISTER_KEY, true) {registry.register (subscribeUrl.addParameters (Constants.CATEGORY_KEY, Constants.CONSUMERS_CATEGORY, Constants.CHECK_KEY, String.valueOf (false) } / / sequence number 3. The logic here is basically the same as the 'zookeeper subscription' shared before. If you are not familiar with it, you can go back to directory.subscribe (subscribeUrl.addParameter (Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY + "," + Constants.CONFIGURATORS_CATEGORY + "," + Constants.ROUTERS_CATEGORY)). / / the serial number 4 is also mentioned in the cluster fault tolerance series. If you are not familiar with it, you can go back and check return cluster.join (directory);}

The above four steps have completed the conversion of schemas tag information to invoker, so the following is the creation of a proxy object (serial number 5)

Private T createProxy (Map map) {/ / (omit some code) / / create a service proxy return (T) proxyFactory.getProxy (invoker);}

We know that in order to encapsulate this communication detail and allow users to invoke remote services in a local way, we must use proxies. Then when it comes to dynamic proxies, we generally think of two kinds, one is the dynamic proxy of JDK, the other is the dynamic proxy of CGLIB, so let's see what the characteristics of the two are.

The object of JDK's dynamic proxy must implement an interface, while for classes that have no interface, CGLIB. To understand the difference between the two, we must understand the principle, which is repeatedly emphasized before. The principle of CGLIB is also very simple. It generates a subclass for the specified target class and overrides the method to achieve enhancement, but because it uses inheritance, it cannot proxy the class modified by final.

In addition to the above two familiar ways, in fact, there is another way, that is, javassist generates bytecode to implement the proxy (as will be discussed later, dubbo uses javassist in many ways). So which way does dubbo use to implement the proxy? Let's look down.

The answer to the question about how the Dubbo source code parses the service reference principle is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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