3、 Source code analysis of Dubbo cluster fault tolerance

Time:2021-1-10

We’ve seen Dubbo’s earlierserviceIntroduction andService exportSource code, let me take a look at the DubboCluster fault toleranceSource code of

1、 Relationship between related components

Description of cluster fault tolerance in Dubbo

Fail over cluster – failure. When the automatic switch fails, other servers will be tried again
Failfast Cluster – after the request fails, an exception will be returned quickly without retrying
Failsafe Cluster – if there is an exception in failure security, the request will be load balanced if it is ignored directly
Failback Cluster – after the failure of the automatic recovery request, the request will be automatically recorded to the failure queue
Forcing Cluster – calls multiple service providers in parallel, one of which returns the result immediately
2、 Source code analysis of related components
1. Cluster and its default implementation class
 

 

 

 

 

You can see that dojoin is a template method

 

 

It’s actually very simple here, just creating a clusterinvoker

 

 

 

 2、Invoker

 

 

 

 

 

 

Here we can see that invoke mainly obtains the invokerlist and load balancer from the directory, and delegates the call to the subclass

Now let’s take another look org.apache.dubbo . rpc.cluster.support The concrete implementation of. Failoverclusterinvoker ා doinvoke

 

 

From the picture above, we can see that there are several main things to do here

1. Gets the number of retries by method name

2. Traverse the number of retries and do load balancing

3. Initiate the call

 

 

 

3、LoadBalance

 

 

 

 

 

Here is another template method. Let’s take a look at its default implementation class org.apache.dubbo . rpc.cluster.loadbalance Doselect of. Randomloadbalance

 

 

The implementation idea is whether the weights of all invockers are the same. If they are the same, one invocker will be selected randomly

Otherwise, add up the total weight of all the invockers, and then generate a random number in [0, total] to find the first one that matches the random number

4. Execution logic of invoker

org.apache.dubbo.rpc.protocol.AbstractInvoker#invoke

 

 

 

 

 

It is mainly used here org.apache.dubbo . remoting.exchange.ExchangeClient Send request

 

 

 

 

 

 

 

 

 

 

 

It will eventually be handed over to you org.apache.dubbo . remoting.exchange.support . header.HeaderExchangeChannel#request implement

 

 

The channle object here is created through the SPI transporter, and the netty protocol is created here
5. Information cache component directory
It is mainly used to cache the list of providers that can be called at present. When consumers call, we will get all the providers list through this interface, and then carry out subsequent processing

 

Abstract directory is a basic implementation class in directory. It mainly encapsulates some common methods and mainly relies on the real implementation of subclasses.

 

 

 

So when did directory come into being? Can we recall that we also saw the registry directory when the service was referenced? Now let’s look at the code method

 

 

 

Are you familiar with it

Well, by now, the source code of our cluster fault tolerance mechanism is basically finished