Previously (How are services found automatically in Dubo-Go?) Three key problems in the transformation of Dubbo-Go from service-level discovery to application-level discovery have been mentioned. The solutions to these three problems can be summarized as follows: application-level discovery model, distinguishing application and service information (metadata), and association between metadata and application-level discovery model.

Application level service discovery model

After the transformation of the application-level discovery model, the registry data becomes the application as the dimension (Example 1). How to modify the Consumer terminal to use the modified data?

`"application1": [` ` {" name ":" instance1 ", "IP" : "127.0.0.1", "metadata" : {}}, ` ` {" name ":" instance2 ", "IP" : "127.0.0.2." "Metadata" : {}}, ` ` {" name ":" instanceN ", "IP" : "127.0.0.3", "metadata" : {}} ` ` `]Copy the code

Sample 1

Referenceconfig.refer () is the starting point for Consumer, which then builds an internal metadata center and subscribesto the registry to get a list of existing instances. But it is not easy to find the entry to subscribe to the registry.

As mentioned earlier, the Consumer of Dubbo-Go subscribes to the registry via the following URL by default. What about in the case of application-level service discovery? The Consumer gets the following URL (sample 2) and subscribes to the registry from that URL.

`registry://:? group=&metadata=default&name_mapping=in-memory&registry=service-discovery&registry.label=true&registry.preferred=false&r egistry.role=0&registry.timeout=5s&registry.ttl=10m&registry.weight=0&registry.zone=&service_discovery=zk-dis1&simplifie d=false`Copy the code

The sample 2

The Protocol in the URL is Registry, and the Protocol interface is the interface for parsing the Protocol. Are these two things related? Let’s start with the Protocol class diagram:

Figure 1

You can see from the Protocol class diagram (Figure 1) that RegistryProtocol is a class that handles the RegistryProtocol. The Refer method included is the method we need to find to subscribe to the registry. Next, drill down to the inside of the registryProtocol.refer:

`func (proto *registryProtocol) Refer(url *common.URL) protocol.Invoker {` `... ` `if registryUrl.Protocol == constant.REGISTRY_PROTOCOL {` `registryUrl.Protocol = registryUrl.GetParam(constant.REGISTRY_KEY, "")` `}` `... ` ` `}Copy the code

Sample 3

As you can see from the above code, if it is a Registry protocol, the Registry parameter in param is taken out and the protocol in the URL is replaced. The resulting agreement translates to the following URL (example 4) for application-level service discovery using the Service-Discovery protocol.

`service-discovery://:? group=&metadata=default&name_mapping=in-memory&registry=service-discovery&registry.label=true&registry.preferred=false&r egistry.role=0&registry.timeout=5s&registry.ttl=10m&registry.weight=0&registry.zone=&service_discovery=zk-dis1&simplifie d=false`Copy the code

The sample of 4

How do you know which registry to use once you’ve switched to application-level service discovery?

If you look closely at the URL above (Example 4), you can see that there is one parameter: service_Discovery =zk-dis1. If service_discovery=zk-dis1 has a corresponding configuration in the configuration file, the URL can be converted from the configuration item in the configuration file. In the configuration file, you can find the following configuration (Example 5).

` remote: ` ` zk1: ` ` address: "127.0.0.1:2181" ` ` timeout: "5 s" ` `... ` `service_discovery:` `zk-dis1:` `protocol: "zookeeper"` `remote_ref: "zk1"`Copy the code

The sample 5

However, in the configuration remote. Zk1 has already put the constructor into the cache when loading the configuration file. At this point, when you call reg = getRegistry(registryUrl) (see example 6), you get its constructor and use it for subsequent object initialization.

`var reg registry.Registry` `if regI, loaded := proto.registries.Load(registryUrl.Key()); ! Loaded {<<< reg = getRegistry(registryUrl) <<< gets the corresponding registry connection initialization method via protocol. ` `proto.registries.Store(registryUrl.Key(), reg)` `} else {` `reg = regI.(registry.Registry)` `}`Copy the code

The sample of 6

Dubbo-go eventually converts the URL (example 4) to registry information. After pulling the Provider information from the registry, match the Provider information. There is only application information, while Dubbo-Go is RPC based on method initiation, and the method is in the service (interface). Therefore, how to obtain additional information such as method name and method parameters contained in the original service-level discovery model becomes the key. Dubbo-go classifies this information as metadata, and the place where metadata information is stored is called a metadata center. Next, take a look at how metadata is implemented.

Welcome to the community

In the public account [minister technology road] background reply keyword [dubbogo] to join the community.