A service reference is the process of referencing a service to a service provider after the service's consumption is directed to the service address provided by the Registry's subscription service provider.
The application of the service in spring configuration example is as follows:
< dubbo:reference id="Demoservice"interface="Com.alibaba.dubbo.demo. Demoservice "/>
As configured on spring, the custom schema elements are parsed <dubbo:reference/> converted into Dubbo internal data structures when the container is started Referencebean
Referencebean In addition to inheriting the configured abstract class to handle the configuration information, it also implements some of the spring container interface, here we analyze the next Factorybean, first it is a bean for spring, participate in the bean creation of all life cycle, The key is that spring's bean factory Beanfactory.getbean ("Demoservice") gets the bean when it determines if it is a Factorybean instance, if it is a factorybean.getobject () Returns, otherwise returns the bean.
The demoservice we get for a remote call is not actually trying to referencebean the object instance itself, we want to get the proxy for the remote call and be able to invoke the remote service through this proxy service. Here is the use of factorybean.getobject () to create a reference to return a proxy based on the Demoservice interface to the reference, which encapsulates a complex implementation of the user's transparent Dubbo.
Procedure for creating an agent:
1. Get the consumer configuration
2. Get the configured registry, configure the assemble URL through the configuration center, and it should be a configuration center cluster on the line
3. Traverse the Registry List<url> Collection
Load Monitoring Center URL if monitoring Center is configured in the registry URL plus Monitor_key
Add Refer_key to the registered URL based on the configured reference service parameter
4. Traverse the registry List<url> collection, where the registry URL contains Monitorurl and Referurl
Protocol.refer (Interface,url) calls the Protocol reference service to return the Invoker executable object (this invoker is not a simple dubboinvoker, Instead, the Registryprotocol builds a directory service-based cluster policy invoker, which Invoker can make a truly callable remote service Invoker through the Directory Service list)
For the registry URL settings, the cluster policy is availablecluster, and the invoker that is called by Availablecluster all object registrations is disguised as a invoker
5. Create a remote service proxy through the proxy factory return to the use of Proxyfactory.getproxy (Invoker);
Procotol.refer (interface, URL) process for referencing services
1. After Protocollistenerwrapper, protocolfilterwrapper because it is a registry URL call Registryprotocol.refer
2. Get the Registry agreement zookeeper, Redis, or Dubbo, and obtain the Registrar registry to interact with the registry based on the Registry Protocol through the Registrar Factory registryfactory.getregistry (URL)
3. According to the configured group group
4. Create a Registry service catalog Registrydirectory and set up the Registrar
5. Building a subscription service Subscribeurl
6. Register the Subscribeurl consumer URL via the Registrar registry to the Registration center
7. Directory Services Registrydirectory.subscribe (SUBSCRIBEURL) subscription service (here we take the open source version zookeeper as an example of the registry, the Dubbo protocol registry is a bit different)
In fact the interior is also through the Registrar registry.subscribe (url,this) Here This is registrydirectory it realizes the Notifylistener.
The service provider writes down its own URL address to the Zookeeper Registry Service/dubbo/com.alibaba.dubbo.demo.demoservice/providers/node
The service consumer writes down its own URL address to the ZOOKEEPR registration service/dubbo/com.alibaba.dubbo.demo.demoservice/consumers/node
Service consumer to Zookeeper subscription service/dubbo/com.alibaba.dubbo.demo.demoservice/providers/node all service provider URL address
Zookeeper through the watcher mechanism to monitor the node, the node data changes through the watcher callback client on the node, regenerate the refer to the service
In the process of subscribing by obtaining URLs for all service providers/dubbo/com.alibaba.dubbo.demo.demoservice/providers/under (similar to dubbo://10.33.37.8:20880/ com.alibaba.dubbo.demo.demoservice?anyhost=true&application=demo-consumer&check=false&dubbo=2.0.0 &generic=false&interface=com.alibaba.dubbo.demo.demoservice&methods=sayhello&owner=william &pid=7356&side=consumer×tamp=1416971340626), the active callback Notifylistener to generate a reference to the service provider based on the URLs generated executable invokers For the directory service to hold,
Look at the code implementation in the following registrydirectory.notify (URLs) method
8. Merging invoker through Cluster.join (directory) and providing a cluster invocation strategy
Dubboprotocol.refer Process:
1. After Protocollistenerwrapper, Protocolfilterwrapper constructs the listener chain and the filter chain.
2. Dubboprotocol gets the Exchangeclient object based on the URL, and if it is share exists, the return does not exist to create a new object that is not share created directly. Exchangeclient is the client for the underlying communication and is not explained here for the communication layer creation function.
3. Create Dubboinvoker, this Invoker object contains a long link to the remote service provider and is the executable object that actually executes the remote service call
4. Return the created Invoker to the directory service
Sequence diagram of application services for official documents
Publish a service activity diagram:
9. Dubbo Principle Analysis-Proxy service Reference