Write it at the front

Before , A lot of friends sent me private letters : How to quickly master Dubbo The core principle and source code of . therefore , I wrote an article 《 How can I quickly grasp Dubbo The principle and source code of ( Dry only )?》. about Dubbo Source code analysis series article , I'm also thinking about how to make the article of source code analysis easier to understand , therefore , I adjusted my writing Dubbo Source code analysis article strategy , Try to make it easier for your friends 、 Easy to understand way to master thoroughly Dubbo Source code . today , Let's talk about Dubbo How to realize the unified contract in .

The article has been included in :

https://github.com/sunshinelyz/technology-binghe

https://gitee.com/binghe001/technology-binghe

I have to say URL

URL The full name is the uniform resource locator , It can locate a unique network address in the Internet .URL The format of is as follows .

protocol://username:password@host:port/path?key=value&key=value

among , A brief description of each part is as follows .

  • protocol:URL The agreement . The most common protocol is HTTP and HTTPS, Others are FTP、WS、FILE、SMTP etc. .
  • username: user name .
  • password: password .
  • host: host , It's usually a domain name or IP Address .
  • port: Host port number .
  • path: The path of the requested target file .
  • parameters: Request specific parameter information , Here for key=value&key=value.

This is what we see on the Internet URL Brief description .

that , stay Dubbo Inside , A large number of methods receive parameters in the form of URL Encapsulated , that ,URL stay Dubbo What role does the interior play ? Let's move on .

Dubbo Medium URL

in general , stay Dubbo Inside , Service providers Provider Will encapsulate their own information into URL Sign up to Zookeeper Or other registries , So as to expose the services they provide . And service consumers Consumer Also through URL In the form of Zookeeper Or other registries subscribe to the services they want to call . And in the Dubbo Of SPI In the implementation ,URL And will participate in the logic processing of extension implementation . So ,URL stay Dubbo Is very important in the implementation of . It can also be said that ,Dubbo Medium URL Namely Dubbo The unified contract of .

So let's see Dubbo Medium URL What does it look like , By debugging Dubbo Bring their own Provider Sample source code , We can see in the Dubbo Medium URL As shown below .

dubbo://192.168.175.1:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=dubbo-demo-annotation-provider&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello,sayHelloAsync&pid=15012&release=&side=provider&timestamp=1610857629484

This is also Provider Sign up to Zookeeper Or other registry information . The description of each part is as follows .

  • dubbo: It uses dubbo agreement .
  • host: The host IP The address is 192.168.175.1.
  • port: The port number is 20880.
  • path: The request path here is :org.apache.dubbo.demo.DemoService
  • parameters: Requested parameter information , Here for :anyhost=true&application=dubbo-demo-annotation-provider&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello,sayHelloAsync&pid=15012&release=&side=provider&timestamp=1610857629484.

since Dubbo Is to Zookeeper Or other registries to register this information , that Dubbo How does the interior respond to URL It's packaged .

stay dubbo-common Module , There is one URL Class is designed to encapsulate URL, As shown below .

stay URL Class , Let's look at a core constructor , As shown below .

public URL(String protocol,
String username,
String password,
String host,
int port,
String path,
Map<String, String> parameters,
Map<String, Map<String, String>> methodParameters) {
if (StringUtils.isEmpty(username)
&& StringUtils.isNotEmpty(password)) {
throw new IllegalArgumentException("Invalid url, password without username!");
}
this.protocol = protocol;
this.username = username;
this.password = password;
this.host = host;
this.port = Math.max(port, 0);
this.address = getAddress(this.host, this.port); // trim the beginning "/"
while (path != null && path.startsWith("/")) {
path = path.substring(1);
}
this.path = path;
if (parameters == null) {
parameters = new HashMap<>();
} else {
parameters = new HashMap<>(parameters);
}
this.parameters = Collections.unmodifiableMap(parameters);
this.methodParameters = Collections.unmodifiableMap(methodParameters);
}

You can see ,Dubbo about URL The core encapsulation of , It's basically the same as in the Internet URL Encapsulation is consistent .

stay Dubbo Of dubbo-common The module provides processing URL Tool class of :URLBuilder and URLStrParser. As shown below .

The implementation of these two classes is relatively simple , Friends can read by themselves Dubbo Source code .

Next , Let's see Dubbo Inside ,URL How to realize the unified contract ?

Dubbo in URL Practical application of

here , We mainly talk about it from three aspects URL stay Dubbo Internal applications :

  • URL stay SPI Application in .
  • URL Applications in service registration .
  • URL Applications in service discovery .

URL stay SPI Application in

I know a little about Dubbo Everyone knows that ,Dubbo With a high degree of scalability , And this scalability is based on Dubbo Self SPI To achieve . stay Dubbo Realized SPI in ,URL And played a very important role .

stay Dubbo SPI In the implementation of , A typical scene is being @Adaptive Annotation decorated interface method , for example , stay dubbo-registry-api Module RegistryFactory Interface getRegistry() The method is @Adaptive({"protocol"}) To modify . As shown below .

explain RegistryFactory Interface getRegistry() Method is an adapter method ,Dubbo In the process of running , Would be getRegistry() Methods are generated dynamically RegistryFactory$Adaptive type . for example , Generated RegistryFactory$Adaptive The types are as follows .

public class RegistryFactory$Adaptive
implements RegistryFactory {
public Registry getRegistry(org.apache.dubbo.common.URL arg0) {
if (arg0 == null) throw new IllegalArgumentException("");
org.apache.dubbo.common.URL url = arg0;
String extName = (url.getProtocol() == null ? "dubbo" : url.getProtocol());
if (extName == null)
throw new IllegalStateException("");
RegistryFactory extension = (RegistryFactory) ExtensionLoader.getExtensionLoader(RegistryFactory.class).getExtension(extName);
return extension.getRegistry(arg0);
}
}

This code is relatively easy to understand , Generated RegistryFactory$Adaptive It's automatic getRegistry() Method , stay getRegistry() In the method , Will get URL Medium protocol Parameters to determine URL The agreement , If you get protocol It's empty , Use the default dubbo agreement , With this Agreement , I can get through SPI Load specific extension implementation classes dynamically .

We are Dubbo Of dubbo-registry-api Found in module RegistryProtocol class , As shown below .

Find one getRegistry() Method and hit the breakpoint , As shown below .

Next ,debug start-up Dubbo Of Provider Example , As shown below .

You can see , Used at this time protocol Agreement for zookeeper. of Dubbo in SPI The implementation of the , We will analyze it in detail later , today , Let's have a general understanding .

URL Applications in service registration

stay Dubbo The implementation of service registration in ,URL Also played a very important role . here , The registry I use is Zookeeper, therefore , We are dubbo-registry-zookeeper Found in module ZookeeperRegistry class , As shown below .

Find one doRegister() Method , Set a breakpoint , As shown below .

debug start-up Dubbo Self contained provider Example , As shown below .

You can see , Register to Zookeeper Medium URL in , Contains protocol agreement 、host Host name 、port Port number 、path Request path ,parameters Parameter information .

URL Applications in service discovery

Dubbo Consumers of services in the market Consumer When it starts , Will send to Zookeeper The registry subscribes to the services it needs to call , That's how to get through URL What about the subscription ? We are also in dubbo-registry-zookeeper Module ZookeeperRegistry Find in class doSubscribe() Method . stay doSubscribe() Method , As shown below .

start-up Dubbo Self contained Consumer Example , As shown below .

We can see ,Dubbo Of Consumer Will send to Zookeeper Pass in the following parameters to subscribe to the service .

consumer://192.168.175.1/org.apache.dubbo.demo.DemoService?application=dubbo-demo-annotation-consumer&category=providers,configurators,routers&dubbo=2.0.2&init=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello,sayHelloAsync&pid=15184&side=consumer&sticky=false&timestamp=1610860963037

Among them protocol by consumer, Represents a subscription agreement .category Represents the category to subscribe to , Here is providers,configurators,routers Three categories .interface Represents the interface service to subscribe to , Here is org.apache.dubbo.demo.DemoService.methods Represents the method to subscribe to , Here is sayHello,sayHelloAsync.

Another thing to note is : In the process of service registration ,Dubbo Will URL Turn into Zookeeper Path to register information to Zookeeper in ; In the process of service discovery ,Dubbo Will URL Turn into Zookeeper route , To monitor Zookeeper Change the directory to subscribe to related services .

All in all , stay Dubbo Through internal URL The realization of a unified contract . Have you learned ?

Recommended reading

Okay , That's all for today , I'm glacier , If you have any questions, please leave a message below , I can also add wechat :sun_shine_lyz, Exchange technology together , Step up together , Let's fight together ~~

Dubbo How to realize the unified contract in ? More articles about

  1. Dubbo( 5、 ... and ):Dubbo Medium URL Unified resource model and Dubbo agreement

    One .URL brief introduction URL That is to say Uniform Resource Locator, Chinese is called uniform resource locator .Dubbo Whether it's service consumers , Or service providers , Or the registry . It's all through URL Positioning resources . So come today ...

  2. Service oriented transformation practice | How to be in Dubbo Chinese support REST

    What is? REST REST yes Roy Thomas Fielding [[1]](#fn1) stay 2000 His doctoral thesis in 1986 [[2]](#fn2) “ Architecture style and web-based software architecture design ” The one mentioned in ...

  3. Dubbo Chinese vs Spring Configure label extension

    Spring Provides scalability Schema Support for , Completing a custom configuration generally requires the following steps : Design configuration properties and JavaBean To write XSD file To write NamespaceHandler and BeanDefinitionPa ...

  4. Architect's way - stay Dubbo In the development REST Style remote call

    Architect's way : From scratch to build a small and medium-sized Internet company background service architecture and operation and maintenance architecture http://www.roncoo.com/course/view/ae1dbb70496349d3a8899b6c68f7d10b General ...

  5. Dubbo The analysis of encoding and decoding in

    ( The analysis here is not very detailed , Wait until you go through the whole process )Dubbo The work of codec in is done by Codec2 Interface to handle , Think back to the first contact with Codec2 The related content is when the service is exposed on the server side , Expose services according to specific agreements ...

  6. Dubbo Process analysis of exposing services in

    dubbo There are two cases of exposure services , One is to set delayed exposure ( such as delay="5000"), The other is that delay exposure is not set or delay is set to -1(delay="-1"): Set up ...

  7. 【Rest】 stay Dubbo In the development REST Style remote call (RESTful Remoting)

    Catalog summary REST The advantages of Application scenarios Quick start standard Java REST API:JAX-RS brief introduction REST Details of service provider HTTP POST/GET The implementation of the Annotation In the interface class or implementation class J ...

  8. stay Dubbo In the development REST Style remote call (RESTful Remoting)

    rest stay Dubbo In the development REST Style remote call (RESTful Remoting)

  9. Dubbo Subscription and notification resolution in

    Dubbo Subscription and notification of services in mainly occurs in the process of exposing services by service providers and referencing services by service consumers during initialization . 2345678910111213141516171819 public <T> ...

  10. Dubbo in Directory analysis

    Directory For more than one Invoker, You could view it as List Directory Interface Directory Interface inherited Node Interface : 1234567 public interface Directory ...

Random recommendation

  1. Druid Use ConfigFilter

    1.  encryption , Use the following command to encrypt the user name and password cmd Command line execution  java -cp D:/druid-1.0.13.jar com.alibaba.druid.filter.config.ConfigToo ...

  2. iOS Create push Certificate

    1. First of all, you want to create a push certificate just like you did before , We need to prepare a 99$ My paid account number . And then go to the Apple Developer website .http://developer.apple.com/ 2. After logging in, you can see the interface and select ...

  3. Java take List/JavaBean Turn into Json

    import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.util.List; /**  *  ...

  4. Angular.js And Router Learning notes

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. stammer (jieba) Chinese word segmentation and its application

    Unlike English text classification, Chinese text classification only needs to separate words one by one , Chinese text classification needs to separate the words composed of words to form a vector . therefore , Need participle . Here we use the popular online open source word segmentation tool, stuttering word segmentation (jieba), It works ...

  6. python flask frame Use of database

    #coding:utf8 from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) # ...

  7. SpringBoot introduction ( 7、 ... and ) Redis Access operations

    This paper records the learning in SpringBoot Use in Redis. One What is? Redis Redis It's a very fast non relational database (Non-Relational Database), It can store keys (Key) And Varied ...

  8. DIV Several ways of centering

    1. body{ text-align:center; } shortcoming :body All the contents in the center 2. .center{ position: fixed; left: 50%; } shortcoming : Need to set up posi ...

  9. javaSE—— Simple file browser

    import java.io.File; public class Demo02 { public static void main(String[]args){ File file = new Fi ...

  10. Cocos2d-x Lua in Sprite ccsprite

    Elves are Sprite, Its class diagram is shown in the following figure . Sprite Class diagram Sprite Class directly inherits Node class , have Node basic feature . Besides , We can still see that Sprite Subclasses of class are :PhysicsSprite and Skin ...