Router

本文讲解如何通过扩展 Router 实现自定义路由策略,可以根据业务场景的特点来实现特定的路由方式。

通过自定义路由,可以根据业务场景的特点来实现特定的路由方式。本示例 router 扩展实现源码请参见 dubbo-samples-extensibility

开始之前

任务详情

对所有的请求都使用第一提供服务的Provider,如果该Provider下线,则从新选择一个新的Provider。

实现方式

在Consumer中自定义一个Router,在Router中将第一次调用的Provider保存下来,如果后续有请求调用且Provider列表中包含第一次调用时使用的Provider,则继续使用第一次调用时使用的Provider,否则重新选去一个Provider。

代码结构

  1. src
  2. |-main
  3. |-java
  4. |-org
  5. |-apache
  6. |-dubbo
  7. |-samples
  8. |-extensibility
  9. |-router
  10. |-consumer
  11. |-router
  12. |-StickFirstStateRouter.java (实现StateRouter接口)
  13. |-StickFirstStateRouterFactory.java (实现StateRouterFactory接口)
  14. |-resources
  15. |-META-INF
  16. |-application.properties (Dubbo Consumer配置文件)
  17. |-dubbo
  18. |-org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory (纯文本文件)

代码详情

  • StickFirstStateRouter
  1. package org.apache.dubbo.samples.extensibility.router.consumer.router;
  2. import org.apache.dubbo.common.URL;
  3. import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
  4. import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
  5. import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
  6. import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
  7. import org.apache.dubbo.common.logger.LoggerFactory;
  8. import org.apache.dubbo.common.utils.CollectionUtils;
  9. import org.apache.dubbo.common.utils.Holder;
  10. import org.apache.dubbo.rpc.Invocation;
  11. import org.apache.dubbo.rpc.Invoker;
  12. import org.apache.dubbo.rpc.RpcException;
  13. import org.apache.dubbo.rpc.cluster.router.RouterSnapshotNode;
  14. import org.apache.dubbo.rpc.cluster.router.state.AbstractStateRouter;
  15. import org.apache.dubbo.rpc.cluster.router.state.BitList;
  16. public class StickFirstStateRouter<T> extends AbstractStateRouter<T> implements ConfigurationListener {
  17. public StickFirstStateRouter(URL url) {
  18. super(url);
  19. }
  20. public static final String NAME = "STICK_FIRST_ROUTER";
  21. private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(StickFirstStateRouter.class);
  22. private volatile BitList<Invoker<T>> firstInvokers;
  23. @Override
  24. protected BitList<Invoker<T>> doRoute(BitList<Invoker<T>> invokers, URL url, Invocation invocation, boolean needToPrintMessage, Holder<RouterSnapshotNode<T>> routerSnapshotNodeHolder, Holder<String> messageHolder) throws RpcException {
  25. if (CollectionUtils.isEmpty(invokers)) {
  26. if (needToPrintMessage) {
  27. messageHolder.set("Directly Return. Reason: Invokers from previous router is empty.");
  28. }
  29. return invokers;
  30. }
  31. BitList<Invoker<T>> copy = invokers.clone();
  32. if (CollectionUtils.isEmpty(copy)) {
  33. this.firstInvokers = new BitList<>(BitList.emptyList());
  34. this.firstInvokers.add(copy.get(0));
  35. } else {
  36. this.firstInvokers = copy.and(invokers);
  37. if(CollectionUtils.isEmpty(this.firstInvokers)){
  38. this.firstInvokers.add(copy.get(0));
  39. }
  40. }
  41. return this.firstInvokers;
  42. }
  43. @Override
  44. public void process(ConfigChangedEvent event) {
  45. if (logger.isDebugEnabled()) {
  46. logger.debug("Notification of tag rule, change type is: " + event.getChangeType() + ", raw rule is:\n " +
  47. event.getContent());
  48. }
  49. // Reset
  50. if (event.getChangeType().equals(ConfigChangeType.DELETED)) {
  51. this.firstInvokers = null;
  52. }
  53. }
  54. @Override
  55. public void stop() {
  56. super.stop();
  57. this.firstInvokers = null;
  58. }
  59. }
  • StickFirstStateRouterFactory
  1. package org.apache.dubbo.samples.extensibility.router.consumer.router;
  2. import org.apache.dubbo.common.URL;
  3. import org.apache.dubbo.rpc.cluster.router.state.StateRouter;
  4. import org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory;
  5. public class StickFirstStateRouterFactory implements StateRouterFactory {
  6. @Override
  7. public <T> StateRouter<T> getRouter(Class<T> interfaceClass, URL url) {
  8. return new StickFirstStateRouter<>(url);
  9. }
  10. }

SPI配置

resources/META-INF/dubbo/org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory文件中添加如下配置:

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

配置文件

resources/application.properties文件中添加如下配置:

  1. # 配置自定义路由
  2. dubbo.consumer.router=stickfirst

运行结果

使用本地IDE的方式来运行任务,结果如下:

dubbo-samples-extensibility-router-output.png

最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)