Dubbo Principle Analysis-Registration center implementation of simple registry based on Dubbo protocol

Source: Internet
Author: User
Tags failover

Based on the Dubbo protocol open source just gives the default one registry implementation Simpleregistryservice, it is just a simple implementation, does not support clustering, is the use of map<string/*ip:port*/, map<string/* service*/, url> to store the service address, specifically not verbose, please read the source code, can be used as a reference in the custom registration.

Registration Center Launch

Simpleregistryservice itself is also exposed as a Dubbo service.

<dubbo:protocolport= "9090"/>

<dubbo:service interface= "Com.alibaba.dubbo.registry.RegistryService" ref= "Registryservice" registry= "N/A" ondisconnect= "Disconnect" callbacks= ">"

<dubbo:methodname= "Subscribe" ><dubbo:argument index= "1" callback= "true"/></dubbo:method>

<dubbo:methodname= "Unsubscribe" ><dubbo:argument index= "1" callback= "false"/></dubbo:method>

</dubbo:service>

<bean id= "Registryservice" class= "Com.alibaba.dubbo.registry.simple.SimpleRegistryService"/>

Above is the Dubbo service configuration that exposes the registry,

Defines the port number of the Registry service

To publish the Registryservice service, the registry attribute is "N/a" to not get the registry, and the distribution of the Registry service is also a general release of the Dubbo service, if this property is not configured it will also look for the registry, to be published through the registry, Because oneself is the registration center, direct external release service, external through Ip:port direct use.

The Service release defines the callback interface, which defines the callback service exposed by the subscribe's second entry class for registry callbacks to be pushed back to the client when the registered service state changes.

Dubbo the Registration center of the protocol is exposed and the process process is the same as the normal Dubbo service, which may be different from most services when Simpleregistryservice is receiving a subscription request subscribe. The Notifylistener service exposed by the caller is also refer and automatically pushed when there is a change in the registration data

Producer Publishing Services

The Dubbo agreement publishes the service to the registry: When the service provider publishes the service to the Registry of the Dubbo Agreement, how to obtain, create the registry, how to register and subscribe to the service, we analyze its process.

Look at the following configuration Publishing service:

<dubbo:registry protocol= "Dubbo" address= "127.0.0.1:9090"/>

<beanid= "Demoservice" class= "Com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>

<dubbo:serviceinterface= "Com.alibaba.dubbo.demo.DemoService" ref= "Demoservice"/>

1. What kind of registry is specified, based on the Dubbo protocol, which specifies the address of the registry and the port number

2. Release the Demoservice service, the implementation of the service is Demoserviceimpl

Each <dubbo:service/> generates an Servicebean instance within spring that invokes the export method during the instantiation of the Servicebean to expose the service

1. Get the registry via Loadregistries Registryurls

registry://127.0.0.1:9090/com.alibaba.dubbo.registry.registryservice?application=demo-provider&dubbo=2.5.4 -snapshot&owner=william&pid=7084&registry=dubbo&timestamp=1415711791506

Represented by a unified data Model URL:

Protocol=registry represents a registry URL

Registration Center Address 127.0.0.1:9090

Call the Registry service Registryservice

The Registration Center agreement is Registry=dubbo

。。。。。。

2. Build the URL of the Publishing service

dubbo://192.168.0.102:20880/com.alibaba.dubbo.demo.demoservice?anyhost=true&application=demo-provider& Dubbo=2.5.4-snapshot&generic=false&interface=com.alibaba.dubbo.demo.demoservice&loadbalance= roundrobin&methods=sayhello&owner=william&pid=7084&side=provider&timestamp=1415712331601

Release Agreement Protocol =dubbo

The address of the service provider is 192.168.0.102:20880

The published service is Com.alibaba.dubbo.demo.DemoService

。。。。。。

3. Traverse the Registryurls to register the service with the registration center

Add the properties to each Registryurl key to Export,value for the above Publishing Service URL to get the following Registryurl

registry://127.0.0.1:9098/com.alibaba.dubbo.registry.registryservice?application=demo-provider&dubbo=2.5.4 -snapshot&export=dubbo%3a%2f%2f192.168.0.102%3a20880%2fcom.alibaba.dubbo.demo.demoservice%3fanyhost% 3dtrue%26application%3ddemo-provider%26dubbo%3d2.5.4-snapshot%26generic%3dfalse%26interface% 3dcom.alibaba.dubbo.demo.demoservice%26loadbalance%3droundrobin%26methods%3dsayhello%26owner%3dwilliam%26pid% 3d7084%26side%3dprovider%26timestamp%3d1415712331601&owner=william&pid=7084&registry=dubbo& timestamp=1415711791506

4. By the published service instance, the service interface and Registryurl as parameters, the Invoker object is obtained through the proxy factory proxyfactory, the Invoker object is the core model of Dubbo, and the other objects move closer to it or convert it to it.

5. Exposing service via Protocol object Protocol.export (Invoker)

Monitoring via Dubboprotocol exposure service (not in this section)

Publish the service address to the registry via Registryprotocol and subscribe to the service

Registryprotocol.export (Invoker) Exposure service

1. Monitoring of Dubboprotocol exposure service

2. Get the Registry getregistry (Invoker)

URL conversion, the URL obtained by Invoker is registryurl its protocol attribute is used to select which protocol instances such as Registryprotocol, Dubboprotocol, or Redisprotocol, and so on. This is the URL to choose which registry, so according to the Registry=dubbo property, the URL to reset the protocol properties to Registryurl

Dubbo://127.0.0.1:9098/com.alibaba.dubbo.registry.registryservice?application=demo-provider&dubbo =2.5.4-snapshot&export=dubbo%3a%2f%2f192.168.0.102%3a20880%2fcom.alibaba.dubbo.demo.demoservice% 3fanyhost%3dtrue%26application%3ddemo-provider%26dubbo%3d2.5.4-snapshot%26generic%3dfalse%26interface% 3dcom.alibaba.dubbo.demo.demoservice%26loadbalance%3droundrobin%26methods%3dsayhello%26owner%3dwilliam%26pid% 3d5040%26side%3dprovider%26timestamp%3d1415715706560&owner=william&pid=5040&timestamp= 1415715706529

Registryfactory.getregistry (URL) through the factory class to create a registry, registryfactory through the Dubbo SPI mechanism to obtain the corresponding factory class, here is the Dubbo protocol-based Registry, So it's dubboregistryfactory.

3. Get the publication URL is the value of the export parameter of Registryurl

registryproviderurl=dubbo://10.33.37.7:20880/com.alibaba.dubbo.demo.demoservice?anyhost=true&application= demo-provider&dubbo=2.5.4-snapshot&generic=false&interface=com.alibaba.dubbo.demo.demoservice& loadbalance=roundrobin&methods=sayhello&owner=william&pid=6976&side=provider&timestamp= 1415846958825

4. Dubboregistry.register (Registryproviderurl)

Registering a service with a registry via the Registrar

Note here that Registryproviderurl does not set the category attribute, when compared to the registry Urlutils.ismatch (Conuumerurl, Providerurl), The Providerurl Category property takes the default value providers,

This time the consumer subscribes to the category=providers of the URL of the subscription, to determine whether there is no registered provider.

5. Build a subscription service Overrideproviderurl, we are a publishing service

provider://10.33.37.7:20880/com.alibaba.dubbo.demo.demoservice?anyhost=true&application= demo-provider&category=configurators&check=false&dubbo=2.5.4-snapshot&generic=false& Interface=com.alibaba.dubbo.demo.demoservice&loadbalance=roundrobin&methods=sayhello&owner=william &pid=6432&side=provider&timestamp=1415847417663

6. Build Overridelistener It implements the callback with Notifylisener when the URL of the registry's subscription changes and re-export

7. Registry.subscribe (Overrideproviderurl, Overridelistener), the Registrar subscribes to the Overrideproviderurl registry and the override Listener is exposed as a callback service, and when the registry's Overrideproviderurl data changes,

The Registrar dubboregistry registry,subscribe, Unregistry, unsubscribe are similar and is a Dubbo remote service call


Dubboregistryfactory Creating a Registry procedure

1. Re-build based on incoming Registryurl

Remove Export_key Refer_key

To add a subscription callback parameter

dubbo://127.0.0.1:9098/com.alibaba.dubbo.registry.registryservice?application=demo-provider&callbacks= 10000&connect.timeout=10000&dubbo=2.5.4-snapshot&interface= Com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=register,subscribe,unregister, Unsubscribe,lookup&owner=william&pid=8492&reconnect=false&sticky=true& Subscribe.1.callback=true&timeout=10000&timestamp=1415783872554&unsubscribe.1.callback=false

2. Build the registered directory object registrydircectory according to the URL Registration service interface, implement the Notiyflisener, Here Notiyflisener implementation is mainly based on the URLs to refer reference remote services Registryservice get corresponding invoker, when the URLs change refer; Directory service can list all the invoker that can be executed

3. Using the cluster join method, dirctory multiple Invoker objects are disguised as a Invoker object, where the default cluster policy gets Failoverclusterinvoker

4. Failoverclusterinvoker using Proxyfactory to obtain proxy objects to the Registryservice service

5. The Registry Registrar for the Dubbo Protocol is built by the proxy object and Failoverclusterinvoker of the Registryservice service Dubboregistry

6. Registrydircectory Set the Registrar dubboregistry, set the Dubbo protocol

7. Call the Registrydircectory notify (URLs) method

Mainly based on Registryurls, referencing the Registryservice service implementations of each registry, the referenced service is cached Key=menthodname/value=invoker, directory service Directory.list ( invocation) Lists all invoker of the called method, and a invoker represents the calling entity for a registry.

8. Subscribe to the Registry service, the provider of the service Registryservice the service of the registration center belongs to the consumer, so the agreement to subscribe to the service's URL is consumer

Consumer://192.168.0.102/com.alibaba.dubbo.registry.registryservice?application=demo-provider& Callbacks=10000&connect.timeout=10000&dubbo=2.5.4-snapshot&interface= Com.alibaba.dubbo.registry.registryservice&lazy=true&methods=register,subscribe,unregister,unsubscribe , Lookup&owner=william&pid=6960&reconnect=false&sticky=true&subscribe.1.callback=true &timeout=10000&timestamp=1415800789364&Unsubscribe.1.callback=false

The purpose of the subscription is to reverse-push the subscriber when the data in the registry changes

Directory.subscribe (URL) eventually calls the Regsryservice remote service of the registry, which is a normal Dubbo remote call. The difference between remote calls with most Dubbo: the URL parameter subscribe.1.callback=true It means that the second parameter of the Subscribe method of the Registryservice is exposed as a callback service Notifylistener the parameters of the URL unsubscribe.1.callback=false This means that the second parameter of the Registryservice unsubscribe method Notifylistener The exposed callback service is destroyed.

Here the Dubbo Protocol Registration Center Registry Service adopted the default cluster call policy is failover, select a registry, only when the failure to retry the other server, the registry implementation is relatively simple does not have the cluster function, If you want an initial cluster function you can choose to broadcastcluster it at least to each registry traversal call Register again

Consumer Referral Services

< Dubbo:registry protocol="Dubbo" address="127.0.0.1:9098" />

< dubbo:reference id="Demoservice"interface="COM.ALIBABA.D ubbo.demo.DemoService" />

1. What kind of registry is specified, based on the Dubbo protocol, which specifies the address of the registry and the port number

2. Referencing the remote Demoservice service

Each <dubbo:reference/> tag spring will generate a referenc Ebean when loaded.


If the Referencebean implements the spring Factorybean interface, the bean that implements this interface passes the spring's Beanfactory.getbean ("Beanname") The object obtained is not the configured bean itself but the object returned through the Factorybean.getobject () method, which is widely used within spring to obtain proxy objects and so on. Here the Getobjec T method is used to generate the proxy for the remote service invocation

1. Loadregistries () Get the Registryurls of the configured registry

2. Traverse the Registryurls collection and give Registryurl plus refer key is the remote service to reference

[Registry://127.0.0.1:9098/com.alibaba.dubbo.registry.registryservice?application=demo-consumer& dubbo=2.0.0&pid=2484&refer=application%3ddemo-consumer%26dubbo%3d2.0.0%26interface% 3dcom.alibaba.dubbo.demo.demoservice%26methods%3dsayhello%26pid%3d2484%26side%3dconsumer%26timestamp% 3D1415879965901&REGISTRY=DUBBO&TIMESTAMP=1415879990670]

3. Traverse the Registryurls collection, using Protocol.refer (Interface,regist ryurl) to the executable object Invoker

4. If there are multiple registries, the cluster policy cluser.join () disguises multiple invoke R as an executable invoker, where available policy is used by default

5. Using the proxy factory to generate proxy objects proxyfactory.getproxy (invoker)

Protocol.refer Process Flow

1. According to the incoming registryurl is used to select Registryprocol its Protocol property is registry, the following to choose which kind of registry to use, so you want to reset according to the Registry_key property Registrurl

Dubbo://127.0.0.1:9098/com.alibaba.dubbo.registry.registryservice?application=demo-consumer&dubbo =2.0.0&pid=4524&refer=application%3ddemo-consumer%26dubbo%3d2.0.0%26interface% 3dcom.alibaba.dubbo.demo.demoservice%26methods%3dsayhello%26pid%3d4524%26side%3dconsumer%26timestamp% 3d1415881461048&timestamp=1415881461113

2. Use Registryfactory to obtain the Registrar according to Registrurl (the process is the same as the exposed service), here is the Dubbo protocol gets the registrar is dubboregistry

Reference and subscribe to the Registry service,

3. Building the Subscribeurl of the reference service

consumer://10.5.24.221/com.alibaba.dubbo.demo.DemoService?application=demo-consumer& Category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService& methods=sayhello&pid=8536&side=consumer&timestamp=1415945205031

and register the consumer through the Registrar, the main category is consume RS

4. Build directory service Registrydirectory,

Build a subscription to the consumer subscription URL, where the main category=providers go to the registry to find registered service providers

consumer://10.33.37.4/com.alibaba.dubbo.demo.demoservice?application=demo-consumer&Category=providers, Configurators,routers&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.demoservice&methods= sayhello&pid=9692&side=consumer&timestamp=1415967547508

Subscribing to the registry to the consumer, the registry finds a matching service provider URL based on the URL that the consumer has passed in (note: The service provider does not have a category, the registry is not set for default fetch providers value ) dubbo://10.33.37.4:20880/com.alibaba.dubbo.demo.demoservice?anyhost=true&application= demo-provider&dubbo=2.5.4-snapshot&generic=false&interface=com.alibaba.dubbo.demo.demoservice& loadbalance=roundrobin&methods=sayhello&owner=william&pid=9828&side=provider&timestamp= 1415968955329

Then the registry callback service consumer exposes the callback interface to reference the service provider's service refer generate the corresponding executable object invoker. The service provider establishes a connection with the consumption of the service,

5. Returns the executable object by merging invokers in directory with cluster invoker

6. Proxyfactory.getproxy (invoker) creates a proxy object to return to the business party using

Here the Dubbo Protocol Registration Center Registry Service adopted the default cluster call policy is failover, select a registry, only when the failure to retry the other server, the registry implementation is relatively simple does not have the cluster function, If you want an initial cluster function you can choose to broadcastcluster it at least to each registry traversal call Register again

Dubbo Principle Analysis-Registration center implementation of simple registry based on Dubbo protocol

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.