Dubbo use understanding and ideas

Posted May 26, 20207 min read

Dubbo

dubbo.jpg

On the second day of the holiday, I sorted out some of Dubbo's understandings. For everyone, Dubbo is a distributed, high-availability, load-balanced remote calling framework. For narrow projects in the past, the above functions I have not used one, it is a complete stand-alone deployment, so the following can only be regarded as a personal understanding, after all, it is not actually used in production.


Introducing Dubbo

There are many introductions on Dubbo online, not much introduction here, just need to know that it is a distributed RPC framework, developed by Alibaba and has been open source, [Dubbo official address]( http://dubbo.apache.org/en-us /), Github address .

For Dubbo's RPC process, here is an official call flow chart.

architecture.png

The above picture can basically see the calling process of Dubbo, but the 5th point count Monitor should be added in the latest Dubbo 2.7.x version, the previous 2.5.x version did not monitor this piece of logic.


Use Dubbo

Dubbo is also more convenient to use, the version can use Spring, SpringBoot, SpringCloud-Alibaba.

Dubbo official is the recommended Spring. This way, in my opinion, there are too many xml files. Of course, there are many convenient places. For example, directly changing the xml file is much more reliable than changing the code.

And SpringBoot Dubbo , there are not too many annotations to use, the code is very clean, and it is also very powerful to cooperate with Springboot to start the Dubbo service. Describe it with the phrase "It's hard to be too cool".

SpringCloud-Alibaba, as an alternative version of SpringCloud-Nextflix strongly promoted by Ali, can only say:Well, yes!

The following is explained with the Spring version

Register

Dubbo's calling process is as shown in the previous figure. First, you need to prepare a registration center. The official recommendation is Zookeeper. Personally think that Zookeeper is very good, but you can also understand the following Nacos. Of course, there is no registration center. You can first register and call locally, just need Change the address of the registration center to N/A.

Here is a description of Zookeeper. Zookeeper uses nodes to register and consume services, much like the binary tree algorithm. Of course, many frameworks and databases use binary trees.

When we use Zookeeper, we can also use the characteristics of its nodes to implement the lock, register a specific unique key to the node, and determine whether it is locked according to the existence of the node. The problem of distributed locks will not be too much. The big problem is that you only need to configure Zookeeper in a cluster to monitor each other's heartbeats.

Provider

Provider is basically registered to Zookeeper in the form of an interface. If you have seen the nodes under Zookeeper, you will find that the registration path is dubbo://com.yanzhenyidai.squid.example.SquidService. First, dubbo Is the protocol name, other is java
The package path in the project.

dubbo-provider.png

Run sh zkCli.sh in Zookeeper's bin directory to enter Zookeeper's local client, you can intuitively see the dubbo registration node.

Consumer

The design of Consumer and Provider is similar. The bottom layer of the call is implemented through Netty. First, obtain the node information registered by Zookeeper, obtain the open port, and directly request to send the data.

Other functions

General call

The significance of the generalized call is that, for example, you are using the Java language, and the caller of the other party uses GO. The party of GO cannot write interfaces to call according to the Java specification. Dubbo's design is still very forward-looking.

Just set generic in<dubbo:reference>to true, the interface is declared as a generalized interface, and then use GenericService to invoke, similar code is as follows:

<? xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
      xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
      xmlns:dubbo = "http://code.alibabatech.com/schema/dubbo"
      xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">

   <dubbo:application name = "demotest-consumer" owner = "programmer" organization = "dubbox" />
   <!-Subscribe to zookeeper's provider's address, which is pushed regularly by zookeeper->
   <dubbo:registry address = "zookeeper://localhost:2181" />
   <!-Using dubbo protocol to call the defined api.PermissionService interface->
   <dubbo:reference id = "permissionService" interface = "com.alibaba.dubbo.demo.DemoService" generic = "true" />
</beans>

package com.alibaba.dubbo.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.rpc.service.GenericService;

public class Consumer {
   public static void main(String []args) {
       /////////////////Spring generalization call //////////
       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
       context.start();
       System.out.println("consumer start");
       GenericService demoService =(GenericService) context.getBean("permissionService");
       System.out.println("consumer");
       Object result = demoService. $Invoke("getPermissions", new String []{"java.lang.Long"}, new Object []{1L});
       System.out.println(result);
   }
}

It is added that although the generalization seems to be very troublesome and does not come quickly with the Rest interface, it is useful to design it, and it is really indispensable.

Load balancing

Dubbo's load balancing does not have a special LoadBanlance like Spring Cloud Robbin. Students who have seen Dubbo-Admin should know that the services inside have a concept called weight, and Dubbo's load balancing is based on weight Calculation.

public class RandomLoadBalance extends AbstractLoadBalance {
   public static final String NAME = "random";

   public RandomLoadBalance() {
   }

   protected <T> Invoker <T> doSelect(List <Invoker <T >> invokers, URL url, Invocation invocation) {
       int length = invokers.size();
       boolean sameWeight = true;
       int []weights = new int [length];
       int firstWeight = this.getWeight((Invoker) invokers.get(0), invocation);
       weights [0]= firstWeight;
       int totalWeight = firstWeight;

       int offset;
       int i;
       for(offset = 1; offset <length; ++ offset) {
           i = this.getWeight((Invoker) invokers.get(offset), invocation);
           weights [offset]= i;
           totalWeight + = i;
           if(sameWeight && i! = firstWeight) {
               sameWeight = false;
           }
       }

       if(totalWeight> 0 &&! sameWeight) {
           offset = ThreadLocalRandom.current(). nextInt(totalWeight);

           for(i = 0; i <length; ++ i) {
               offset-= weights [i];
               if(offset <0) {
                   return(Invoker) invokers.get(i);
               }
           }
       }

       return(Invoker) invokers.get(ThreadLocalRandom.current(). nextInt(length));
   }
}

It can be seen that Dubbo's algorithm first obtains the total weight of all services, and then assigns a percentage according to the weight of each service. If the weights are all the same, random calls are made directly.

SPI

SPI is a powerful extension designed in java. You can create a file under META-INF/services. Generally, the file name is service name, and the specified implementation class is loaded in the file, similar to the scenario where the notebook can be connected to the keyboard. .

For Dubbo's SPI, the official description redefines a set of rules. In the new dubbo version, in the ExtensionLoader class, the loadResource method, the source code is indeed the logic of using IOC. It should be noted that it is different from java SPI , In the dubbo SPI file
The content appears as key-value.

PojoUtil

Finally, write about dubbo's serialization mechanism. In the previous 2.5.x version, it was miserable. Most companies with large request transmission volume have performed serialization replacement. In PojoUtil, dubbo realizes the conversion between Map and Object. If byte bytes are transferred, the ploy capacity can only be increased in <dubbo:protocol>.


Deploy Dubbo

The way to deploy Dubbo, divided by service, is very convenient to deploy, start the service separately, and finally start the front-end function.

If you are doing distributed, because you have not had similar experience before, but slowly study by yourself, come to the following way:

It is best to monitor Zookeeper with each other, and it is best to have three or more odd-numbered forms. Everyone knows that Zookeeper's election mechanism is calculated in the singular form. Zookeeper listens to each other, and the URL address registered by Dubbo service should be added to all Zookeeper addresses separately, with ;
The end, for example, 192.168.10.1:2181; 192.168.10.2:2181, so that after successful registration, you can see on Dubbo-Admin that the service is registered on each Zookeeper separately.

For the load balancing mechanism mentioned above, this place can determine the server configuration to configure the weight. You can use one high-configuration server and two low-configuration servers. Most services can be called on the high-configuration server, and some are placed in the low-configuration server. On the server.

Note that for distributed deployments, log files must be divided according to the maximum size of each file every day. For example, each file can only store up to 20MB, so it is also very convenient to find logs.


Pit encountered in Dubbo

There are too many pits encountered in Dubbo, which can't be recalled for a while. The general existence is as follows:

  • The service is repeatedly called, the solution, and the retry mechanism is mostly placed in the provider configuration, and the consumer must be configured to 0
  • IPv4 and IPv6, using java parameter configuration, only supports IPv4 startup, after all, IPv6 is not yet popular.

to sum up

To sum up, Dubbo is really the most used distributed framework, and the most written content is the most, but because Dubbo uses too much, and SpringCloud is much less difficult than Dubbo, most of the projects are now transferred to Above SpringCloud, there will be more SpringCloud related later
Using the experience articles, you can really read more official documents in Dubbo's documents. The very mature document structure can help students who use Dubbo to locate service problems faster. The main reason is that there are many Chinese documents.