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