Dubbo Custom Load Balancing

Keywords: Programming Dubbo Java Load Balance

Want to know the implementation strategy of Dubbo custom load balancing,

First of all, we need to understand the SPI mechanism. See the article.( https://www.jianshu.com/p/46b42f7f593c)

Understanding the SPI mechanism, we extend Dubbo's load balancing strategy based on Dubbo's SPI mechanism

In brief, dubbo's SPI has been extended and implemented by itself, so it has more functions than java's native SPI.

I. Achieving Goals

1. If there is only one dubbo service provider, return directly to the only service provider

2. Use dubbo's default random weight load balancing if there is no specified service provider IP

3. If the specified IP does not have a corresponding service provider, load balancing using dubbo's default random weights

2. The implementation code is as follows

package com.xxx.balance;

import java.util.List;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance;

/**
 * Dubbo Load Balancing Specified Service IP Call Implementation (Implementation Rules)
 * If there is only one dubbo service provider
 * So go back directly. See AbstractLoadBalance for details.
 * If no ip is specified in RpcInvoker, dubbo's default random weight load balancing is used
 * If an ip is specified in RpcInvoker, but the provider list does not have this ip address, dubbo's default random weight load balancing is still used.
 *
 */
public class DispatcherLoadBalance extends RandomLoadBalance {


	@Override
	protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
		String ip = RpcInvoker.get();
		if (ip == null) {
			return super.doSelect(invokers, url, invocation);
		}
		RpcInvoker.remove();
		for (Invoker<T> invoker : invokers) {
			String host = invoker.getUrl().getHost();
			if (host.equals(ip)) {
				return invoker;
			}
		}
		return super.doSelect(invokers, url, invocation);
	}
}
package com.xxx.balance;

/**
 * Specify dubbo service IP calls
 *
 */
public class RpcInvoker {
	
	private static ThreadLocal<String> holder = new ThreadLocal<>();
	
	public static void set(String ip){
		holder.set(ip);
	}
	
	//************ Visit levels are not exposed to other types of non-contracted ********//
	
	static void remove(){
		holder.remove();
	}
	
	static String get(){
		return holder.get();
	}
	
	
	
}

ThreadLocal is used here to ensure thread variable security

Configuration of DubboSpi

First add a directory called META-INF to the project's classpath directory

The catalogue structure is as follows:

The document reads as follows. Note that the package and class names are based on the actual path of your own project.

dispatcher=com.xxx.balance.DispatcherLoadBalance

4. Consumption Configuration of Service Providers

Add a load balance = "dispatcher" in the dubbo:reference node, as shown below

<dubbo:reference check="false" interface="com.xxx.HelloService" id="helloService" loadbalance="dispatcher"/>

The above process completes a dubbo custom load balancing strategy implementation.

See the dubbo source code for what features SPI can extend, as shown below

Posted by han2754 on Thu, 12 Sep 2019 00:56:27 -0700