Dubbo3.0如何实现进行路由扩展

前言

Dubbo提供了丰富的路由配置规则,但有时候确实有一些自定义的路由规则在Dubbo的现有配置框架下不能满足我们的需求,需要我们对路由进行扩展。[官网文档]: https://dubbo.apache.org/zh/docs/references/spis/router/ 提供了范例怎么做,但是这个范例可能还是2.7或以前版本时代的事儿,经实测在3.0时代好像行不通。。

关键类介绍

这一part介绍几个路由相关的关键类,没时间可以直接跳到如何扩展路由

RouterFactory 接口

这个接口是个路由器工厂类,用来生产Router实例对象。从注释看,从2.7版本以后有一些特性的改变

@SPI
public interface RouterFactory {
    /**
     * Create router.
     * Since 2.7.0, most of the time, we will not use @Adaptive feature, so it's kept only for compatibility.
     */
    @Adaptive("protocol")
    Router getRouter(URL url);
}

CacheableRouterFactory抽象类

这是3.0版本新加的个抽象类,实现了RouterFactory接口,按照注释,这是基于2.7版本的一个基础抽象类,如果要自定义路由规则的话,在2.x的版本直接实现RouterFactory接口,3.0版本则推荐继承这个类,实现了一个缓存功能,减少创建对象的开销。

/**
 * If you want to provide a router implementation based on design of v2.7.0, please extend from this abstract class.
 * For 2.6.x style router, please implement and use RouterFactory directly.
 */
public abstract class CacheableRouterFactory implements RouterFactory {
    private ConcurrentMap<String, Router> routerMap = new ConcurrentHashMap<>();

    @Override
    public Router getRouter(URL url) {
        return routerMap.computeIfAbsent(url.getServiceKey(), k -> createRouter(url));
    }

    protected abstract Router createRouter(URL url);
}

Router 接口

这个是真正的路由接口,我删除了一些与本文无关的源码和注释,我们的自定义路由器就要实现这个接口。其中标记为@Deprecated的route方法是之前的用法,是直接挑选一些符合路由规则的InvokerList。下面的route方法返回对象为RouterResult,而不直接是Invoker集合了。这个RouterResult会在RouterChain中用到,做了一些路由规则的优化。

/**
 * Router. (SPI, Prototype, ThreadSafe)
 */
public interface Router extends Comparable<Router> {

    /** Get the router url. */
    URL getUrl();

    /**
     * Filter invokers with current routing rule and only return the invokers that comply with the rule.
     */
    @Deprecated
    default <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
        return null;
    }

    /**
     * ** This method can return the state of whether routerChain needed to continue route. **
     * Filter invokers with current routing rule and only return the invokers that comply with the rule.
     */
    default <T> RouterResult<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation,
                                                     boolean needToPrintMessage) throws RpcException {
        return new RouterResult<>(route(invokers, url, invocation));
    }
}

AbstractRouter 抽象类

这个抽象类是在2.7和3.0都有的抽象类,没有什么变化,虽然官网说的是扩展路由类要实现Router接口,但实际直接继承AbstractRouter会更方便

public abstract class AbstractRouter implements Router {
    protected int priority = DEFAULT_PRIORITY;
    protected boolean force = false;
    protected URL url;

    protected GovernanceRuleRepository ruleRepository;

    public AbstractRouter(URL url) {
        this.ruleRepository = ExtensionLoader.getExtensionLoader(GovernanceRuleRepository.class).getDefaultExtension();
        this.url = url;
    }

    public AbstractRouter() {
    }
}

RouterChain 类

这是路由链的组织协调类,我们定义的路由规则在这里组织起来并生效。这个类在2.7和3.0之间也有一些不同这个以后再说。重点看一下 simpleRoute 方法就好,里面有个routers的for循环,就是路由筛选的过程。由于源码较长,这里就不贴了,感兴趣朋友们自行查看。

怎么扩展路由

实现抽象类

  • org.apache.dubbo.rpc.cluster.CacheableRouterFactory
  • org.apache.dubbo.rpc.cluster.router.AbstractRouter

Maven项目结构

src
 |-main
    |-java
        |-com
            |-xxx
                |-XxxRouterFactory.java (继承CacheableRouterFactory抽象类)
                |-XxxRouter.java (继承AbstractRouter抽象类)
    |-resources
        |-META-INF
            |-dubbo
                |-org.apache.dubbo.rpc.cluster.RouterFactory (纯文本文件,内容为:xxx=com.xxx.XxxRouterFactory)

xxxRouterFactory.java

package com.xxx;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.cluster.CacheableRouterFactory;
import org.apache.dubbo.rpc.cluster.Router;

/**
 * AddressRouterFactory
 */
@Activate(value = AddressRouterFactory.NAME, order = 0)
public class xxxRouterFactory extends CacheableRouterFactory {

    public static final String NAME = "xxx";

    @Override
    protected Router createRouter(URL url) {
        return new xxxRouter();
    }
}

xxxRouter.java

package com.xxx;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.router.AbstractRouter;
import org.apache.dubbo.rpc.cluster.router.RouterResult;
import java.util.List;

public class xxxRouter extends AbstractRouter {

    private static final int ADDRESS_INVOKERS_DEFAULT_PRIORITY = 180;

    public AddressInvokersSelector() {
        setPriority(ADDRESS_INVOKERS_DEFAULT_PRIORITY);
    }

    @Override
    public <T> RouterResult<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation, boolean needToPrintMessage) throws RpcException {
//        你的代码;
        return new RouterResult<>(invokers);
    }
}

配置spi文件

在/resrouces/META-INF/dubbo/目录新建org.apache.dubbo.rpc.cluster.RouterFactory配置文件,文件内容为下面的内容

xxx=com.xxx.XxxRouterFactory

这里要注意几点:

  1. 是META-INF,不是META-INFO!不是META-INFO!不是META-INFO!
  2. 确定maven工程的编辑脚本会将META-INF文件夹及内容拷贝到target的class目录
  3. 配置文件的文件名就是org.apache.dubbo.rpc.cluster.RouterFactory,不是按照这个去建目录
  4. 内容里面key的xxx是和xxxRouterFactory的注解@Activate指定的名字一样

官网到这里就结束了,但是其实还少了一步。

配置consumer的Reference

<dubbo:reference id="aService" interface="org.test.api.AServiceInterface" router="xxx"/>

或下面形式
<dubbo:reference interface="org.apache.dubbo.samples.version.api.VersionService" id="versionService">
        <dubbo:parameter key="router" value="xxx"/>
</dubbo:reference>

注意这里在reference的配置后面加了个router的参数,它的值就是META-INF配置文件内容里面的key。这样才能才能将自定义的路由规则类加载到RouterChain的routers里面。

手打不宜,祝福点赞的大帅哥和大漂亮升职加薪

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值