路由器

自訂路由策略

透過建立自訂路由器,您可以根據業務場景的特性來實現特定的路由方法。

先決條件

選擇以下兩種部署和運行方法之一

基於 Kubernetes

  • 安裝 Kubernetes 環境
  • 修改 提供者 中的設定檔,以啟用部署在 Kubernetes 中的 Nacos 地址
    # (The configuration remains the same as in the original documentation)
    
  • 修改 消費者 中的設定檔,以啟用部署在 Kubernetes 中的 Nacos 地址
    # (The configuration remains the same as in the original documentation)
    
  • 部署 擴展性路由器任務

使用本地 IDE

  • 部署 Nacos 2.2.0 版
  • 修改 提供者 中的設定檔,以啟用本地 Nacos 地址
    # (The configuration remains the same as in the original documentation)
    
  • 修改 消費者 中的設定檔,以啟用本地 Nacos 地址
    # (The configuration remains the same as in the original documentation)
    

任務細節

任務是堅持使用第一個開始提供服務的提供者。如果該提供者離線,則選擇一個新的提供者。

實現方法

在消費者中建立一個自訂路由器。在此路由器中,儲存第一次調用時使用的提供者。對於後續的調用,如果提供者列表包含第一次調用時使用的提供者,則繼續使用它;否則,選擇一個新的提供者。

程式碼結構

src
 |-main
    |-java
        |-org
            |-apache
                |-dubbo
                    |-samples
                        |-extensibility
                            |-router
                                |-consumer
                                    |-router
                                        |-StickFirstStateRouter.java (Implement StateRouter interface)
                                        |-StickFirstStateRouterFactory.java (Implement StateRouterFactory interface)
    |-resources
        |-META-INF
            |-application.properties (Dubbo Consumer configuration file)
            |-dubbo
                |-org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory (Plain text file)

程式碼細節

  • StickFirstStateRouter
package org.apache.dubbo.samples.extensibility.router.consumer.router;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.Holder;
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.RouterSnapshotNode;
import org.apache.dubbo.rpc.cluster.router.state.AbstractStateRouter;
import org.apache.dubbo.rpc.cluster.router.state.BitList;

public class StickFirstStateRouter<T> extends AbstractStateRouter<T> implements ConfigurationListener {
    public StickFirstStateRouter(URL url) {
        super(url);
    }

    public static final String NAME = "STICK_FIRST_ROUTER";
    private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(StickFirstStateRouter.class);
    private volatile BitList<Invoker<T>> firstInvokers;

    @Override
    protected BitList<Invoker<T>> doRoute(BitList<Invoker<T>> invokers, URL url, Invocation invocation, boolean needToPrintMessage, Holder<RouterSnapshotNode<T>> routerSnapshotNodeHolder, Holder<String> messageHolder) throws RpcException {
        if (CollectionUtils.isEmpty(invokers)) {
            if (needToPrintMessage) {
                messageHolder.set("Directly Return. Reason: Invokers from previous router is empty.");
            }
            return invokers;
        }
        BitList<Invoker<T>> copy = invokers.clone();
        if (CollectionUtils.isEmpty(copy)) {
            this.firstInvokers = new BitList<>(BitList.emptyList());
            this.firstInvokers.add(copy.get(0));
        } else {
            this.firstInvokers = copy.and(invokers);
            if(CollectionUtils.isEmpty(this.firstInvokers)){
                this.firstInvokers.add(copy.get(0));
            }
        }
        return this.firstInvokers;
    }

    @Override
    public void process(ConfigChangedEvent event) {
        if (logger.isDebugEnabled()) {
            logger.debug("Notification of tag rule, change type is: " + event.getChangeType() + ", raw rule is:\n " +
                    event.getContent());
        }
        // Reset
        if (event.getChangeType().equals(ConfigChangeType.DELETED)) {
            this.firstInvokers = null;
        }
    }

    @Override
    public void stop() {
        super.stop();
        this.firstInvokers = null;
    }
}
  • StickFirstStateRouterFactory
package org.apache.dubbo.samples.extensibility.router.consumer.router;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.cluster.router.state.StateRouter;
import org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory;

public class StickFirstStateRouterFactory implements StateRouterFactory {
    @Override
    public <T> StateRouter<T> getRouter(Class<T> interfaceClass, URL url) {
        return new StickFirstStateRouter<>(url);
    }
}

SPI 配置

請在 resources/META-INF/dubbo/org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory 檔案中加入以下設定:

stickfirst=org.apache.dubbo.samples.extensibility.router.consumer.router.StickFirstStateRouterFactory

設定檔

請在 resources/application.properties 檔案中加入以下設定:

# Configure custom router
dubbo.consumer.router=stickfirst

執行結果

使用**透過本機 IDE** 的方法執行任務,結果如下:

dubbo-samples-extensibility-router-output.png

總而言之,Dubbo 的擴充性允許您建立自定義路由器,提供根據您的業務需求自定義路由邏輯的方法。本教學示範如何建立「固定使用第一個服務提供者」的路由策略,這對於優化網路流量和減少延遲非常有用。


最後修改日期:2023 年 11 月 1 日:翻譯擴充套件相關文件 (#2844) (e603103c03a)