Dubbo Source Analysis (ii): Design patterns used in Dubbo

Source: Internet
Author: User

1. Factory mode

There is a field in ServiceConfig, and the code is this:

View Text Printing

private static final Protocol Protocol =  Extensionloader.getextensionloader (Protocol.class). Getadaptiveextension ();

There's a lot of this code in Dubbo. This is also a factory model, only the implementation of the class is obtained using the JDKSPI machine

System. The advantage of this implementation is strong scalability, want to extend the implementation, only need to add a file under the Classpath to

In the code 0 intrusion. In addition, like the adaptive implementation above, you can make a call to dynamically decide which real

now , but because this implementation uses the dynamic proxy, will cause the code debugging to be troublesome, need to analyze the actual call

The implementation class.


2. Adorner mode

Dubbo the adorner mode in both the start and call stages. Take the call chain provided by provider as an example, with

The call chain code for the body is done in Protocolfilterwrapper Buildinvokerchain, specifically to include

There are group=provider filter implementations, sorted by order, and the last call order is

View Text Printing

echofilter-"classloaderfilter-" genericfilter-"contextfilter-" exceptionfilter-"

timeoutfilter-"monitorfilter-" Tracefilter.

Rather, this is a mix of adorners and chain of responsibility patterns. For example, the role of Echofilter is to determine

Whether it is an echo test request, or a direct return to the content, this is the embodiment of a chain of responsibility. And like Classloaderfilter

Only adds functionality to the main function, changing the classloader of the current thread, which is a typical adorner pattern.


3. Observer mode

Dubbo provider start, you need to interact with the registry, register your own services, and then subscribe to your own

When subscribing, the viewer mode is used to open a listener. The registration center will check the service every 5 seconds

Update, if there is an update, send a notify message to the provider of the service, provider receive the Notify message

After that, run the Notifylistener notify method to execute the listener method.


4. Dynamic Agent Mode

The adaptive implementation of class Extensionloader for Dubbo extended Jdkspi is a typical dynamic proxy implementation. Dubbo

There is a need for flexible control of the implementation class, that is, in the call phase dynamically determines which implementation class to invoke, so use Mr.

The method of proxy class, can make flexible call. The code that generates the proxy class is Extensionloader

Createadaptiveextensionclasscode method. The primary logic of the proxy class is to get the parameters specified in the URL parameter

Value as the key to get the implementation class.


Dubbo Source Analysis (ii): Design patterns used in Dubbo

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.