preface

In the previous article, ServiceConfig processed the URL information and handed it over to DubboProtocol for service exposure, turned on the Netty server, and then saw the ServiceConfig code end. So how do you write services to the registry?

Invoker<? > invoker = proxyFactory.getInvoker(ref, (Class) interfaceClass, registryURL.addParameterAndEncoded(Constants.EXPORT_KEY, url.toFullString())); DelegateProviderMetaDataInvoker wrapperInvoker =new DelegateProviderMetaDataInvoker(invoker, this); Exporter<? > exporter = protocol.export(wrapperInvoker);Copy the code

ServiceConfig has one such code, the core export code, but it starts with:

registryURL.addParameterAndEncoded(Constants.EXPORT_KEY, url.toFullString())
Copy the code

This code has changed the URL to something like this:

The difference is that the url before the modification starts with dubbo. According to spi logic, the class DubboProtocol will be called, but after modification, it starts with Registry. In fact, the class called is RegistryProtocol

The body of the

Zookeeper registration mechanism

Zookeeper is a tree-structured registry, which you can think of as a directory structure. There are four types of nodes:

  • Persistent node: ensures that the node is not lost and still exists after the restart
  • Persistent order node: Order function is added to persistence
  • Temporary node: Removed after connection loss and session timeout
  • Temporary order nodes: explained as above

The tree structure and values of dubbo registered in Zookeeper are as follows:

—-dubbo

———service

— — — — — — — — — — — — — — — – will: dubbo: / / IP. Port/interface path? key=..

— — — — — — — — — — — — — — — – consumers: consumer: / / IP. Port/interface path Key =..

— — — — — — — — — — — — — — — – routers: condition…

— — — — — — — — — — — — — — — – configurators: override…

The main function of the ++ registry is to subscribe to publishing, and when a new service is published, other nodes can automatically follow the new ++

The registration process

There are three implementation classes under the RPC package

Two of these are Wrapper classes, which are called by default, and as DESCRIBED earlier in SPI, Wrapper classes are wrapped in order to make chain calls

As you can see, any protocol that is Registry will enter the RegistryProtocol

Enter the export method of the RegistryProtocol

The first is doLocalExport

The getCacheKey method parses the URL as shown below, using the protocol dubbo, and then checks the cache

So the real local exposure is to enter the doLocalExport method and then call the DubboProtocol to expose and start the Netty server as described in the previous article.

URL registryUrl = getRegistryUrl(originInvoker);
Copy the code

The next step is to get the regitryUrl, which contains the RegistryService internal information:

Zookeeper: / / 127.0.0.1:2181 / com. Alibaba. Dubbo. Registry. RegistryService? Application = demo - provider&dubbo = 2.0.2 & export = dubbo % 3 a % 2 f % 2 f192 168.168.115%3 a20880%2 fcom. Alibaba. Dubbo. Demo. DemoService % 3 fanyhost % 3 dtrue % 26 application % 3 ddemo - provider % 26 bean. The name % 3 dcom. The alibaba. Dubbo. Demo. DemoService % 26 bind. The IP % 3 d192. 168.168. 115% 26 bind. Port % 3 d20880%26 dubbo % 3 d2. The 0.2% % 26 generic 3 dfalse % 26 interface % 3 dcom. The alibaba. Dubbo. Demo. DemoService % 26 the methods % 3 ds ayHello%26pid%3D90732%26qos.port%3D22222%26side%3Dprovider%26timestamp%3D1608539235916&pid=90732&qos.port=22222&timestam p=1608539235882Copy the code

Then use this URL to get the REGPOP instance

 final Registry registry = getRegistry(originInvoker);
Copy the code

RegistryFactory is also an SPI interface, and ZookeeperRegistryFactory is used here

Internal ZookeeperTransporter is also an SPI method. You can choose to use either Zookeeper curator or ZkClient, the default curator, which encapsulates zooKeeper operation methods.

After obtaining an instance of Registry and parsing the Registry URL, you can connect to the Registry through the URL and manipulate the API

Zkclient registration method

A path is created, and the value is the URL

Then subscribe to the URL

Create a new OverrideListener and call subscribe

This is a full subscription service, meaning all categories are subscribed

This is subscribing to only one category: providers, consumers, etc

When a new service is updated, the notify method is returned and the URL is changed

This is an interface diagram for the registry. AbstractRegistry internally caches a list of services that can be invoked by the registry in exchange for space and time. The FailbackRegistry adds a retry function, internally maintains sets that fail to initiate registration, fail to unregister, fail to initiate subscription, and fail to unsubscribe, wrapping interface methods. The real implementation is a design pattern that uses template methods, all implemented by subclasses and only called by the parent class.

example

The parent class calls methods starting with Do, and the subclass is responsible for implementing them


The final process is:

  1. The ServiceConfig encapsulates the URL as the Registry protocol and presents it to the RegistryProtocol
  2. RegistryProtocol calls doLocalExport, using a custom protocol (default Dubbo), to make a local call
  3. Resolves the registry address and gets the action class based on the configured Client (default curator)
  4. Register the URL with ZooKeeper (create a directory), subscribe, and call notify locally if the registry changes

As you can see, the registry code is relatively simple. If multiple registries are configured, a circular registration is performed in ServiceConfig. The registration process does not involve clustering.