Dubbo (5) Service Registration Center

Source: Internet
Author: User
Tags zookeeper

First of all, Dubbo is essentially an RPC framework, an indispensable 2 role: service providers and service consumers.

The service-side exposure port process has been profiled. This article simply describes the registration center.

1. What is a registry?

This is the legend provided by the official website

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/8B/F7/wKioL1hd64ujeWFHAAHY0C6Ailg777.png "title=" 1.png " alt= "Wkiol1hd64ujewfhaahy0c6ailg777.png"/>

Through the legend, it can be seen that consumers and providers are not directly communicating, there is a third party in the middle, is the registration center. This structure can realize the dependence of both consumers and providers, and the clustering of parameter information. So this brings up a few problems.

    1. Service Registration

    2. Service discovery

    3. Service subscription

    4. Service Notifications

2. Service exposure and service registration process

"Dubbo Drip (4) Exposure service Analysis" has analyzed the Dubbo protocol specifically open the network port process. The content of this section will be hidden from this section. Because of a complete service exposure, mainly involving 2 parts, 1) opening the port waiting for the consumer to connect; 2) Register the service information with the registry to inform consumers that they can connect.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/8B/FA/wKiom1hd7nHhegDoAADMiy-mF8w911.png "title=" 2.png " alt= "Wkiom1hd7nhhegdoaadmiy-mf8w911.png"/>

There are 3 points to note:

1) First, a INJVM local Service (step 6) will be exposed based on the criteria;

Injvmprotocol protocol is completed, mainly for the same jvm species of consumer calls, to provide RPC efficiency.

2) exposing a Dubbo service for the service (step 12), generally Dubboprotocol completed

3) The services provided by step 12 are registered with the registration center (step 13-step 23). This step is the focus of this paper.

3. Knowledge of the registration centre

650) this.width=650; "src=" http://www.myexception.cn/img/2014/08/01/110353770.jpg "width=" 854 "height=" 635 "alt=" 110353770.jpg "/>

The graph is the overall structure of the Dubbo. Focus on the resistry layer. What's important is that several components

Zookeeperregistry : Responsible for interacting with zookeeper

Registryprotocol : Gets the available services from the registry, or registers the service with the zookeeper, then provides the service or provides the calling agent.

registrydirectory : Maintains all available remote invoker or local invoker. This class implements the Notifylistner.

Notifylistener : Responsible for registrydirectory and zookeeperregistry communication.

Failbackregistry: Inherits from registry, implements the failure retry mechanism.

4. Registration Center Data Model

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/8B/F7/wKioL1hd9GbhoQpFAAB6Ku26nf4144.jpg "title=" 55.jpg "alt=" Wkiol1hd9gbhoqpfaab6ku26nf4144.jpg "/>

Process Description:

    • When the service provider starts

      • Write your own URL address to the/dubbo/com.foo.barservice/providers directory.

    • When the service consumer starts

      • Subscribe to the provider URL address under the/dubbo/com.foo.barservice/providers directory.

      • and write your own URL address to the/dubbo/com.foo.barservice/consumers directory.

    • When the monitoring center starts

      • Subscribe to all provider and consumer URL addresses in the/dubbo/com.foo.barservice directory.

4.Registry Structure Tree

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/8B/F7/wKioL1hd9eThQ4lUAABw_9KRcyI681.png "title=" 6.png " alt= "Wkiol1hd9ethq4luaabw_9krcyi681.png"/>

Zookeeperregistry is a common registry implementation that is constructed by zookeeperregistryfactory.

Abstractregistry This class primarily stores the service interfaces that have been registered, the service interfaces that have been subscribed to, and the URLs of the interfaces that have been notified, and cannot be called directly.

public abstract class abstractregistry implements registry {     //  Local Disk cache, with a special key value. Registies Record Registry list, others are notified service provider list     private final  properties properties = new properties ();    //  file cache timed write     private final ExecutorService registryCacheExecutor =  Executors.newfixedthreadpool (1, new namedthreadfactory ("Dubbosaveregistrycache",  true));     //whether to save files synchronously     private final boolean syncSaveFile ;     private final AtomicLong lastCacheChanged = new  Atomiclong ();     private final set<url> registered = new  ConcurrentHashSet<URL> ();     private final concurrentmap<url,  set<notifylistener>>&nbsP;subscribed = new concurrenthashmap<url, set<notifylistener>> ();     private final ConcurrentMap<URL, Map<String, List<URL>>>  notified = new ConcurrentHashMap<URL, Map<String, List<URL>>> ();     ...    }

The

Failbackregistry inherits from Abstractregistry and implements the failure retry mechanism.

public abstract class failbackregistry extends abstractregistry {//  Timer Task Actuator private final scheduledexecutorservice retryexecutor =  Executors.newscheduledthreadpool (1, new namedthreadfactory ("Dubboregistryfailedretrytimer",  true);//  failed retry timer, timed check for request failure, if any, infinite retry private final scheduledfuture<?> retryfuture; Private final set<url> failedregistered = new concurrenthashset<url> ();p rivate final set<url> failedunregistered = new concurrenthashset< Url> ();p rivate final concurrentmap<url, set<notifylistener>>  Failedsubscribed = new concurrenthashmap<url, set<notifylistener>> ();p rivate  final ConcurrentMap<URL, Set<NotifyListener>> failedUnsubscribed =  new concurrenthashmap<url, set<notifylistener&Gt;> ();p rivate final concurrentmap<url, map<notifylistener, list<url>> > failednotified = new concurrenthashmap<url, map<notifylistener, list <URL>>> ();..}


5. The registration process initiated by the service provider

The service provider writes its own URL address to the/dubbo/com.foo.barservice/providers directory when it starts. Next, find the code execution path. Registryprotocol as the core component of the registry, as an entry point to the code, make it clear that this is a registration process.

Entrance public class registryprotocol implements protocol {final registry  Registry = getregistry (Origininvoker);final url registedproviderurl =  Getregistedproviderurl (Origininvoker); Registry.register (Registedproviderurl);} public abstract class abstractregistry implements registry {     public void register (Url url)  {        if   (Url == null)  {             Throw new illegalargumentexception ("Register url == null");         }        if  (logger.isinfoenabled ()) {             logger.info ("Register: "  +  url);        }        registered.add (URL);     } ...} public abstract class failbackregistry extends abstractregistry {public  Void register (Url url)  {    super.register (URL);     Failedregistered.remove (URL);     failedunregistered.remove (URL);     try {        //  sending a registration request to the server          doregister (URL);    } catch  (exception e)  {     ...    }        //  Log failed registration requests to the failure list, timed retry         failedregistered.add (URL);     }    protected abstract void doregister (URL url);         protected abstracT void dounregister (Url url);        protected  Abstract void dosubscribe (Url url, notifylistener listener);         protected abstract void dounsubscribe (URL url, NotifyListener  listener);..}      public class zookeeperregistry extends failbackregistry  {     public zookeeperregistry (Url url, zookeepertransporter  zookeepertransporter)  {        super (URL);         //if the URL of provider is "0.0.0.0" or if a anyhost=true is thrown in the parameter, the registered address does not exist          if  (Url.isanyhost ())  {           throw new illegalstateexception ("Registry address == null");        }        //Service Group (default "Dubbo")          string group = url.getparameter (Constants.GROUP_KEY, DEFAULT _root);        if  (! group.startswith (Constants.PATH_ SEPARATOR))  {            group =  constants.path_separator + group;        }         //Service Group Root address         this.root =  Group;        zkclient = zookeepertransporter.connect (URL);         //Adding status Listeners          Zkclient.addstatelistener (New statelistener ()  {             public  void statechanged (int state)  {                if  (state == reconnected)  {                try {           recover ();       } catch  (Exception  e)  {          logger.error (E.getMessage (),  e);        }                }            }         });     }      protected  void doregister (Url url)  {             try {                //Connection Registry registration, creating nodes on ZK                zkclient.create ( Tourlpath (URL),  url.getparameter (constants.dynamic_key, true));             } catch  (throwable e)  {                 throw new rpcexception ("Failed  to register  " + url + "  to zookeeper  " + geturl ()  +  ", cause: "  + e.getmessage (),  e);             }        } }

6. Registration Center Relationship Class diagram

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/8B/FB/wKiom1hd--Pyk5anAADYmQZPcMI910.png "title=" 22.png "alt=" Wkiom1hd--pyk5anaadymqzpcmi910.png "/>

Key concepts:

Zookeeperregistry : Responsible for interacting with zookeeper

Registryprotocol : Gets the available services from the registry, or registers the service with the zookeeper, then provides the service or provides the calling agent.

registrydirectory : Maintains all available remote invoker or local invoker. This class implements the Notifylistner.

Notifylistener : Responsible for registrydirectory and zookeeperregistry communication.

Failbackregistry: Inherits from registry, implements the failure retry mechanism.

This article is from a "simple" blog, so be sure to keep this source http://dba10g.blog.51cto.com/764602/1885719

Dubbo (5) Service Registration Center

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.